package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lantone.qc.dbanaly.facade.changx.CxXmlUtil;
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.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.comsis.Preproc;

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

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

    @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((List) contentMap.get("术后首次病程及谈话记录"));
        Map<String, OperationRecordDoc> operationRecordDocMap = getOperationRecordDocMap((List) contentMap.get("手术记录"));
        Map<String, PreoperativeDiscussionDoc> preoperativeDiscussionDocMap = getPreoperativeDiscussionDocMap((List) contentMap.get("术前讨论、术前小结"));
        Map<String, OperationInformedConsentDoc> operationInformedConsentDocMap = getOperationInformedConsentDocMap((List) contentMap.get("手术知情同意书"));
        Map<String, OperationSafetyChecklistDoc> operationSafetyChecklistDocMap = getOperationSafetyChecklistDocMap((List) 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<Map<String, Object>> contentMaps) {
        Map<String, OperationDiscussionDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            OperationDiscussionDoc operationDiscussionDoc = getOperationDiscussionDoc(contentMap);
            operationDiscussionDoc.setOperationName(operationName);
            retMap.put(operationName, operationDiscussionDoc);
            index++;
        }
        return retMap;
    }

    private OperationDiscussionDoc getOperationDiscussionDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, operationDiscussion_keyContrasts);
        if (StringUtil.isNotBlank(structureMap.get("手术开始时间"))) {
            structureMap.put("手术时间", structureMap.get("手术开始时间"));
        }
        if (StringUtil.isNotBlank(structureMap.get("签名"))) {
            structureMap.put("记录医师", structureMap.get("签名"));
        }

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, operationDiscussion_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        OperationDiscussionDoc operationDiscussionDoc = ModelDocGenerate.operationDiscussionDocGen(cutWordMap);
        operationDiscussionDoc.setText(text);
        operationDiscussionDoc.setPageData((Map) cutWordMap);

        return operationDiscussionDoc;
    }

    private List<String> operationDiscussion_sourceTitles = Lists.newArrayList(
            "记录医师",
            "记录时间",
            "手术时间",
            "麻醉方式",
            "手术方式",
            "术中诊断",
            "手术简要经过",
            "患者术后情况",
            "术后处理措施",
            "术后可能出现的并发症及预防措施",
            "术后注意事项",
            "谈话医师签字",
            "患者签名",
            "患者签名时间",
            "被授权人/代理人签名",
            "代理人签名时间"
    );

    private List<String> operationDiscussion_keyContrasts = Lists.newArrayList(
            "记录日期=记录时间",
            "手术开始时间=",
            "手术结束时间=",
            "麻醉方式=",
            "麻醉医生=麻醉医师",
            "手术方式=",
            "手术人员=手术相关人员",
            "手术方式++++手术名称=手术方式",
            "术中诊断++++初步诊断=术中诊断",
            "术中诊断++++术后诊断=术中诊断",
            "并发症++++手术并发症=术中并发症",
            "手术经过=手术简要经过",
            "患者术后情况=",
            "术后处理措施=",
            "术后并发症及预防措施=术后可能出现的并发症及预防措施",
            "术后注意事项=",
            "患者签名=",
            "签名时间=代理人签名时间",
            "签字++++患者授权的其他代理人签字=代理人签名",
            "签名++++=医生签名"
    );


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

    private OperationRecordDoc getOperationRecordDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, operationRecord_keyContrasts);
        if (StringUtil.isNotBlank(structureMap.get("签名"))) {
            structureMap.put("记录医师", structureMap.get("签名"));
        }

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, operationRecord_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        OperationRecordDoc operationRecordDoc = ModelDocGenerate.operationRecordDocGen(cutWordMap);
        operationRecordDoc.setText(text);
        operationRecordDoc.setPageData((Map) cutWordMap);

        return operationRecordDoc;
    }

    private List<String> operationRecord_sourceTitles = Lists.newArrayList(
            "记录医师",
            "记录时间",
            "手术名称",
            "麻醉方式",
            "术前诊断",
            "术后诊断",
            "手术经过"
    );

    private List<String> operationRecord_keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=",
            "性别=",
            "病区++++病区名称=",
            "床号=",
            "病案号++++住院号=",
            "手术开始时间=",
            "手术结束时间=",
            "术前诊断=",
            "手术名称=",
            "术后诊断=",
            "主刀医师=",
            "一助=",
            "二助=",
            "器械护士=",
            "麻醉方式=",
            "麻醉医生=",
            "清洁程度=",
            "ASA分级++++麻醉ASA分级=",
            "手术持续时间=",
            "风险评估分值=",
            "++++浅层组织手术=",
            "++++深层组织手术=",
            "++++器官手术=",
            "++++空隙手术=",
            //            "++++术中送检无=术中送检",
            //            "++++术中送检有=术中送检",
            "冰冻切片结果=",
            "++++并发症无=手术并发症",
            "++++并发症有=手术并发症",
            "手术并发症=",
            "失血量=",
            "手术经过=",
            "记录时间=",
            "记录时间=",
            "术（中）后诊断++++术后中诊断=术后诊断"
    );


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

    private PreoperativeDiscussionDoc getPreoperativeDiscussionDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, preoperativeDiscussion_keyContrasts);
        structureMap.put(
                "术前准备内容",
                (StringUtil.isBlank(structureMap.get("术前准备内容")) ? "" : structureMap.get("术前准备内容")) + (StringUtil.isBlank(structureMap.get("术前准备++++术前准备情况")) ? "" : structureMap.get("术前准备++++术前准备情况"))
        );
        structureMap.put(
                "术前术后注意事项",
                (StringUtil.isBlank(structureMap.get("术前术后注意事项")) ? "" : structureMap.get("术前术后注意事项")) + (StringUtil.isBlank(structureMap.get("术后注意事项")) ? "" : structureMap.get("术后注意事项"))
        );
        structureMap.put("讨论时间", structureMap.get("记录时间"));
        if (StringUtil.isNotBlank(structureMap.get("签名"))) {
            structureMap.put("记录医师", structureMap.get("签名"));
        }
        if (StringUtil.isNotBlank(structureMap.get("拟行治疗指征及禁忌症"))) {
            structureMap.put("拟行术式", structureMap.get("拟行治疗指征及禁忌症"));
        } else if (StringUtil.isNotBlank(structureMap.get("拟施手术方式"))) {
            structureMap.put("拟行术式", structureMap.get("拟施手术方式"));
        }


        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, preoperativeDiscussion_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        PreoperativeDiscussionDoc preoperativeDiscussionDoc = ModelDocGenerate.preoperativeDiscussionDocGen(cutWordMap);
        preoperativeDiscussionDoc.setText(text);
        preoperativeDiscussionDoc.setPageData((Map) cutWordMap);

        return preoperativeDiscussionDoc;
    }

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

    private List<String> preoperativeDiscussion_keyContrasts = Lists.newArrayList(
            "讨论时间++++记录日期=记录时间",
            "签字时间=主刀医师签字时间",
            "参加人员姓名=参加人员",
            "病情摘要++++简要病情=简要病情",
            "术前诊断++++初步诊断=术前诊断",
            "手术指征与禁忌征++++手术指征=手术指征",
            "拟行治疗指征及禁忌症=",
            "拟施麻醉方式=麻醉方式",
            "术前准备情况=术前准备内容",
            "术前准备++++术前准备情况=",
            "手术风险及预防措施++++文本框=可能意外和防范措施",
            "术中注意事项=术前术后注意事项",
            "术后注意事项=",
            "目前使用的药物=目前服用药物",
            "诊疗计划=治疗计划和措施",
            "注意事项=术前术后注意事项",
            "手术名称及方式=拟施手术方式名称及可能的变更与禁忌症",
            "拟施手术方式、名称及可能的变更与禁忌征=拟施手术方式名称及可能的变更与禁忌症"
    );


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

    private OperationInformedConsentDoc getOperationInformedConsentDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, operationInformedConsent_keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, operationInformedConsent_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        OperationInformedConsentDoc operationInformedConsentDoc = ModelDocGenerate.operationInformedConsentDocGen(cutWordMap);
        operationInformedConsentDoc.setText(text);
        operationInformedConsentDoc.setPageData((Map) cutWordMap);

        return operationInformedConsentDoc;
    }

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

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


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

    private OperationSafetyChecklistDoc getOperationSafetyChecklistDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, operationSafetyChecklist_keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, operationSafetyChecklist_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        OperationSafetyChecklistDoc operationSafetyChecklistDoc = ModelDocGenerate.operationSafetyChecklistDocGen(cutWordMap);
        operationSafetyChecklistDoc.setText(text);
        operationSafetyChecklistDoc.setPageData((Map) cutWordMap);

        return operationSafetyChecklistDoc;
    }

    private List<String> operationSafetyChecklist_sourceTitles = Lists.newArrayList(
            "姓名",
            "性别",
            "病区",
            "床号",
            "病案号",
            "签名"
    );

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

}