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.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.ward.AttendingDoctorWardDoc;
import com.lantone.qc.pub.model.doc.ward.DirectorDoctorWardDoc;
import com.lantone.qc.pub.model.doc.ward.GeneralDoctorWardDoc;
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.trans.changx.util.CxXmlUtil;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.comsis.Preproc;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 查房记录文档生成
 * @author: rengb
 * @time: 2020/3/17 13:27
 */
public class ChangxThreeLevelWardDocTrans extends ModelDocTrans {

    @Override
    public List<ThreeLevelWardDoc> extract(MedrecVo medrecVo) {
        List<ThreeLevelWardDoc> retList = Lists.newArrayList();
        List<String> contents = (List) medrecVo.getContent().get("content");
        ThreeLevelWardDoc threeLevelWardDoc = new ThreeLevelWardDoc();
        contents.forEach(content -> {
            classifyThreeLevelWardDoc(threeLevelWardDoc, content);
        });
        retList.add(threeLevelWardDoc);
        return retList;
    }

    private void classifyThreeLevelWardDoc(ThreeLevelWardDoc threeLevelWardDoc, String content) {
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);
        structureMap.put("查房日期", structureMap.get("记录时间"));

        Map<String, String> cutWordMap = Maps.newHashMap();
        String text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Region/Content_Text");
        if (StringUtil.isNotBlank(text)) {
            if (StringUtil.isBlank(structureMap.get("病情记录"))) {
                structureMap.put("病情记录", text);
            }
            cutWordMap = Preproc.getCutWordMap(true, sourceTitles, text);
            if (StringUtil.isBlank(structureMap.get("记录医师"))) {
                if(StringUtil.isNotEmpty(cutWordMap.get("医师签名"))){
                    Pattern p = Pattern.compile("^[^\\u4e00-\\u9fa5]+$");//分词结果如果不包含中文为日期，则不覆盖
                    Matcher m = p.matcher(cutWordMap.get("医师签名"));
                    if (!m.matches()) {
                        structureMap.put("记录医师", cutWordMap.get("医师签名"));
                    }
                }
            }
        }

        //总的查房记录 汇总
        ThreeLevelWardDoc allDoctorWradDoc = new ThreeLevelWardDoc();
        allDoctorWradDoc.setStructureMap(structureMap);
        allDoctorWradDoc.setPageData((Map) xmlNodeValueMap);
        threeLevelWardDoc.addAllDoctorWradDoc(allDoctorWradDoc);

        //主任医师查房
        DirectorDoctorWardDoc directorDoctorWardDoc = findDirectorDoctorWardDoc(xmlNodeValueMap, structureMap);
        if (directorDoctorWardDoc != null) {
            threeLevelWardDoc.addDirectorDoctorWardDoc(findDirectorDoctorWardDoc(xmlNodeValueMap, structureMap));
        }
        //主治医师查房
        AttendingDoctorWardDoc attendingDoctorWardDoc = findAttendingDoctorWardDoc(xmlNodeValueMap, structureMap);
        if (attendingDoctorWardDoc != null) {
            threeLevelWardDoc.addAttendingDoctorWardDoc(attendingDoctorWardDoc);
        }
        //普通医师查房
        if (directorDoctorWardDoc == null && attendingDoctorWardDoc == null) {
            GeneralDoctorWardDoc generalDoctorWardDoc = new GeneralDoctorWardDoc();
            generalDoctorWardDoc.setStructureMap(structureMap);
            generalDoctorWardDoc.setPageData((Map) xmlNodeValueMap);
            threeLevelWardDoc.addGeneralDoctorWardDoc(generalDoctorWardDoc);
        }
    }

    /**
     * 主任医师查房
     *
     * @param sourceMap
     * @param structureMap
     */
    private DirectorDoctorWardDoc findDirectorDoctorWardDoc(Map<String, String> sourceMap, Map<String, String> structureMap) {
        String title = structureMap.get("查房标题");
        title = subTitle(title);    //标题有代字

        DirectorDoctorWardDoc directorDoctorWardDoc = null;
        if (StringUtils.isNotEmpty(title) && title.contains("主任")) {
            directorDoctorWardDoc = new DirectorDoctorWardDoc();
            directorDoctorWardDoc.setStructureMap(structureMap);
            directorDoctorWardDoc.setPageData((Map) sourceMap);
        }
        return directorDoctorWardDoc;
    }

    /**
     * 主治医师查房
     *
     * @param sourceMap
     * @param structureMap
     */
    private AttendingDoctorWardDoc findAttendingDoctorWardDoc(Map<String, String> sourceMap, Map<String, String> structureMap) {
        String title = structureMap.get("查房标题");
        title = subTitle(title);    //标题有代字
        AttendingDoctorWardDoc attendingDoctorWardDoc = null;
        if (StringUtils.isNotEmpty(title) && title.contains("主治")) {
            attendingDoctorWardDoc = new AttendingDoctorWardDoc();
            attendingDoctorWardDoc.setStructureMap(structureMap);
            attendingDoctorWardDoc.setPageData((Map) sourceMap);
        }
        return attendingDoctorWardDoc;
    }

    private String subTitle(String srcText) {
        if (StringUtil.isNotBlank(srcText) && srcText.contains("代") && srcText.indexOf("代") != srcText.length() - 1) {
            srcText = srcText.substring(srcText.lastIndexOf("代") + 1);
        }
        return srcText;
    }

    private List<String> sourceTitles = Lists.newArrayList(
            "医师签名"
    );

    private List<String> keyContrasts = Lists.newArrayList(
            "病程记录标题=查房标题",
            "在此输入查房记录=病情记录",
            "签名++++=记录医师",
            "记录日期=记录时间"
    );

}