package com.lantone.qc.trans.ywzxyy.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.facade.ywzxyy.YwzxyyXmlUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @Description:
 * @author: rengb
 * @time: 2021/1/25 14:04
 */
@Slf4j
public class YwzxyyPreoperativeDiscussionXmlAnalysis implements YwzxyyXmlAnalysis {

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> ret = null;
        String xml = args[0];
        String recTitle = args[1];
        String recTypeId = args[2];
        if (recTitle.equals("术前小结")) {
            ret = sqxj(xml);
        } else if (recTitle.equals("术前讨论记录")) {
            ret = sqtl(xml);
        } else if (recTitle.equals("术前讨论兼术前小结")) {
            ret = xjtl(xml);
        }
        return ret;
    }

    private Map<String, String> sqxj(String xml) {
        Map<String, String> level1StructureMap = Maps.newHashMap();
        try {
            String level1Text = YwzxyyXmlUtil.bcXmlAna(xml);
            List<String> level1Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病历记录时间", "病历记录标题", "原始文本", "医师签名", "记录医师"), level1Text);
            CommonAnalysisUtil.cutByTitles(level1Text, level1Titles, 0, level1StructureMap);

            Map<String, String> level2StructureMap = Maps.newHashMap();
            String level2Text = level1StructureMap.get("原始文本");
            List<String> level2Titles = CommonAnalysisUtil.sortTitles(
                    Lists.newArrayList("简要病情", "术前诊断", "手术指征", "拟施手术名称和方式", "拟施麻醉方式",
                            "其他术前相关情况", "注意事项", "记录时间"),
                    level2Text
            );
            CommonAnalysisUtil.cutByTitles(level2Text, level2Titles, 0, level2StructureMap);
            level1StructureMap.putAll(level2StructureMap);

            MapUtil.keyAssig(level1StructureMap, "记录时间", "病历记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书时间", "病历记录时间", "记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书标题", "病历记录标题");
            MapUtil.keyAssig(level1StructureMap, "病历内容", "原始文本");
            MapUtil.keyAssig(level1StructureMap, "记录医生", "医师签名", "记录医师");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return level1StructureMap;
    }

    private Map<String, String> sqtl(String xml) {
        Map<String, String> level1StructureMap = Maps.newHashMap();
        try {
            String level1Text = YwzxyyXmlUtil.bcXmlAna(xml);
            List<String> level1Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病历记录时间", "病历记录标题", "原始文本", "医师签名", "记录医师"), level1Text);
            CommonAnalysisUtil.cutByTitles(level1Text, level1Titles, 0, level1StructureMap);

            Map<String, String> level2StructureMap = Maps.newHashMap();
            String level2Text = level1StructureMap.get("原始文本");
            List<String> level2Titles = CommonAnalysisUtil.sortTitlesNoColon(
                    Lists.newArrayList("参与讨论者的姓名及专业技术职务", "讨论时间", "术前准备情况", "手术指征",
                            "手术方案", "可能出现的意外及防范措施", "讨论意见", "主持人小结"),
                    level2Text
            );
            CommonAnalysisUtil.cutByTitlesNoColon(level2Text, level2Titles, 0, level2StructureMap);
            level1StructureMap.putAll(level2StructureMap);

            MapUtil.keyAssig(level1StructureMap, "记录时间", "病历记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书时间", "病历记录时间", "记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书标题", "病历记录标题");
            MapUtil.keyAssig(level1StructureMap, "病历内容", "原始文本");
            MapUtil.keyAssig(level1StructureMap, "记录医生", "医师签名", "记录医师");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return level1StructureMap;
    }

    private Map<String, String> xjtl(String xml) {
        Map<String, String> level1StructureMap = Maps.newHashMap();
        try {
            xml = xml.replaceFirst("</病历记录时间>", "</病历记录时间><病历记录标题>")
                    .replaceFirst("<讨论时间>", "</病历记录标题>讨论时间<讨论时间>")
                    .replaceFirst("</讨论时间>", "</讨论时间>讨论地点");
            String level1Text = YwzxyyXmlUtil.bcXmlAna(xml);
            List<String> level1Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病历记录时间", "病历记录标题", "原始文本"), level1Text);
            CommonAnalysisUtil.cutByTitles(level1Text, level1Titles, 0, level1StructureMap);

            Map<String, String> level2StructureMap = Maps.newHashMap();
            String level2Text = level1StructureMap.get("原始文本");
            List<String> level2Titles = CommonAnalysisUtil.sortTitlesNoColon(
                    Lists.newArrayList("讨论时间", "讨论地点", "主持人", "经治医师", "参加人员", "讨论方式", "讨论记录", "1.简要病情",
                            "2.术前诊断", "3.手术指征与禁忌症", "4.拟施手术名称和方式及可能的变更", "5.可替代的方案", "6.计划性多次手术",
                            "7.拟施麻醉方式", "8.术前准备", "9.心理状态", "10.经济评估", "11.主要麻醉、术中、术后风险及防范措施",
                            "12.术中、术后注意事项", "记录医师", "主持人"),
                    level2Text
            );
            CommonAnalysisUtil.cutByTitlesNoColon(level2Text, level2Titles, 0, level2StructureMap);
            level2StructureMap = removeSerialNumber(level2StructureMap);
            level1StructureMap.putAll(level2StructureMap);

            MapUtil.keyAssig(level1StructureMap, "记录时间", "病历记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书时间", "病历记录时间", "记录时间");
            MapUtil.keyAssig(level1StructureMap, "文书标题", "病历记录标题");
            MapUtil.keyAssig(level1StructureMap, "病历内容", "原始文本");
            MapUtil.keyAssig(level1StructureMap, "记录医生", "医师签名", "记录医师");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return level1StructureMap;
    }

    private Map<String, String> removeSerialNumber(Map<String, String> sourceMap) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        List<String> titles = Lists.newArrayList("1.简要病情", "2.术前诊断", "3.手术指征与禁忌症",
                "4.拟施手术名称和方式及可能的变更", "5.可替代的方案", "6.计划性多次手术", "7.拟施麻醉方式",
                "8.术前准备", "9.心理状态", "10.经济评估", "11.主要麻醉、术中、术后风险及防范措施", "12.术中、术后注意事项");
        sourceMap.forEach((key, value) -> {
            if (titles.contains(key)) {
                structureMap.put(key.replaceFirst("[0-9]+.", ""), value);
            } else {
                structureMap.put(key, value);
            }
        });
        return structureMap;
    }

}