package com.yaoyicloud.render.cso;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import cn.hutool.core.date.DateUtil;
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.easier.common.file.core.FileProperties;
import com.yaoyicloud.easier.common.file.core.FileTemplate;
import com.yaoyicloud.factory.CSOAbstractRender;
import com.yaoyicloud.message.CSOProtos;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;

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

    public EntHeaderSectionRender(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.ReportHeader.Builder header = CSOProtos.ReportHeader.newBuilder();

        JsonFormat.parser().merge(info, header);
        CSOProtos.ReportHeader defaultInstance = CSOProtos.ReportHeader.getDefaultInstance();
        CSOProtos.ReportHeader mergedProto = defaultInstance.toBuilder()
            .mergeFrom(header.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>>() {});
        commonDataCache.insertData(relationId, "reportCreateTime",
            DateUtil.format(LocalDateTime.now(), "yyyy年MM月dd日 HH:mm:ss"));
        Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
        if (MapUtils.isNotEmpty(commonDataCacheData)) {
            data.putAll(commonDataCacheData);
        }
        commonDataCache.insertData(relationId, "totalPackageScore", data.get("totalPackageScore"));
        commonDataCache.insertData(relationId, "totalApprovedScore", data.get("totalApprovedScore"));
        commonDataCache.insertData(relationId, "taskUserNames", data.get("taskUserNames"));
        commonDataCache.insertData(relationId, "submitedTaskScore", data.get("submitedTaskScore"));
        commonDataCache.insertData(relationId, "createTime", data.get("createTime"));
        commonDataCache.insertData(relationId, "serviceEndDate", data.get("serviceEndDate"));
        commonDataCache.insertData(relationId, "sensitiveFlag", data.get("sensitiveFlag"));
        commonDataCache.insertData(relationId, "scorePackageName", data.get("scorePackageName"));
        commonDataCache.insertData(relationId, "entrustingCompany", data.get("entrustingCompany"));
        commonDataCache.insertData(relationId, "taskFinishTime", data.get("taskFinishTime"));
        commonDataCache.insertData(relationId, "serviceCompany", data.get("serviceCompany"));
        data.put("levelInteger", counter);
        fillDefaultValues(data);
        return data;
    }

    @Override
    protected ConfigureBuilder builder(String relationId) {
        ConfigureBuilder builder = Configure.builder();
        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) {

    }

}
