package org.jeecg.modules.stm.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.DocumentRenderPolicy;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.deepoove.poi.data.NumberingFormat.DECIMAL_PARENTHESES;

public class CreateWordUtil {

    private static Map<String, Object> datasTemplate;
    private  ConfigureBuilder builder;
    private  String basePath = "";
    public  void addText(String strKey,String text){
        datasTemplate.put(strKey, text);
    }

    public void addList(String strKey,List<String> list){
       if(list==null) {
           datasTemplate.put(strKey, null);
       } else {
           String[] array = list.toArray(new String[0]);
           Numberings.NumberingBuilder numberingBuilder = Numberings.of(DECIMAL_PARENTHESES);
           for(int i=0;i< array.length;i++) {
               numberingBuilder.addItem(array[i]);
           }
           datasTemplate.put(strKey, numberingBuilder.create());
       }
    }

    public  void addObject(String strKey,Object obj){
        datasTemplate.put(strKey, obj);
    }

    public  void addTable(String strKey, RowRenderData header, List<String[]> tableDatas, Map<String, Object> cmap){
        TableRenderData tableObj = new TableRenderData();
        tableObj.addRow(header);
        TableStyle tableStyle = new TableStyle();
        tableStyle.setColWidths(new int[]{1300, 2500,3000,1300});
        tableStyle.setWidth("8500");
        tableObj.setTableStyle(tableStyle);
        if (tableDatas.size() >0) {
            for(int i=0;i<tableDatas.size();i++) {
                tableObj.addRow(Rows.of(tableDatas.get(i)).rowHeight(10).create());
            }
            cmap.put(strKey, tableObj);
        } else {
            cmap.put(strKey, null);
        }
    }

    public  void addAreasDetail(String strKey,List<Map<String,Object>> dataList){
        List<Map<String, Object>> tmpList = new ArrayList<>();
        for(int i=0;i<dataList.size();i++) {
            Map<String,Object> dataItem = dataList.get(i);
            String strTitle = dataItem.get("title").toString();
            String strDesc = dataItem.get("desc").toString();
            Map<String, Object> cmap = new HashMap<>();
            cmap.put("detailTitle", strTitle);
            cmap.put("detailDesc", strDesc);
            Map<String, Object> mapTable = new HashMap<>();
            List<String[]> data0 = (List<String[]>)dataItem.get("data0");
            List<String[]> data1 = (List<String[]>)dataItem.get("data1");
            List<String[]> data2 = (List<String[]>)dataItem.get("data2");
            List<String[]> data3 = (List<String[]>)dataItem.get("data3");

            RowRenderData header = Rows.of("指标编号","指标名称","应采取措施","填报值")
                    .textColor("FFFFFF").bgColor("ff9800").center()
                    .rowHeight(10).create();

            int ind = 1;
            if(data0.size()>0) {
                cmap.put("tableTitle0",ind+".数据填报不完整");
                addTable("detailTable0",header,data0,cmap);
                cmap.put("detail0",true);
                ind++;
            } else {
                cmap.put("tableTitle0",null);
                cmap.put("detailTable0",null);
                cmap.put("detail0",false);
            }
            if(data1.size()>0) {
                cmap.put("tableTitle1",ind+".低风险指标(无需处理)");
                addTable("detailTable1",header,data1,cmap);
                cmap.put("detail1",true);
                ind++;
            } else {
                cmap.put("tableTitle1",null);
                cmap.put("detailTable1",null);
                cmap.put("detail1",false);
            }
            if(data2.size()>0) {
                cmap.put("tableTitle2",ind+".中风险指标(需要重点关注)");
                addTable("detailTable2",header,data2,cmap);
                cmap.put("detail2",true);
                ind++;
            } else {
                cmap.put("tableTitle2",null);
                cmap.put("detailTable2",null);
                cmap.put("detail2",false);
            }
            if(data3.size()>0) {
                cmap.put("tableTitle3",ind+".高风险指标(需要人工干预)");
                addTable("detailTable3",header,data3,cmap);
                cmap.put("detail3",true);
            } else {
                cmap.put("tableTitle3",null);
                cmap.put("detailTable3",null);
                cmap.put("detail3",false);
            }

            Map<String, Object> mapMtrc = new HashMap<>();
            mapMtrc.put("mtrc",cmap);
            tmpList.add(mapMtrc);
        }
        this.datasTemplate.put(strKey, tmpList);
        this.setConfig(strKey);
    }

    public  void addChart(String strKey,String chartTitle,Map<String,Integer[]> dataMap,List<String> categories){
        try {

            if(chartTitle==null) {
                chartTitle = "信息科技风险监测情况";
            }
            String[] stringArray = categories.toArray(new String[0]);
            Charts.ChartMultis charts= Charts.ofMultiSeries(chartTitle,stringArray);
            for (Map.Entry<String, Integer[]> entry : dataMap.entrySet()) {
                charts.addSeries(entry.getKey(),entry.getValue());
            }
            datasTemplate.put(strKey,charts.create());

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public  void setConfig(String strKey) {
        this.builder.bind(strKey,new DocumentRenderPolicy());
    }

    public  void ini() {
        this.datasTemplate = new HashMap<String, Object>();
        this.builder = Configure.builder();
    }

    public void clear() {
        this.datasTemplate = null;
        this.builder = null;
    }

    public  String createFile(String rootPath,String fileName) throws Exception {
        this.basePath = rootPath;
        String saveFilePath = getSaveFilePath(fileName);
        builder.useSpringEL();
        try {
            Configure config = builder.build();
            String templatePath = rootPath+ StmConstans.METRIC_REPORT_TEMPLATE_PATH;
            XWPFTemplate template = XWPFTemplate.compile(templatePath,config).render(datasTemplate);
            FileOutputStream out = new FileOutputStream(saveFilePath);
            template.write(out);
            out.flush();
            out.close();
            template.close();
            return StmConstans.METRIC_REPORT_SAVE_PATH + "/" + fileName;
        } catch (Exception e) {
           throw e;
        } finally {
            clear();
        }
    }

    public  String getSaveFilePath(String fileName){
        String strPath = this.basePath  + File.separator + StmConstans.METRIC_REPORT_SAVE_PATH;
        File file = new File(strPath);
        if (!file.exists()) {
            file.mkdirs();// 创建文件根目录
        }
        return strPath + File.separator+ fileName;
    }

    private void setTableData( List<String[]> tableDatas) {
        int len = tableDatas.size();
        RowRenderData[] listRows = new RowRenderData[len+1];
        TableRenderData tableData = new TableRenderData();
        RowRenderData header = Rows.of("指标编号","指标名称","指标说明","风险等级")
                .textColor("FFFFFF").bgColor("ff9800").center()
                .rowHeight(10).create();
        listRows[0] = header;
        tableData.addRow(header);
    }

}
