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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/9/12 13:53
 */
@Slf4j
public class NingHaiYiYiConsultationHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "31";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();

        try {
            List<String> titles = Lists.newArrayList("姓名", "性别", "出生日期", "科别", "病区", "床号", "住院号",
                    "会诊类型", "被邀医院", "被邀科室", "被邀医师", "申请时间", "患者病情及诊疗经过、申请会诊的理由及目的", "申请会诊科别",
                    "被邀会诊科别", "申请会诊医师", "会诊意见", "会诊时间", "科主任", "会诊到达时间", "查体", "会诊建议",
                    "会诊诊断", "由受邀医生填写，本次会诊是否必要","会诊科室", "会诊医师", "外院会诊医师所在医疗机构名称", "会诊医师所在医疗机构名称");
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            String htmlContent = null;
            Document doc = Jsoup.parse(html);
            if ("外院专家会诊申请单".equals(recTitle)) {
                htmlContent = CommonAnalysisUtil.html2String(html);
                if (StringUtil.isNotBlank(htmlContent)) {
                    htmlContent = htmlContent.replaceAll("[   ]", " ").replace("第1页", "");
                    CommonAnalysisUtil.html2StructureMap(titles, htmlContent, structureMap);
                }
            } else if ("协定方会诊".equals(recTitle)) {
                htmlContent = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0).getElementById("main"), true);
                if (StringUtil.isNotBlank(htmlContent)) {
                    htmlContent = htmlContent.replaceAll("[   ]", " ").replace("第1页", "");
                    CommonAnalysisUtil.html2StructureMap(titles, htmlContent, structureMap);
                }
                if (StringUtil.isNotBlank(structureMap.get("会诊时间"))) {
                    String[] strArr = structureMap.get("会诊时间").split(" ");
                    structureMap.put("会诊意见", strArr[strArr.length - 1]);
                    structureMap.put("会诊时间", strArr[0]);
                }
                CommonAnalysisUtil.extractDateByTitle(structureMap, "申请时间");
            } else if ("MDT会诊申请表".equals(recTitle) || "新-MDT会诊三合一".equals(recTitle)) {
                titles = Lists.newArrayList("姓名", "性别", "出生日期", "联系电话",
                        "申请科室", "入院/首诊时间", "住院号", "病情概述（含主诉、病史、诊断、诊治过程等）", "拟申请MDT时间、地点",
                        "拟请MDT参加科室", "MDT目的", "申请人签名", "申请递交时间", "科主任签字", "专家诊治建议", "专家科室", "签名",
                        "填写时间", "主持科室小结（MDT的最终诊治决议）", "科主任（主持人）签名", "记录人（主管医师）签字", "日期");
                htmlContent = CommonAnalysisUtil.html2String(html);
                if (StringUtil.isNotBlank(htmlContent)) {
                    htmlContent = htmlContent.replaceAll("[   ]", " ").replace("住院/门诊号", "").replace("第1页", "");
                    StringBuffer sbf = new StringBuffer();
                    List<String> distinctText = Lists.newArrayList(htmlContent.split(" ")).stream().distinct().collect(Collectors.toList());
                    for (String text : distinctText) {
                        sbf.append(text).append(" ");
                    }
                    CommonAnalysisUtil.html2StructureMapNoColon(titles, sbf.toString(), structureMap);
                }
                if (StringUtil.isNotBlank(structureMap.get("主持科室小结（MDT的最终诊治决议）"))) {
                    String[] valueArray = structureMap.get("主持科室小结（MDT的最终诊治决议）").split(" ");
                    structureMap.put("主持科室小结（MDT的最终诊治决议）", valueArray[1]);
                    String v = valueArray[2];
                    String[] vArr = v.split("：");
                    if (vArr.length > 1) {
                        structureMap.put(vArr[0], vArr[1]);
                    }
                }
            } else if ("（外地专家）会诊邀请函".equals(recTitle)) {
                titles = Lists.newArrayList("姓名", "出生年月", "病区", "床位", "住院号", "病情摘要", "会诊日期", "被邀医师", "专业",
                        "职称", "单位", "会诊费用", "会诊科室", "医师签名", "医生签名", "科主任 签名", "患方意见", "签字", "外来专家 意见", "签字",
                        "邀请医院（钉钉审批后盖章）");
                htmlContent = CommonAnalysisUtil.html2String(html);
                if (StringUtil.isNotBlank(htmlContent)) {
                    htmlContent = htmlContent.replaceAll("[   ]", " ").replace("第1页", "").replace("年   月   日   时   分", "");
                    CommonAnalysisUtil.html2StructureMapNoColon(titles, htmlContent, structureMap);
                }
                if (structureMap.containsKey("外来专家意见")) {
                    structureMap.put("外来专家意见", structureMap.get("外来专家意见").replaceAll("签字：", ""));
                }
                if (structureMap.containsKey("被邀医师")) {
                    structureMap.put("会诊医师", structureMap.get("被邀医师"));
                }

            } else {
                htmlContent = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0).getElementById("main"), true);
                //htmlContent = CommonAnalysisUtil.html2String(html);
                if (StringUtil.isNotBlank(htmlContent)) {
                    htmlContent = htmlContent.replaceAll("[   ]", " ").replace("第1页", "");
                    CommonAnalysisUtil.html2StructureMap(titles, htmlContent, structureMap);
                }
            }
            if (structureMap.containsKey("会诊到达时间")) {
                String arriveDateStr = CommonAnalysisUtil.extractDate(structureMap.get("会诊到达时间"));
                if (StringUtil.isNotBlank(arriveDateStr)) {
                    structureMap.put("会诊意见", structureMap.get("会诊到达时间").replace(arriveDateStr, "").trim());
                    structureMap.put("会诊到达时间", arriveDateStr);
                }
            }
            CommonAnalysisUtil.processType(structureMap, "会诊类型");
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

}