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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

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

/**
 * @Description: 自定义病程记录html解析
 * @author: HUJING
 * @time: 2020/9/15 10:28
 */
@Slf4j
public class NingHaiYiYiThreeLevelWardHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "4";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        try {
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            Document doc = Jsoup.parse(html);
            Element bigDivElement = doc.selectFirst("body").child(0).getElementById("main");
            if (bigDivElement.childNodeSize() == 1) {
                bigDivElement = bigDivElement.child(0);
            }
            if (bigDivElement.selectFirst("hr") != null) {
                bigDivElement.selectFirst("hr").previousElementSiblings().remove();
            }
            String htmlText = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(bigDivElement, true);
            htmlText = htmlText.replace("注意：上级医师查房主要记 录：患者病情、诊断、鉴别诊断、当前治疗措施和疗效的分析及下一步诊疗意见等，能反应上级医 师的水平。", "")
                    .replace("提醒：有创诊疗操作记录内容包括操作名称、时间、步骤、结果及患者的一般情况，记录操作过 程是否顺利，有无不良反应，术后注意事项，操作医师签名、记录时间等。 手术室完成的、治疗性质的及全麻下完成的有创诊疗操作参照手术管理。（包括介入治疗、胃肠镜 下肿瘤切除/粘膜下肿瘤剥除等）", "");
            CommonAnalysisUtil.NHExtractWardInfo(recTitle, htmlText, structureMap);
            String bqjlText = "病情记录" + structureMap.get("病情记录");
            List<String> titles = Lists.newArrayList(
                    "病情记录","医生签名","医师签名"
            );
            titles = CommonAnalysisUtil.sortTitlesNoColon(titles, bqjlText);
            CommonAnalysisUtil.cutByTitlesNoColon(bqjlText, titles, 0, structureMap);
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

    /**
     * 将html内容以行为单位存进list,从<hr>之后开始处理
     *
     * @param html 原始html内容
     * @return
     */
    public static List<String> html2List(String html, boolean existHr) {
        List<String> htmlText = Lists.newArrayList();
        Document document = Jsoup.parse(html);
        Element body = document.select("body").first();
        List<Node> nodes = body.childNodes();
        List<Node> subNodes = nodes.get(0).childNodes();
        boolean findNode = false;
        for (Node node : subNodes) {
            if ("hr".equals(node.nodeName())) {
                findNode = true;
                continue;
            }
            if (findNode || !existHr) {
                String title = node.attr("title");
                if ("main".equals(title)) {
                    Element element = (Element) node;
                    Elements elements = element.select("div");
                    for (Element e : elements) {
                        String text = e.text();
                        if (text.length() > 150) {
                            continue;
                        }
                        htmlText.add(text);
                    }
                }
            }
        }
        if (htmlText.get(0).length() > 200) {
            htmlText.remove(0);
        }
        return htmlText;
    }

}