package net.northking.cctp.executePlan.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import net.northking.cctp.executePlan.constants.MsgConstant;
import net.northking.cctp.executePlan.enums.HtmlSpaceEnum;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.List;

/**
 * 生成word文档工具类
 */
public class WordUtils {

    /**
     * 创建标题
     *
     * @param paragraph
     * @param data
     */
    public void createTitle(XWPFParagraph paragraph, String data) {
        XWPFRun createRun = paragraph.insertNewRun(0);
        createRun.setText(data);
        createRun.setFontFamily(MsgConstant.SIMSUN);
        createRun.setFontSize(14);
        createRun.setBold(true);
        paragraph.setSpacingAfter(10);
        paragraph.setSpacingBefore(10);
        //对齐方式
        paragraph.setAlignment(ParagraphAlignment.LEFT);
    }

    /**
     * 创建表格
     *
     * @param document
     * @param tableList
     */
    public void createTable(XWPFDocument document, List<String[]> tableList) {
        XWPFTable table = document.createTable(tableList.size(), tableList.get(0).length);
        //设置表格的宽度
        CTTblWidth comTableWidth = table.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.PCT);
        comTableWidth.setW(BigInteger.valueOf(5000));

        // 填充数据
        int length = tableList.size();
        for (int i = 0; i < length; i++) {
            XWPFTableRow row = table.getRow(i);
            List<XWPFTableCell> cells = row.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                cells.get(j).setText(tableList.get(i)[j]);
            }
        }

    }

    /**
     * 生成echart图片并添加到word中
     *
     * @param paragraph 创建段落
     * @param imgBase64 Base64图片
     * @throws IOException
     * @throws InvalidFormatException
     */
    public void addWordPicture(XWPFParagraph paragraph, String imgBase64) throws IOException, InvalidFormatException {
        if (!imgBase64.isEmpty()) {
            paragraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun run = paragraph.createRun();
            run.addCarriageReturn();
            //替换转移字符
            String newImageInfo = imgBase64.replaceAll(" ", "+");
            //取出图片部分
            String[] arr = newImageInfo.split(MsgConstant.REGEX_BASE64);
            //添加echarts图片替换数据
            imgBase64 = arr[1];
            byte[] bytes = Base64.getDecoder().decode(imgBase64);
            String fileName = DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN) + ".jpg";
            run.addPicture(new ByteArrayInputStream(bytes), Document.PICTURE_TYPE_PNG, fileName,
                    Units.toEMU(500), Units.toEMU(120));
        }

    }

    public static String replaceHtmlSpace(String str){
        String outStr = str;
        for (HtmlSpaceEnum htmlSpaceEnum : HtmlSpaceEnum.values()) {
            outStr = outStr.replace(htmlSpaceEnum.getName(), htmlSpaceEnum.getValue());
        }
        return outStr;
    }
}


