package com.yaoyicloud.factory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import com.deepoove.poi.config.ConfigureBuilder;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

@Slf4j
public abstract class FXYAbstractRender extends AbstractRender {

    public FXYAbstractRender(String cwd) {
        super(cwd);
    }

    @SuppressWarnings("checkstyle:ParameterNumber")
    public final String createFXYDocument(String info, InputStream inputStream, String relationId, Integer counter,
        String moduleType)
        throws IOException {
        // 获取数据
        Map<String, Object> originalMap = this.renderDocx(info, relationId, counter);
        ConfigureBuilder builder = this.builder(relationId);
        // String docxResultPath = this.documentCompile(originalMap, inputStream, builder, relationId, moduleType);
        String docxResultPath = this.fxyCompile(originalMap, inputStream, builder, relationId, moduleType);
        log.info("文档导出成功，文档路径为: {}", docxResultPath);
        return docxResultPath;
    }

    @SuppressWarnings({"checkstyle:ParameterNumber", "checkstyle:NestedIfDepth"})
    public String fxyCompile(Map<String, Object> dataMap, InputStream inputStream,
        ConfigureBuilder builder, String relationId, String moduleType)
        throws IOException {
        String resultPath = super.documentCompile(dataMap, inputStream, builder, relationId, moduleType);

        // 特别处理经营异常模块 财务信息表格行动态展示
        Boolean financialInfoFlag = (Boolean) dataMap.get("financialInfo_flag");
        if (financialInfoFlag != null && !financialInfoFlag) {


            try (XWPFDocument doc = new XWPFDocument(new FileInputStream(resultPath))) {
                List<XWPFTable> tables = doc.getTables();

                if (tables.size() > 1) {
                    XWPFTable targetTable = tables.get(1);
                    List<XWPFTableRow> rows = targetTable.getRows();

                    if (!rows.isEmpty()) {

                        XWPFTableRow firstRow = rows.get(0);

                        if (!firstRow.getTableCells().isEmpty()) {
                            XWPFTableCell firstCell = firstRow.getTableCells().get(0);
                            StringBuilder cellContent = new StringBuilder();
                            for (XWPFParagraph para : firstCell.getParagraphs()) {
                                for (XWPFRun run : para.getRuns()) {
                                    String text = run.getText(0);
                                    if (text != null) {
                                        cellContent.append(text);
                                    }
                                }
                            }
                            String cellText = cellContent.toString().trim();

                            if (cellText.contains("审查项")) {
                                if (rows.size() > 0) {
                                    int lastRowIndex = rows.size() - 1;
                                    targetTable.removeRow(lastRowIndex);
                                }
                            }
                        }
                    }

                    try (FileOutputStream out = new FileOutputStream(resultPath)) {
                        doc.write(out);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException("处理表格行时发生错误", e);
            }

        }

        return resultPath;
    }

}
