package com.lantone.qc.trans.taizhou;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationInformedConsentDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.model.doc.operation.OperationSafetyChecklistDoc;
import com.lantone.qc.pub.model.doc.operation.PreoperativeDiscussionDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.taizhou.util.TzXmlUtil;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 手术文档生成
 * @author: rengb
 * @time: 2020/3/20 17:11
 */
public class TaiZhouOperationDocTrans extends ModelDocTrans {

    /**
     * 手术文档包含：术后首次病程及谈话记录；手术记录；术前讨论、术前小结
     *
     * @param medrecVo
     * @return
     */
    @Override
    public List<OperationDoc> extract(MedrecVo medrecVo) {
        List<OperationDoc> retList = Lists.newArrayList();

        Map<String, List<String>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return retList;
        }

        Map<String, OperationDiscussionDoc> operationDiscussionDocMap = getOperationDiscussionDocMap(contentMap.get("术后首次病程及谈话记录"));
        Map<String, OperationRecordDoc> operationRecordDocMap = getOperationRecordDocMap(contentMap.get("手术记录"));
        Map<String, PreoperativeDiscussionDoc> preoperativeDiscussionDocMap = getPreoperativeDiscussionDocMap(contentMap.get("术前讨论、术前小结"));
        Map<String, OperationInformedConsentDoc> operationInformedConsentDocMap = getOperationInformedConsentDocMap(contentMap.get("手术知情同意书"));
        Map<String, OperationSafetyChecklistDoc> operationSafetyChecklistDocMap = getOperationSafetyChecklistDocMap(contentMap.get("手术安全核查表"));

        Set<String> operationNameSet = Sets.newHashSet();
        operationNameSet.addAll(operationDiscussionDocMap.keySet());
        operationNameSet.addAll(operationRecordDocMap.keySet());
        operationNameSet.addAll(preoperativeDiscussionDocMap.keySet());
        operationNameSet.addAll(operationInformedConsentDocMap.keySet());
        operationNameSet.addAll(operationSafetyChecklistDocMap.keySet());

        operationNameSet.forEach(operationName -> {
            OperationDoc operationDoc = new OperationDoc();
            operationDoc.setOperationName(operationName);
            operationDoc.setOperationDiscussionDoc(operationDiscussionDocMap.get(operationName));
            operationDoc.setOperationRecordDoc(operationRecordDocMap.get(operationName));
            operationDoc.setPreoperativeDiscussionDoc(preoperativeDiscussionDocMap.get(operationName));
            operationDoc.setOperationInformedConsentDoc(operationInformedConsentDocMap.get(operationName));
            operationDoc.setOperationSafetyChecklistDoc(operationSafetyChecklistDocMap.get(operationName));
            retList.add(operationDoc);
        });
        return retList;
    }


    /*******************************************术后首次病程及谈话记录***************************************************/
    private Map<String, OperationDiscussionDoc> getOperationDiscussionDocMap(List<String> contents) {
        Map<String, OperationDiscussionDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            operationName = index + "";
            OperationDiscussionDoc operationDiscussionDoc = getOperationDiscussionDoc(content);
            operationDiscussionDoc.setOperationName(operationName);
            retMap.put(operationName, operationDiscussionDoc);
            index++;
        }
        return retMap;
    }

    private OperationDiscussionDoc getOperationDiscussionDoc(String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationDiscussion_keyContrasts);

        OperationDiscussionDoc operationDiscussionDoc = new OperationDiscussionDoc();
        operationDiscussionDoc.setStructureMap(structureMap);
        operationDiscussionDoc.setPageData((Map)sourceMap);

        return operationDiscussionDoc;
    }

    private List<String> operationDiscussion_keyContrasts = Lists.newArrayList();


    /**********************************************手术记录*************************************************************/
    private Map<String, OperationRecordDoc> getOperationRecordDocMap(List<String> contents) {
        Map<String, OperationRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            operationName = index + "";
            OperationRecordDoc operationRecordDoc = getOperationRecordDoc(content);
            operationRecordDoc.setOperationName(operationName);
            retMap.put(operationName, operationRecordDoc);
            index++;
        }
        return retMap;
    }

    private OperationRecordDoc getOperationRecordDoc(String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationRecord_keyContrasts);

        OperationRecordDoc operationRecordDoc = new OperationRecordDoc();
        operationRecordDoc.setStructureMap(structureMap);
        operationRecordDoc.setPageData((Map)sourceMap);
//        MapUtil.console(structureMap, "手术记录");
        return operationRecordDoc;
    }

    // 手术记录
    private List<String> operationRecord_keyContrasts = Lists.newArrayList(
            "手术名称=手术名称",
            "麻醉方式=麻醉方式",
            "术前诊断=术前诊断",
            "术后诊断=术后诊断",
            "手术经过=手术经过",
            "结束时间=手术结束时间",
            "开始时间=手术开始时间"
            );


    /**********************************************术前讨论、术前小结****************************************************/
    private Map<String, PreoperativeDiscussionDoc> getPreoperativeDiscussionDocMap(List<String> contents) {
        Map<String, PreoperativeDiscussionDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            operationName = index + "";
            PreoperativeDiscussionDoc preoperativeDiscussionDoc = getPreoperativeDiscussionDoc(content);
            preoperativeDiscussionDoc.setOperationName(operationName);
            retMap.put(operationName, preoperativeDiscussionDoc);
            index++;
        }
        return retMap;
    }

    private PreoperativeDiscussionDoc getPreoperativeDiscussionDoc(String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZWithReplace(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, preoperativeDiscussion_keyContrasts);

        PreoperativeDiscussionDoc preoperativeDiscussionDoc = new PreoperativeDiscussionDoc();
        preoperativeDiscussionDoc.setStructureMap(structureMap);
        preoperativeDiscussionDoc.setPageData((Map)sourceMap);
//        MapUtil.console(structureMap, "术前讨论、术前小结");
        return preoperativeDiscussionDoc;
    }

    private List<String> preoperativeDiscussion_keyContrasts = Lists.newArrayList(
            "讨论时间=讨论时间",
            "讨论方式=讨论方式",
            "参加人员及职称=参加人员",
            "简要病情=简要病情",
            "主刀医师=主刀医师签名",
            "主刀医师签字时间=主刀医师签字时间",
            "手术指征=手术指征",
            "麻醉方式=麻醉方式",
            "术前准备=术前准备内容",
            "注意事项=术前术后注意事项",
            "术中、术后防范措施=可能意外和防范措施",
            "拟施手术名称和方式=拟行术式"
    );

    /**********************************************手术知情同意书****************************************************/
    private Map<String, OperationInformedConsentDoc> getOperationInformedConsentDocMap(List<String> contents) {
        Map<String, OperationInformedConsentDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            operationName = index + "";
            OperationInformedConsentDoc operationInformedConsentDoc = getOperationInformedConsentDoc(content);
            operationInformedConsentDoc.setOperationName(operationName);
            retMap.put(operationName, operationInformedConsentDoc);
            index++;
        }
        return retMap;
    }

    private OperationInformedConsentDoc getOperationInformedConsentDoc(String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationInformedConsent_keyContrasts);

        OperationInformedConsentDoc operationInformedConsentDoc = new OperationInformedConsentDoc();
        operationInformedConsentDoc.setStructureMap(structureMap);
        operationInformedConsentDoc.setPageData((Map) sourceMap);
//        MapUtil.console(structureMap, "手术知情同意书");
        return operationInformedConsentDoc;
    }

    private List<String> operationInformedConsent_keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "性别=",
            "病区++++病区名称=病区",
            "床号=",
            "病案号++++住院号=病案号",
            "手术医生=",
            "目前诊断=",
            "手术名称=",
            "手术指征=",
            "风险及并发症=",
            "++++保守治疗=保守治疗",
            "++++其他手术=其他手术",
            "签名++++=签名",
            "签名时间=签名时间"
    );


    /**********************************************手术安全核查表****************************************************/
    private Map<String, OperationSafetyChecklistDoc> getOperationSafetyChecklistDocMap(List<String> contents) {
        Map<String, OperationSafetyChecklistDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            operationName = index + "";
            OperationSafetyChecklistDoc operationSafetyChecklistDoc = getOperationSafetyChecklistDoc(content);
            operationSafetyChecklistDoc.setOperationName(operationName);
            retMap.put(operationName, operationSafetyChecklistDoc);
            index++;
        }
        return retMap;
    }

    private OperationSafetyChecklistDoc getOperationSafetyChecklistDoc(String content) {
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content); // xml原始数据给华卓
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationSafetyChecklist_keyContrasts);

        OperationSafetyChecklistDoc operationSafetyChecklistDoc = new OperationSafetyChecklistDoc();
        operationSafetyChecklistDoc.setStructureMap(structureMap);
        operationSafetyChecklistDoc.setPageData((Map) sourceMap);

        return operationSafetyChecklistDoc;
    }

    private List<String> operationSafetyChecklist_keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "性别",
            "病区",
            "床号",
            "病案号++++住院号=病案号",
            "签名++++=签名"
    );

}