package com.casic.missiles.service.listeners.register.data.site;


import com.casic.missiles.dto.business.certificate.ElectronicImage;
import com.casic.missiles.dto.business.file.FilePrintRegister;
import com.casic.missiles.dto.flowable.ApprovalLogResponse;
import com.casic.missiles.enums.notifyevent.PrintFileDataBeanNameAliasEnum;
import com.casic.missiles.model.business.BusinessSiteExecutiveInfo;
import com.casic.missiles.service.listeners.register.data.PrintFileDataGeneric;
import com.casic.missiles.service.listeners.register.data.PrintFileDataSupport;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

@Component(PrintFileDataBeanNameAliasEnum.EXTERNAL_CALIBRATION_FILE_BEAN)
public class CalibrationFileData extends PrintFileDataSupport implements PrintFileDataGeneric {

    @Value("${casic.template.calibration}")
    private String calibrationTemplateName;

    @Override
    public FilePrintRegister CustomizedPrintFileRegister(Object object, String templateFileDir) {
        BusinessSiteExecutiveInfo metaData = (BusinessSiteExecutiveInfo) object;
        //初始化，生成模板文件
        FilePrintRegister filePrintRegister = initialization(templateFileDir, metaData.getId(), getTemplateName(calibrationTemplateName));
        //查询工作流审批记录
        List<List<ApprovalLogResponse>> approvalLogList = approvalOperateService.approvalLog(metaData.getProcessId());
        Map<String, List<ApprovalLogResponse>> ApprovalLogMap = approvalLogList.stream()
                .collect(Collectors.toMap(e -> e.get(0).getTaskName(), e -> e));
        //定制化签名开发预处理，定制化参数
        customizedSignNamesParamCreator();
        //定制化签名开发
        List<ElectronicImage> electronicSignList = this.signNamesCreator(ApprovalLogMap, filePrintRegister);
        filePrintRegister.setElectronicImageList(electronicSignList);
        //填充定制化的参数
        filePrintRegister.setParams(siteFileParamCreator(metaData.getOrderId(), metaData.getId()));
        filePrintRegister.getParams().putAll(populateCalibration(metaData));
        resolverApprovalTime(filePrintRegister, ApprovalLogMap);
        return filePrintRegister;
    }

    private Map<String, Object> populateCalibration(BusinessSiteExecutiveInfo metaData) {
        Map<String, Object> calibrationDataParams = new HashMap<>();
        calibrationDataParams.put("siteExecutiveNo", metaData.getSiteExecutiveNo());
        calibrationDataParams.put("executiveAddress", metaData.getExecutiveAddress());
        calibrationDataParams.put("siteExecutiveName", metaData.getSiteExecutiveName());
//        environmentDataParams.put("zeroVoltage", metaData.getZeroGroundVoltage());
        calibrationDataParams.put("executiveEnvironment", metaData.getExecutiveEnvironment());
        return calibrationDataParams;
    }

    /**
     * 文件自定义名称参数构建器
     */
    @Override
    protected void customizedSignNamesParamCreator() {
        keyParamList = Arrays.asList("审核人", "批准人");
        customizedSignNames = Arrays.asList("技术负责人审核", "计量测试站主任批准");
    }

    private void resolverApprovalTime(FilePrintRegister filePrintRegister, Map<String, List<ApprovalLogResponse>> ApprovalLogMap) {
        for (int i = 0; i < keyParamList.size(); i++) {
            if (ApprovalLogMap.containsKey(keyParamList.get(i))) {
                List<ApprovalLogResponse> approvalLogResponseList = ApprovalLogMap.get(keyParamList.get(i));
                if (CollectionUtils.isNotEmpty(approvalLogResponseList)) {
                    Optional<ApprovalLogResponse> optionalApprovalLogResponse = approvalLogResponseList.stream().findFirst();
                    if (optionalApprovalLogResponse.isPresent()) {
                        Date nodeFinishTime = optionalApprovalLogResponse.get().getFinishTime();
                        if(!ObjectUtils.isEmpty(nodeFinishTime)){
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(nodeFinishTime);
                            filePrintRegister.getParams().put("y" + i, calendar.get(Calendar.YEAR));
                            filePrintRegister.getParams().put("m" + i, calendar.get(Calendar.MONTH) + 1);
                            filePrintRegister.getParams().put("d" + i, calendar.get(Calendar.DAY_OF_MONTH));
                        }
                    }
                }
            }
        }
    }
}
