package cn.iocoder.yudao.module.product.utils;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionRespVO;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class WordUtils {

    /**
     * 根据word模板和参数生成图片
     */
    public static void convertToImageByWordTemplate(String templatePath, String imagePath, Map<String, Object> params) {
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
            BufferedImage image = WordUtils.wordToImage(doc);
            FileOutputStream fos = new FileOutputStream(imagePath);
            ImageIO.write(image, "png", fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage wordToImage(XWPFDocument docx) throws IOException {
        StringWriter writer = new StringWriter();
        XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
        // 获取Html
        xhtmlConverter.convert(docx, writer, null);

        // 转换得到的Html
        String html = writer.toString();
        String formatHtml = formatHtmlString(html);
        System.out.println(formatHtml);

        HtmlParser htmlParser = new HtmlParserImpl();
        htmlParser.loadHtml(formatHtml);
        ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
        return imageRenderer.getBufferedImage();
    }

    public static String formatHtmlString(String html) {
        int titleStartIndex = html.indexOf("<p style=");// 找到标题开始的位置
        int titleSemicolonIndex = html.indexOf(";", titleStartIndex);// 找到第一个样式结束的位置
        // 将标题位置居中
        html = html.substring(0, titleStartIndex) + "<p style=\"text-align: center" + html.substring(titleSemicolonIndex);
        // 在style标签中添加自定义table样式
        int styleIndex = html.indexOf("<style>");
        html = html.substring(0, styleIndex + 7) + "table {border-collapse: collapse;}table td {border: 1px solid #000;}" + html.substring(styleIndex + 7);

        // 将table宽度设定为百分百
        int firstTableIndex = html.indexOf("<table");
        html = html.substring(0, firstTableIndex) + "<table style=\"width: 100%;border-collapse: collapse;\"" + html.substring(firstTableIndex + 6);
        int secondTableIndex = html.indexOf("<table", firstTableIndex + 6);
        html = html.substring(0, secondTableIndex) + "<table style=\"width: 100%;border-collapse: collapse;\"" + html.substring(secondTableIndex + 6);

//        // 将主表tr宽度设定为百分25
//        int firstTableEndIndex = html.indexOf("</table>", firstTableIndex);
//        String inspectionHtml = html.substring(firstTableIndex, firstTableEndIndex + 8);
//        // 循环替换主表所有td的样式
//        int tdIndex = inspectionHtml.indexOf("<td");
//        while (tdIndex != -1) {
//            int tdEndIndex = inspectionHtml.indexOf(">", tdIndex);
//            int colspanIndex = inspectionHtml.indexOf("colspan", tdIndex);
//            // 判断下一个colspan是否更小
//            if(colspanIndex != -1 && colspanIndex < tdEndIndex) {
//                inspectionHtml = inspectionHtml.substring(0, tdIndex) + "<td class=\"a1 a5\" style=\"width:75%;border: 1px solid;\"" + inspectionHtml.substring(colspanIndex);
//            } else {
//                inspectionHtml = inspectionHtml.substring(0, tdIndex) + "<td class=\"a1 a5\" style=\"width:25%;border: 1px solid;\"" + inspectionHtml.substring(tdEndIndex);
//            }
//            tdIndex = inspectionHtml.indexOf("<td", tdIndex + 4);
//        }
//
//        html = html.substring(0, firstTableIndex) + inspectionHtml + html.substring(firstTableEndIndex + 8);
        // 将子表tr宽度设定为百分20

        return html;
    }

    public static void main(String[] args) throws Exception {
        //加载资源文件
        ClassPathResource classPathResource = new ClassPathResource("template/inspection.docx");
        //获取文件
        File file = classPathResource.getFile();
        //获取路径
        String path = classPathResource.getPath();
        //转成输入流
        InputStream in = WordUtils.class.getClassLoader().getResourceAsStream(path);
        //获取文件流
        FileInputStream inputStream = new FileInputStream(file);

        Map<String, Object> params = new HashMap<>();
        params.put("inspection_id", "aaaaaaaaaaaaaaaaaaaa");

        WordUtils.convertToImageByWordTemplate(path, "D://test.png", params);
    }
}
