package com.yunyan.business.document;

import cn.hutool.core.date.DateUtil;
import com.yunyan.business.constant.TemplateEpType;
import com.yunyan.business.document.common.constant.TemplateFilePathConstants;
import com.yunyan.business.document.y22.FinalY22CompareLastUtils;
import com.yunyan.business.document.y22.ReadFinalExcelY22Utils;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalExcelDataVo;
import com.yunyan.business.document.y23.budget.ReadBudgetExcelY23Utils;
import com.yunyan.business.document.y23.budget.UpdateReportBudgetData;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetDataVo;
import com.yunyan.business.document.y23.finalData.ReadFinalExcelY23Utils;
import com.yunyan.common.utils.StringUtils;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;

@Slf4j
public class WordGeneratorUtils {

    public static String TEMPLATE_WORD_Y2024BT1 = "template/".concat("y2024bt1.template.docx");
    public static String TEMPLATE_WORD_Y2024BT2 = "template/".concat("y2024bt2.template.docx");
    public static String TEMPLATE_WORD_Y2024BT3 = "template/".concat("y2024bt3.template.docx");
    public static String TEMPLATE_WORD_Y2024BT4 = "template/".concat("y2024bt4.template.docx");
    public static String TEMPLATE_WORD_Y2024BT5 = "template/".concat("y2024bt5.template.docx");

    public static String TEMPLATE_WORD_Y2025BT1 = "template/".concat("y2025bt1.template.docx");

    public static boolean generatorWord(Object dataVo, String targetWordFileName) {
        // 获取模板配置文件路径
        String templatePath = TemplateFilePathConstants.TEMPLATE_WORD_Y2022FT1;
        return generatorWord(dataVo, targetWordFileName, templatePath);
    }

    /**
     * 生成 word 文档
     * @param dataVo 数据
     * @param targetWordFileName 目标word文档
     * @return
     */
    public static boolean generatorWord(Object dataVo, String targetWordFileName, String templatePath) {
        if(StringUtils.isBlank(templatePath)) {
            templatePath = TemplateFilePathConstants.TEMPLATE_WORD_Y2022FT1;
        }
        try(
            InputStream templateStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(templatePath);
            OutputStream outputStream = new FileOutputStream(targetWordFileName);
        ) {
            //注册xdocreport实例并加载FreeMarker模板引擎
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(templateStream,
                    TemplateEngineKind.Freemarker);

            //创建xdocreport上下文对象
            IContext context = report.createContext();

            //对象数据
            context.put("dv", dataVo);

            report.process(context, outputStream);
            log.debug("Word文档生成成功！");

            return true;
        } catch (FileNotFoundException e) {
            log.error("word文档生成  文件读取未找到异常 : [{}], [{}]", e.getMessage(), e);
        } catch (IOException e) {
            log.error("word文档生成 IO异常 : [{}], [{}]", e.getMessage(), e);
        } catch (XDocReportException e) {
            log.error("word文档生成 XDocReportException异常 : [{}], [{}]", e.getMessage(), e);
        }

        return false;
    }

}
