package com.yaoyicloud.template;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;

import com.deepoove.poi.data.Texts;
import com.deepoove.poi.policy.RenderPolicy;
import com.yaoyicloud.config.FilerepoProperties;
import com.yaoyicloud.constant.enums.ReportType;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class AcademicAssociationReport extends AbstractReportExporter {
    private final FilerepoProperties filerepoProperties;

    @Override
    protected String getReportImagePath() {
        return filerepoProperties.getReportImagePath();
    }



    @Override
    protected String getTemplate(ReportType reportType, String basePath) {
        String reportTemplateFileName = getReportTemplateFileName(reportType);
        return basePath + File.separator + reportTemplateFileName;
    }

    @Override
    protected void bindConfigure(ConfigureBuilder builder) {
        RenderPolicy indicatorsRenderPolicy = this.indicatorsRenderPolicy();
            RenderPolicy scoreRenderPolicy = this.getScoreRenderPolicy();
        builder.bind("checkItemScores", scoreRenderPolicy).bind("basicInfoChecks", indicatorsRenderPolicy)
                    .bind("businessAbnormals", indicatorsRenderPolicy).bind("dishonestPersons", indicatorsRenderPolicy)
                    .bind("penaltyRecords", indicatorsRenderPolicy).bind("taxPenalties", indicatorsRenderPolicy)
                    .bind("severeViolations", indicatorsRenderPolicy).bind("questionnaireItems", indicatorsRenderPolicy)
                    .bind("indicators", indicatorsRenderPolicy).bind("superiorAuthority", indicatorsRenderPolicy);
    }

    @Override
    protected String imagePath() {
        return filerepoProperties.getReportImagePath();
    }


    @Override
    protected void renderTemplate(String reportTempWordFile, String templatePath, ConfigureBuilder builder,
        Map<String, Object> data) throws IOException {
        Configure config = builder.build();
        XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(data);
        template.writeToFile(reportTempWordFile);
        template.close();
    }



    @Override
    protected String getBasicPath() throws IOException {
        return filerepoProperties.getBasePath();
    }

    /**
     * 获取模板文件路径
     *
     * @param reportType
     * @return
     */
    public String getReportTemplateFileName(ReportType reportType) {
        return reportType.getType().toLowerCase() + "_template.docx";
    }

    @Override
    protected List<String> getFontPaths() {
        return Arrays.asList(filerepoProperties.getSourceHanSansCnFontMediumPath());
    }

    @Override
    public Map<String, Object> processData(Map<String, Object> rawData) throws Exception {
        Map<String, Object> processedData = new HashMap<>(rawData);

        String docurl1 = (String) processedData.get("docurl1");
        if (StrUtil.isNotBlank(docurl1)) {
            processedData.put("docurl1", Texts.of(docurl1).link(docurl1).create());
        }


        if (processedData.containsKey("docurl1Text")) {
            int i = 2;
            while (processedData.containsKey("docurl" + i)) {
                String url = (String) processedData.get("docurl" + i);
                if (StrUtil.isNotBlank(url)) {
                    processedData.put("docurl" + i, Texts.of(url).link(url).create());
                }
                i++;
            }
        }



        // Process other specific links
        processLink(processedData, "socialSecurityDetailsLink");
        processLink(processedData, "taxDeclarationImage");
        processLink(processedData, "financialInfoLink");

        // Process image lists
        processImageListToNumberedRenderData("files", processedData);
        processImageListToNumberedRenderData("relatedEntitiesImage", processedData);
        processImageListToNumberedRenderData("interestConflict", processedData);

        processImageListToNumberedRenderData("businessLicenseImage", processedData);
        processImageListToNumberedRenderData("legalRegistrationCertImages", processedData);
        processImageListToNumberedRenderData("annualInspectionResultImage", processedData);
        processImageListToNumberedRenderData("bankCertificationImages", processedData);
        processImageListToNumberedRenderData("assessmentLevelCertImage", processedData);
        processImageListToNumberedRenderData("fullCharterImage", processedData);
        processImageListToNumberedRenderData("topDonorsListImage", processedData);
        processImageListToNumberedRenderData("topExpenditureListImage", processedData);
        processImageListToNumberedRenderData("managementFilingFormImage", processedData);
        processImageListToNumberedRenderData("jigoupinggu", processedData);
        processImageListToNumberedRenderData("nianjianjieguo", processedData);
        processImageListToNumberedRenderData("branchInfoImage", processedData);
        processImageListToNumberedRenderData("principalInfoImage", processedData);
        processImageListToNumberedRenderData("projectInfoImage", processedData);
        processImageListToNumberedRenderData("otherInfoImage", processedData);
        processImageListToNumberedRenderData("zongti", processedData);

        return processedData;
    }
}
