package com.yaoyicloud.render.cso;

import java.io.IOException;
import java.util.Map;
import com.deepoove.poi.policy.RenderPolicy;
import com.yaoyicloud.easier.common.file.core.FileProperties;
import com.yaoyicloud.easier.common.file.core.FileTemplate;
import com.yaoyicloud.factory.CSOAbstractRender;
import org.apache.commons.collections4.MapUtils;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
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.message.CSOProtos;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class EntPromotionDetailsRender extends CSOAbstractRender {
    private final FilerepoProperties filerepoProperties;
    private final CommonDataCache commonDataCache;

    public EntPromotionDetailsRender(String cwd, FileTemplate ossTemplate, FileProperties properties,
        FilerepoProperties filerepoProperties,
        CommonDataCache commonDataCache) {
        super(cwd, ossTemplate, properties);
        this.filerepoProperties = filerepoProperties;
        this.commonDataCache = commonDataCache;
    }

    @Override
    protected Map<String, Object> renderDocx(String info, String relationId, Integer counter) throws IOException {
        log.info("开始渲染CSO企业报告推广明细模块，relationId: {}", relationId);

        CSOProtos.PromotionDetails.Builder promotionSummary = CSOProtos.PromotionDetails.newBuilder();
        JsonFormat.parser().merge(info, promotionSummary);

        CSOProtos.PromotionDetails defaultInstance = CSOProtos.PromotionDetails.getDefaultInstance();
        CSOProtos.PromotionDetails mergedProto = defaultInstance.toBuilder()
            .mergeFrom(promotionSummary.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>>() {});
        // 将用户积分数据添加到渲染数据中
        Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
        if (MapUtils.isNotEmpty(commonDataCacheData)) {
            data.putAll(commonDataCacheData);
        }
        data.put("levelInteger", counter);
        fillDefaultValues(data);

        return data;
    }

    @Override
    protected ConfigureBuilder builder(String relationId) {
        ConfigureBuilder builder = Configure.builder();
        RenderPolicy policy = this.indicatorsRenderPolicyToProtobuf();
        builder.bind("userTasks", policy);
        builder.bind("tasks", policy);
        builder.bind("meetingList", policy);
        builder.bind("taskType515253List", policy);
        builder.bind("taskType55List", policy);
        builder.bind("taskType5657List", policy);
        builder.bind("taskType811List", policy);
        builder.bind("taskType910List", policy);
        builder.bind("policyInfoCollectList", policy);
        builder.bind("competingProductsInfoCollectList", policy);
        builder.bind("inventoryInfoCollectList", policy);
        builder.bind("taskType16List", policy);
        builder.bind("taskType18List", policy);
        builder.bind("taskType15List", policy);
        builder.bind("taskType45List", policy);
        builder.bind("taskType54List", policy);
        builder.bind("taskType74List", policy);
        builder.bind("taskType75List", policy);
        builder.bind("taskType76List", policy);
        builder.bind("taskType7879List", policy);
        builder.bind("taskType80List", policy);
        builder.bind("taskType81List", policy);
        builder.bind("taskType38List", policy);
        builder.bind("taskType394041List", policy);
        builder.bind("taskType46List", policy);
        builder.bind("taskType47484950List", policy);
        builder.bind("taskType19List", policy);
        builder.bind("taskType343536List", policy);
        builder.bind("taskType87List", policy);
        builder.bind("taskType86List", policy);
        builder.bind("taskType85List", policy);
        builder.bind("customerVisitList", policy);
        builder.bind("taskType424344List", policy);
        return builder;
    }

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

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

    /**
     * 填充默认值，确保所有必要字段都存在
     */
    @SuppressWarnings("checkstyle:MethodLength")
    private void fillDefaultValues(Map<String, Object> data) {
        processJsonData(data, "userTasks");
        processJsonData(data, "tasks");
        processJsonData(data, "meetingList");
        processJsonData(data, "taskType515253List");
        processJsonData(data, "taskType55List");
        processJsonData(data, "taskType5657List");
        processJsonData(data, "taskType811List");
        processJsonData(data, "taskType910List");
        processJsonData(data, "policyInfoCollectList");
        processJsonData(data, "competingProductsInfoCollectList");
        processJsonData(data, "inventoryInfoCollectList");
        processJsonData(data, "taskType16List");
        processJsonData(data, "taskType18List");
        processJsonData(data, "taskType15List");
        processJsonData(data, "taskType45List");
        processJsonData(data, "taskType54List");
        processJsonData(data, "taskType74List");
        processJsonData(data, "taskType75List");
        processJsonData(data, "taskType76List");
        processJsonData(data, "taskType7879List");
        processJsonData(data, "taskType80List");
        processJsonData(data, "taskType81List");
        processJsonData(data, "taskType38List");
        processJsonData(data, "taskType394041List");
        processJsonData(data, "taskType46List");
        processJsonData(data, "taskType47484950List");
        processJsonData(data, "taskType19List");
        processJsonData(data, "taskType343536List");
        processJsonData(data, "taskType85List");
        processJsonData(data, "taskType86List");
        processJsonData(data, "taskType87List");
        processJsonData(data, "customerVisitList");
        processJsonData(data, "taskType424344List");
        processJsonData(data, "taskType801List");

    }
}
