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.pub.model.doc.consultation.ConsultationApplicationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationRecordDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.changx.util.CxXmlUtil;
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/17 13:22
 */
public class ChangxConsultationDocTrans extends ModelDocTrans {

    @Override
    public List<ConsultationDoc> extract(MedrecVo medrecVo) {
        List<ConsultationDoc> retList = Lists.newArrayList();

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

        Map<String, ConsultationRecordDoc> consultationRecordDocMap = getConsultationRecordDocMap(contentMap.get("会诊记录"));
        Map<String, ConsultationResultsDoc> consultationResultsDocMap = getConsultationResultsDocMap(contentMap.get("会诊结果单"));
        Map<String, ConsultationApplicationDoc> consultationApplicationDocMap = getConsultationApplicationDocMap(contentMap.get("会诊申请单"));

        Set<String> consultationNameSet = Sets.newHashSet();
        consultationNameSet.addAll(consultationRecordDocMap.keySet());
        consultationNameSet.addAll(consultationResultsDocMap.keySet());
        consultationNameSet.addAll(consultationApplicationDocMap.keySet());

        consultationNameSet.forEach(consultationName -> {
            ConsultationDoc consultationDoc = new ConsultationDoc();
            consultationDoc.setConsultationName(consultationName);
            consultationDoc.setConsultationRecordDoc(consultationRecordDocMap.get(consultationName));
            consultationDoc.setConsultationResultsDoc(consultationResultsDocMap.get(consultationName));
            consultationDoc.setConsultationApplicationDoc(consultationApplicationDocMap.get(consultationName));
            retList.add(consultationDoc);
        });

        return retList;
    }


    /**************************************************会诊记录*********************************************************/
    private Map<String, ConsultationRecordDoc> getConsultationRecordDocMap(List<String> contents) {
        Map<String, ConsultationRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            consultationName = index + "";
            ConsultationRecordDoc consultationRecordDoc = getConsultationRecordDoc(content);
            consultationRecordDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationRecordDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationRecordDoc getConsultationRecordDoc(String content) {
        String text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Region/Content_Text");
        if (StringUtil.isBlank(text)) {
            text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Region");
        }
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, consultationRecord_sourceTitles, text);
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
        cutWordMap.putAll(xmlNodeValueMap);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(cutWordMap, consultationRecord_keyContrasts);
        MapUtil.keyAssig(structureMap, "记录医师", "签名");

        ConsultationRecordDoc consultationRecordDoc = ModelDocGenerate.consultationRecordDocGen(structureMap);
        consultationRecordDoc.setText(text);
        consultationRecordDoc.setPageData((Map) xmlNodeValueMap);

        return consultationRecordDoc;
    }

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

    private List<String> consultationRecord_keyContrasts = Lists.newArrayList(
            "执行情况=会诊情况",
            "记录日期=记录时间",
            "签名++++=签名"
    );


    /**************************************************会诊结果单*******************************************************/
    private Map<String, ConsultationResultsDoc> getConsultationResultsDocMap(List<String> contents) {
        Map<String, ConsultationResultsDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            consultationName = index + "";
            ConsultationResultsDoc consultationResultsDoc = getConsultationResultsDoc(content);
            consultationResultsDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationResultsDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationResultsDoc getConsultationResultsDoc(String content) {
        Map<String, String> pageData = Preproc.extract_doc_pub(true, consultationResults_pageDataTitles, content);
        pageData.put("病程记录内容",
                pageData.get("病程记录内容")
                        .replace(pageData.get("病程记录时间"), "")
                        .replace(pageData.get("病程记录名称"), "")
        );

        List<String> targetTitles = Lists.newArrayList();
        consultationResults_sourceTitles.forEach(sourceTitle -> {
            String targetTitle = "";
            for (int index = 0; index < sourceTitle.length(); index++) {
                if (index == sourceTitle.length() - 1) {
                    targetTitle += sourceTitle.substring(index, index + 1);
                } else {
                    targetTitle += sourceTitle.substring(index, index + 1) + "[\\s\\p{Zs}]*";
                }
            }
            targetTitles.add(targetTitle);
        });

        Map<String, String> sourceMap = Preproc.extract_doc_pub(true, targetTitles, content);
        sourceMap.put("记录时间", sourceMap.get("病程记录时间"));

        ConsultationResultsDoc consultationResultsDoc = ModelDocGenerate.consultationResultsDocGen(sourceMap);
        consultationResultsDoc.setText(content);
        consultationResultsDoc.setPageData((Map) pageData);

        return consultationResultsDoc;
    }

    private List<String> consultationResults_pageDataTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录医师"
    );
    private List<String> consultationResults_sourceTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录时间",
            "记录医师"
    );


    /**************************************************会诊申请单*******************************************************/
    private Map<String, ConsultationApplicationDoc> getConsultationApplicationDocMap(List<String> contents) {
        Map<String, ConsultationApplicationDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contents)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (String content : contents) {
            if (StringUtil.isBlank(content)) {
                continue;
            }
            consultationName = index + "";
            ConsultationApplicationDoc consultationApplicationDoc = getConsultationApplicationDoc(content);
            consultationApplicationDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationApplicationDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationApplicationDoc getConsultationApplicationDoc(String content) {
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent", content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, consultationApplication_keyContrasts);

        String text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Content_Text");
        if (StringUtil.isBlank(text)) {
            text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent");
        }
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, consultationApplication_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        ConsultationApplicationDoc consultationApplicationDoc = ModelDocGenerate.consultationApplicationDocGen(cutWordMap);
        consultationApplicationDoc.setText(text);
        consultationApplicationDoc.setPageData((Map) xmlNodeValueMap);

        return consultationApplicationDoc;
    }

    private List<String> consultationApplication_sourceTitles = Lists.newArrayList(
            "姓名",
            "性别",
            "病区",
            "床号",
            "病案号",
            "简要病情及诊疗",
            "申请理由目的",
            "受邀科室",
            "会诊分类",
            "会诊时间",
            "会诊地点",
            "签名"
    );

    private List<String> consultationApplication_keyContrasts = Lists.newArrayList(
            "++++姓名=姓名",
            "性别=性别",
            "++++病区=病区",
            "床号=",
            "++++病案号=病案号",
            "新会诊-简要病情及诊疗=简要病情及诊疗",
            "新会诊-申请理由目的=申请理由目的",
            "新会诊-受邀科室=受邀科室",
            "新会诊-会诊分类=会诊分类",
            "新会诊-会诊时间=会诊时间",
            "新会诊-会诊地点=会诊地点",
            "签名++++=签名"
    );

}