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

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.service.listeners.register.data.PrintFileDataGeneric;
import com.casic.missiles.model.business.BusinessCertificateReport;
import com.casic.missiles.service.listeners.register.data.PrintFileDataSupport;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * 校准证书参数生成
 */
@Component(PrintFileDataBeanNameAliasEnum.CALIBRATION_CERTIFICATE_BEAN)
public class CalibrationPrintFileData extends PrintFileDataSupport implements PrintFileDataGeneric {

    /**
     * 1、进行打印证书对象的初始化
     * 2、获取定制化的签名
     * 3、获取定制化的参数
     *
     * @param object          证书报告信息
     * @param templateFileDir 本地进行打印报告组合的路径
     * @return
     */
    @Override
    public FilePrintRegister CustomizedPrintFileRegister(Object object, String templateFileDir) {
        BusinessCertificateReport metaData = (BusinessCertificateReport) object;
        //初始化，生成模板文件
        FilePrintRegister filePrintRegister = initialization(templateFileDir, metaData.getId(), getTemplateName(metaData.getTemplateId()));
        //查询工作流审批记录
        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);
        electronicSignList.addAll(customizedElectronicSignSeals(metaData, filePrintRegister));
        filePrintRegister.setElectronicImageList(electronicSignList);
        //填充定制化的参数
        filePrintRegister.setParams(certificateFileParamCreator(metaData.getOrderId(), metaData.getSampleId()));
        return filePrintRegister;
    }


    @Override
    protected void customizedSignNamesParamCreator() {
        keyParamList = Arrays.asList("发起人", "核验人", "审核人" );
        customizedSignNames = Arrays.asList("校准人", "核验人", "批准人" );
    }

    /**
     * 定制化电子签章文件
     *
     * @param metaData
     * @return
     */
    private List<ElectronicImage> customizedElectronicSignSeals(BusinessCertificateReport metaData, FilePrintRegister filePrintRegister) {
        List<ElectronicImage> electronicImageSeals = new ArrayList<>();
        ElectronicImage electronicImage = defaultElectronicSignSeal("发证单位");
        electronicImageSeals.add(doPopulateElectronicSignSeal(metaData.getSignId(), electronicImage, filePrintRegister));
        if (StringUtils.isNotEmpty(metaData.getApprovalSignIds())) {
            String[] approvalSignIds = metaData.getApprovalSignIds().split(",");
            List<ElectronicImage> electronicImages = autoCreteAbsoluteSignSealByNumber(approvalSignIds.length);
            for (int i = 0; i < approvalSignIds.length; i++) {
                electronicImageSeals.add(doPopulateElectronicSignSeal(Long.valueOf(approvalSignIds[i]), electronicImages.get(i), filePrintRegister));
            }
        }
        return electronicImageSeals;
    }

}
