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.Element;

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

/**
 * @Description:
 * @author: rengb
 * @time: 2020/9/28 16:36
 */
@Slf4j
public class NingHaiYiYiOperationRecordHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "17";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> map = Maps.newLinkedHashMap();
        try {
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            NingHaiYiYiHtmlAnalysisUtil.tableStyle1InsertMap(Jsoup.parse(html).selectFirst("body").child(0).getElementById("header"), map);
            map.forEach((key, value) -> map.put(key, value.replace("\n", "")));
            Element bigDivElement = Jsoup.parse(html).selectFirst("body").child(0).getElementById("main");
            analysisGeneral(bigDivElement, map);
            NingHaiYiYiHtmlAnalysisUtil.mapValueRemoveStartColon(map);
            String operatingCrew = null;
            if (map.containsKey("手术人员")) {
                operatingCrew = map.get("手术人员");
            } else if (map.containsKey("手术者及助手名称")) {
                operatingCrew = map.get("手术者及助手名称");
            }
            if (StringUtil.isNotBlank(operatingCrew)) {
                CommonAnalysisUtil.cutByTitles(operatingCrew, Lists.newArrayList("主刀", "一助", "二助", "助手", "洗手护士"), 0, map);
            }
            if (map.containsKey("主刀")) {
                map.put("主刀", map.get("主刀").replaceAll("[^\u4E00-\u9FA5]", ""));
            } else {
                if (StringUtil.isNotBlank(operatingCrew) && operatingCrew.contains("、")) {
                    map.put("主刀", operatingCrew.split("、")[0]);
                    map.put("一助", operatingCrew.split("、")[1]);
                }
            }
            if (map.containsKey("麻醉者")) {
                String anesthetist = map.get("麻醉者").replace("麻醉医生：", "");
                map.put("麻醉医师", anesthetist);
                if (anesthetist.contains("1.")) {
                    int index = anesthetist.indexOf("1.");
                    map.put("麻醉医师", anesthetist.substring(0, index));
                    map.put("手术经过", anesthetist.substring(index));
                }
            }
            if (map.containsKey("手术日期")) {
                String operatorDateStr = map.get("手术日期");
                if (operatorDateStr.contains("--")) {
                    String[] operatorDate = operatorDateStr.split("--");
                    map.put("手术开始时间", operatorDate[0]);
                    map.put("手术结束时间", operatorDate[1]);
                }
            }
            //宁海产科手术记录处理
            if (map.containsKey("开始") && map.containsKey("结束")
                    && !map.containsKey("手术开始时间") && !map.containsKey("手术结束时间")
                    && !map.containsKey("手术日期")) {
                map.put("手术开始时间", map.get("开始"));
                String endTime = map.get("结束");
                if (endTime.contains("手术名称")) {
                    CommonAnalysisUtil.html2StructureMapNoColon
                            (Lists.newArrayList("手术名称", "术前诊断", "手术指征", "术后诊断"), endTime, map);
                    endTime = endTime.split("手术名称")[0].trim();
                }
                map.put("手术结束时间", endTime);
                map.put("手术日期", "开始:" + map.get("手术开始时间") + "  结束:" + map.get("手术结束时间"));

            }

            if (map.containsKey("手术开始时间") && map.containsKey("手术结束时间")) {
                map.put("手术日期", "开始:" + map.get("手术开始时间") + "  结束:" + map.get("手术结束时间"));
            }

            CommonAnalysisUtil.makeEmpty(map, "医生签名");
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return map;
    }

    private void analysisGeneral(Element bigDivElement, Map<String, String> map) {
        /*if (bigDivElement.selectFirst("hr") != null) {
            bigDivElement.selectFirst("hr").previousElementSiblings().remove();
        }*/
        String text = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(bigDivElement, true);
        text = text.replaceAll("第1页", "");
        List<String> titles = Lists.newArrayList(
                "姓名", "性别", "出生日期", "出生年月", "科别", "科室", "病区", "床号", "床位", "住院号", "手术开始时间", "手术结束时间",
                "手术时间", "手术日期", "术前诊断", "手术指征", "手术人员", "外邀指导专家", "麻醉方式", "麻醉人员", "麻 醉 者", "洗手护士", "本次手术是否属于非计划再次手术", "接生者",
                "术前胎心", "手术名称", "术后诊断", "手术者及助手名称", "术中取病理标本", "麻醉方法", "手术经过（包括病人体位、切口处理、病灶所见及手术步骤等）",
                "诊断手术/操作简要经过（包括术中有无并发症及具体描述和处理）", "手术经过", "医师签名",
                "医生签名", "手术/操作医生", "记录者", "记录时间", "术中输血", "术前宫颈准备日期", "药物名称及用法", "导管或其他", "插入深度",
                "阴道填塞纱布条", "准备者", "取出日期", "手术方法", "术中特殊情况", "病理检查", "其他", "手术者",
                "检查子宫位置", "开始", "结束"
        );
        titles = CommonAnalysisUtil.sortTitles(titles, text);
        CommonAnalysisUtil.cutByTitlesNoColon(text, titles, 0, map);

    }

}