package com.lantone.structure.facade.tran;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.common.util.StringUtil;
import com.lantone.structure.facade.tran.util.CommonAnalysisUtil;
import com.lantone.structure.model.doc.operation.OperationDiscussionDoc;
import com.lantone.structure.model.doc.operation.OperationDoc;
import com.lantone.structure.util.MapUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:术后首次病程
 * @author: cy
 * @time: 2021/3/16 14:12
 */
@Slf4j
public class OperationDiscussionTran extends TargetTran {

    @Override
    public Map<String, String> convert(String text) {
         List<OperationDoc> operationDocs = new ArrayList<>();
        OperationDoc operationDoc  = new OperationDoc();
        OperationDiscussionDoc operationDiscussionDoc  = new OperationDiscussionDoc();
        operationDiscussionDoc.setText(text);
        operationDoc.setOperationDiscussionDoc(operationDiscussionDoc);
        operationDocs.add(operationDoc);
        inputInfo.setOperationDocs(operationDocs);
        Map<String, String> structureMap = new HashMap<String, String>();
        Map<String, String> retMap = new HashMap<String, String>();
            structureMap = cutWord(text);
        oDContrast(inputInfo.getOperationDocs(), structureMap);
        mapKeyContrastCommon(structureMap,stagesContrasts,retMap);
        return retMap;
    }

    private Map<String, String> cutWord(String text) {
        Map<String, String> sourceMap = Maps.newHashMap();
        List<String> titles = CommonAnalysisUtil.sortTitles(
                Lists.newArrayList("简要病情"),
                text
        );
        CommonAnalysisUtil.cutByTitles(text, titles, 0, sourceMap);
        if( sourceMap.containsKey("术前诊断")){
            sourceMap.remove("术前诊断");
        }
        return sourceMap;
    }

    private List<String> stagesContrasts = Lists.newArrayList(
            "手术指征=手术指征",
            "手术指征=手术适应证"
    );

    public void oDContrast(List<OperationDoc> operationDocs,Map<String, String> retMap) {
        String text = operationDocs.get(0).getOperationDiscussionDoc().getText();
        if(text.contains("\n")){
            String[] split = text.split("\n");
            for (int i = 0; i < split.length; i++) {
                if(i<=1){
                    String date= extractDate(split[i]);
                    if(StringUtil.isNotEmpty(date)){
                        retMap.put("记录日期时间",date);
                    }
                }
            }
        }
        String flagTitle = "患者诊断";
        if(text.contains("修正诊断")){
            flagTitle = "修正诊断";
        }
        if(text.contains("术后诊断")){
            flagTitle = "术后诊断";
        }
        if(text.contains("术后诊断：")){
            flagTitle = "术后诊断：";
        }
        if(text.contains("目前诊断")){
            flagTitle = "目前诊断";
        }
        if(text.contains("目前诊断考虑")){
            flagTitle = "目前诊断考虑";
        }
        if(text.contains("诊断为")){
            flagTitle = "诊断为";
        }

        String flagTitleFir = "分析病历特点：";
         if(text.contains("查房：")){
            flagTitleFir = "查房：";
        }if(text.contains("查房后指出")){
            flagTitleFir = "查房后指出";
        }if(text.contains("术中诊断")){
            flagTitleFir = "术中诊断";
        }
        String flagTitleSec = "诊疗计划";
        String flagTitleThr = "注意事项";
        //诊断依据
        if(text.contains(flagTitleFir)){
            String strSec = text.substring(text.indexOf(flagTitleFir) + flagTitleFir.length());
            if(strSec.contains(flagTitleSec)){
                strSec = strSec.substring(0,strSec.indexOf(flagTitleSec));
            }
            if(strSec.contains("术后诊断：")){
                strSec = strSec.substring(0,strSec.indexOf("术后诊断："));
            }
            if(StringUtil.isNotEmpty(strSec)){
                retMap.put("诊断依据",strSec);
            }
        }

        if(text.contains(flagTitle)){
            String sureStr = text.substring(text.indexOf(flagTitle) + flagTitle.length());

            //注意事项
            if(sureStr.contains("注意观察")){
                flagTitleThr = "注意观察";
            }
            if(sureStr.contains(flagTitleThr)){
                String strSec = sureStr.substring(sureStr.indexOf(flagTitleThr) + flagTitleThr.length());
                if(flagTitleThr.equals("注意观察")){
                    strSec = sureStr.substring(sureStr.indexOf(flagTitleThr));
                }
                if(strSec.contains("/")){
                    strSec = strSec.substring(0,strSec.indexOf("/"));
                }
                if(strSec.contains("。今")){
                    strSec = strSec.substring(0,strSec.indexOf("。今"));
                }
                if(StringUtil.isNotEmpty(strSec)){
                    retMap.put("注意事项",strSec);
                }
            }
            if(sureStr.contains("鉴别诊断")){
                sureStr = sureStr.substring(0,sureStr.indexOf("鉴别诊断"));
            }
            if(sureStr.contains("禁忌症")){
                sureStr = sureStr.substring(0,sureStr.indexOf("禁忌症"));
            }
            if(sureStr.contains("手术指征")){
                sureStr = sureStr.substring(0,sureStr.indexOf("手术指征"));
            }
            if(sureStr.contains("明确")){
                sureStr = sureStr.substring(0,sureStr.indexOf("明确"));
            }if(sureStr.contains("“") && sureStr.contains("”")){
                sureStr = sureStr.substring(sureStr.indexOf("“")+"“".length(),sureStr.indexOf("”"));
            }
            if (sureStr.contains("。")) {
                sureStr = sureStr.split("。")[0];
            }
            retMap.put("术后诊断名称",sureStr);
        }
        String notion = "注意";
       if(text.contains("注意观察")){
            notion = "注意观察";
        }
        if(text.contains(notion) && StringUtil.isEmpty(retMap.get("注意事项"))){
            String sureStr = text.substring(text.indexOf(notion));
            if(sureStr.contains("评分：")){
                sureStr = sureStr.substring(0,sureStr.indexOf("评分："));
            }
            if(sureStr.contains("。")){
                sureStr = sureStr.substring(0,sureStr.lastIndexOf("。"));
            }
            retMap.put("注意事项",sureStr);
        }
        String signName= parseString(text);
        if(StringUtil.isNotEmpty(signName)&& !signName.contains(text) && signName.length()<=15){
            retMap.put("医师签名",signName);
        }
        if(StringUtil.isEmpty(retMap.get("医师签名")) ||StringUtil.isEmpty(retMap.get("医师签名").trim())){
            retMap.remove("医师签名");
        }

    }

    public static void mapKeyContrastCommon(Map sourceMap, List<String> keyContrasts, Map<String, String> retMap) {
        Map<String, String> sourceMap_ = MapUtil.copyMap(sourceMap);
        String[] arry = null;
        String sourceKey = null, targetKey;
        Set<String> removeKey = new HashSet<>();
        for (String keyContrast : keyContrasts) {
            arry = keyContrast.split("=");
            sourceKey = arry[0];
            if (arry.length == 1) {
                targetKey = arry[0];
            } else {
                targetKey = arry[1];
            }
            if (StringUtil.isNotBlank(sourceMap_.get(sourceKey))
                    && (!retMap.containsKey(targetKey) || StringUtil.isBlank(retMap.get(targetKey)))) {
                retMap.put(targetKey, sourceMap_.get(sourceKey));
            }
            removeKey.add(sourceKey);
        }
        Set<String> keySet = retMap.keySet();
        for (String key : sourceMap_.keySet()) {
            if (!keySet.contains(key) && !removeKey.contains(key)) { // 如果之前已放过key就不用放了
                retMap.put(key, sourceMap_.get(key));
            }
        }
    }

    /**
     * 抽取文本中的第一个时间
     *
     * @param top
     * @return
     */
    public static String extractDate (String top){
        Pattern pattern = Pattern.compile("[0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}");
        Matcher matcher = pattern.matcher(top);
        if (matcher.find()) {
            return matcher.group(0);
        } else {
            Pattern p1 = Pattern.compile("[0-9]{4}年[0-9]+月[0-9]+日");
            Matcher m1 = p1.matcher(top);
            if (m1.find()) {
                return m1.group(0);
            }
        }
        return null;
    }

    // 取/后
    public static String parseString(String text){
        if(text.contains("/")){
            text = text.substring(text.lastIndexOf("/")+1);
            text= parseString(text);
        }
        return text;



    }
}