package com.sifang.modules.sunshine.SystemSetting.utils;

import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProJectCompanyUtils {

    private static  String patten = "^1[3-9]\\d{9}$";


    /**
     * 这是.xlsx文件拿到图片位置通用方法
     */

    public static Map<String, PictureData> getXlsxPictures(XSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<>();
        List<POIXMLDocumentPart> list = sheet.getRelations();
        for (POIXMLDocumentPart part : list) {
            if (part instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) part;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture picture = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = picture.getPreferredSize();
                    PictureData pictureDatas = picture.getPictureData();
                    CTMarker marker = anchor.getFrom();
                    // 列号
                    String key =(marker.getRow()) + "-" + marker.getCol();
                    if (pictureDatas!=null) {
                        map.put(key, pictureDatas);
                    }
                }
            }
        }
        return map;
    }


    /**
     * 这是.xls文件拿到图片位置通用方法
     */

    public static Map<String, PictureData> getXlsPictures(HSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<>();
        List<HSSFShape> hapelList = sheet.getDrawingPatriarch().getChildren();
        for (HSSFShape shape : hapelList) {
            if (shape instanceof HSSFPicture) {
                HSSFPicture picture = (HSSFPicture) shape;
                HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
                PictureData pictureDatas = picture.getPictureData();
                //列号
                String key =cAnchor.getRow1() + "-" +cAnchor.getCol1();
                if (pictureDatas!=null) {
                    map.put(key, pictureDatas);
                }
            }
        }
        return map;
    }

        public static boolean checkTelphone(String telphone) {
            Pattern p = Pattern.compile(patten);
            Matcher matcher = p.matcher(telphone);
            return matcher.find();
        }
}
