package com.lantone.qc.trans.beilun.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;

/**
 * @Description:
 * @author: HUJING
 * @time: 2020/9/9 11:24
 */
@Slf4j
public class BeiLunFirstCourseRecordHtmlAnalysis implements BeiLunHtmlAnalysis {

    private String modeId = "2";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        try {
            List<String> titles = Lists.newArrayList("（一）病历特点", "（二）诊断依据", "（三）鉴别诊断",
                    "（四）初步诊断", "（五）诊疗计划", "（二）拟诊讨论", "初步诊断", "诊断依据", "鉴别诊断", "（三）诊疗计划", "心理治疗", "（五）疼痛评估",
                    "疼痛治疗", "营养治疗", "康复治疗", "VTE预防", "医生签名", "（六）诊疗计划");
            String html = args[0];
            String recTitle = args[1];
            Document doc = Jsoup.parse(html);
            String htmlContent = BeiLunHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0), true);
            if (StringUtil.isNotBlank(htmlContent)) {
                htmlContent = htmlContent.replace("—/—", "")
                        .replace("第1页", "")
                        .replace("建议先书写大病", "")
                        .replace("历，并对导入的既往史和现病史进行归纳", "")
                        .replace("若书写者为诊疗组长本人，以下签名框中可只签一个名字", "");
                String[] lines = htmlContent.split("\n\n");
                String text = "";
                if (lines.length > 1) {
                    if (lines[0].contains("宁波市北仑区人民医院")) {
                        for (int i = 1; i < lines.length; i++) {
                            text += lines[i];
                        }
                    } else {
                        for (int i = 0; i < lines.length; i++) {
                            text += lines[i];
                        }
                    }
                    //htmlContent = lines[1];
                } else if (lines.length == 1) {
                    text = lines[0];
                }
                if (StringUtil.isNotBlank(text)) {
                    String date = CommonAnalysisUtil.extractDate(text);
                    if (StringUtil.isNotBlank(date)) {
                        structureMap.put("时间", date);
                        text = text.replace(date, "");
                    }
                    text = text.replaceAll("[   ]", " ");
                    CommonAnalysisUtil.html2StructureMap(titles, text, structureMap);
                }
            }
            if (structureMap.containsKey("（一）病历特点")) {
                CommonAnalysisUtil.cutByTitles(structureMap.get("（一）病历特点"), Lists.newArrayList("入院查体", "辅助检查"), 0, structureMap);
                if (structureMap.containsKey("入院查体")) {
                    structureMap.put("（一）病历特点", structureMap.get("（一）病历特点").split("入院查体")[0]);
                }
            }
            BeiLunHtmlAnalysisUtil.insertModuleId(modeId, args[2], structureMap);
            //        CommonAnalysisUtil.extractDateByTitle(structureMap, "时间");
            //        structureMap.forEach((k,v)-> System.out.println(k + "------" + v));
            //        structureMap.put("rec_title=", "107");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

}