package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.pub.model.doc.FirstCourseRecordDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.dbanaly.facade.changx.CxXmlUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.comsis.Preproc;

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

import static com.lantone.qc.trans.comsis.ModelDocGenerate.structureMapJoin;

/**
 * @Description: 首次病程录文档生成
 * @author: rengb
 * @time: 2020/3/17 13:25
 */
public class ChangxFirstCourseRecordDocTrans extends ModelDocTrans {

    @Override
    public FirstCourseRecordDoc extract(MedrecVo medrecVo) {
        Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = getCutWordMap(text);
        cutWordMap.putAll(structureMap);

        FirstCourseRecordDoc firstCourseRecordDoc = ModelDocGenerate.firstCourseRecordDocGen(cutWordMap);
        firstCourseRecordDoc.setText(text);
        firstCourseRecordDoc.setPageData((Map) cutWordMap);
        List<String> keys = Lists.newArrayList("需求评估", "预期目标", "诊疗计划", "治疗监测计划");
        String treatPlanJoin = structureMapJoin(structureMap, keys);
        firstCourseRecordDoc.getTreatPlanLabel().setAiText(treatPlanJoin);
        return firstCourseRecordDoc;
    }

    private Map<String, String> getCutWordMap(String content) {
        Map<String, String> retMap = Maps.newHashMap();
        if (StringUtil.isBlank(content)) {
            return retMap;
        }
        List<String> targetTitles = Lists.newArrayList();
        sourceTitles.forEach(sourceTitle -> {
            String targetTitle = "[一二三四五六七八九十]{0,1}[、.]{0,1}";
            for (int index = 0; index < sourceTitle.length(); index++) {
                if (index == sourceTitle.length() - 1) {
                    targetTitle += sourceTitle.substring(index, index + 1);
                } else {
                    targetTitle += sourceTitle.substring(index, index + 1) + "[\\s\\p{Zs}]*";
                }
            }
            targetTitles.add(targetTitle);
        });
        Map<String, String> sourceMap = Preproc.extract_doc_pub(true, targetTitles, content);
        for (String key : sourceMap.keySet()) {
            retMap.put(key.replaceAll("[一二三四五六七八九十、.]", ""), sourceMap.get(key));
        }
        return retMap;
    }

    private List<String> sourceTitles = Lists.newArrayList(
            "病例特点",
            "初步诊断",
            "诊断依据",
            "鉴别诊断",
            "诊疗计划"
    );

    private List<String> keyContrasts = Lists.newArrayList(
            "记录日期=记录时间",
            "签名++++=记录医师"
    );

}