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

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

/**
 * @Description:
 * @author: rengb
 * @time: 2020/9/28 20:16
 */
@Slf4j
public class BeiLunStagesSummaryHtmlAnalysis implements BeiLunHtmlAnalysis {

    private String modeId = "28";

    @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];
            Element bigDivElement = Jsoup.parse(html).selectFirst("body").child(0);
            analysisGeneral(recTitle, bigDivElement, map);
            BeiLunHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return map;
    }

    private void analysisGeneral(String recTitle, Element bigDivElement, Map<String, String> map) {
        if (bigDivElement.childNodeSize() == 1) {
            bigDivElement = bigDivElement.child(0);
        }
        if (bigDivElement.selectFirst("hr") != null) {
            bigDivElement.selectFirst("hr").previousElementSiblings().remove();
        }
        String text = BeiLunHtmlAnalysisUtil.blockDivToStr(bigDivElement, true);
        CommonAnalysisUtil.extractWardInfo(recTitle, text, map);
        String bqjlText = "病情记录" + map.get("病情记录");
        List<String> titles = Lists.newArrayList(
                "病情记录"
        );
        titles = CommonAnalysisUtil.sortTitlesNoColon(titles, bqjlText);
        CommonAnalysisUtil.cutByTitlesNoColon(bqjlText, titles, 0, map);
    }

}