package com.yaoyicloud.render;

import java.io.IOException;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.policy.RenderPolicy;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.util.JsonFormat;
import com.yaoyicloud.config.CommonDataCache;
import com.yaoyicloud.config.FilerepoProperties;
import com.yaoyicloud.factory.FXYAbstractRender;

import com.yaoyicloud.message.FxyProtos;
import lombok.extern.slf4j.Slf4j;

/**
 * ServiceProviderInfo渲染器
 *
 */
@Slf4j
public final class CoverRender extends FXYAbstractRender {
    private final FilerepoProperties filerepoProperties;
    private final CommonDataCache commonDataCache;

    public CoverRender(String cwd, FilerepoProperties filerepoProperties,
        CommonDataCache commonDataCache) {
        super(cwd);
        this.filerepoProperties = filerepoProperties;
        this.commonDataCache = commonDataCache;
    }

    @Override
    protected Map<String, Object> renderDocx(String info, String relationId, Integer counter) throws IOException {
        log.info("开始渲染公共封面模块，relationId: {}", relationId);

        FxyProtos.AuditResult.Builder auditResultBuilder = FxyProtos.AuditResult.newBuilder();
        JsonFormat.parser().merge(info, auditResultBuilder);

        FxyProtos.AuditResult defaultInstance = FxyProtos.AuditResult.getDefaultInstance();
        FxyProtos.AuditResult mergedProto = defaultInstance.toBuilder()
            .mergeFrom(auditResultBuilder.build())
            .build();

        String completeJson = JsonFormat.printer()
            .includingDefaultValueFields()
            .print(mergedProto);
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
        data.replaceAll((k, v) -> v.equals("") ? "-" : v);
        String tenantName = (String) data.get("tenantName");
        String name = (String) data.get("serviceProviderName");
        String type = (String) data.get("type");
        String reportDate = LocalDate.now().toString();
        HashMap<String, Object> commonData = new HashMap<>();
        commonData.put("tenantName", tenantName);
        commonData.put("type", type);
        commonData.put("reportDate", reportDate);
        commonData.put("name", name);

        commonDataCache.addDataMap(relationId, commonData);
        data.put("levelInteger", counter);
        data.putAll(commonData);
        fillDefaultValues(data);
        return data;
    }

    @Override
    protected ConfigureBuilder builder(String relationId) {
        // 配置POI-TL渲染器
        ConfigureBuilder builder = Configure.builder();
        RenderPolicy indicatorsRenderPolicy = this.getScoreRenderPolicy();
        builder.bind("checkItemScores", indicatorsRenderPolicy);
        return builder;
    }

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

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

    /**
     * 填充默认值，确保所有必要字段都存在
     */
    private void fillDefaultValues(Map<String, Object> data) {
        Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("overallSummary");
        basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);

    }

}
