package com.lantone.structure.facade.tran;

import com.lantone.common.util.StringUtil;
import com.lantone.structure.model.doc.DailyCourseRecordDoc;
import com.lantone.structure.model.label.DailyCourseRecordLabel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Description:日常病程记录
 * @author: cy
 * @time: 2021/3/16 17:23
 */
@Slf4j
public class DailyCourseRecordTran extends TargetTran {

    @Override
    public Map<String, String> convert(String text) {
        List<DailyCourseRecordDoc> dailyCourseRecordDocs = new ArrayList<>();
        DailyCourseRecordDoc dailyCourseRecordDoc  = new DailyCourseRecordDoc();
        dailyCourseRecordDoc.setText(text);
        dailyCourseRecordDocs.add(dailyCourseRecordDoc);
        inputInfo.setDailyCourseRecordDocs(dailyCourseRecordDocs);
        Map<String, String> structureMap = new HashMap<String, String>();
        dailyCourseContrast(inputInfo.getDailyCourseRecordDocs(), structureMap);
        dailyCourseRecordDoc.setStructureMap(structureMap);
        aiProcess();
        lastSovle(inputInfo.getDailyCourseRecordDocs(),structureMap);
        return structureMap;
    }

    public void dailyCourseContrast(List<DailyCourseRecordDoc> dailyCourseRecordDocs,Map<String, String> retMap) {
        String text = dailyCourseRecordDocs.get(0).getText();
        String file = text;
        String str = extractDate(file);
            if(StringUtil.isNotEmpty(str)){
                retMap.put("记录日期时间",str);
        }

        String signDate = "签名日期";
        if(file.contains("签字日期")){
            signDate = "签字日期";
        }
        if(file.contains(signDate)){
            String date = file.substring(file.lastIndexOf(signDate));
            date = extractDate(date);
            if(StringUtil.isNotEmpty(date)){
                retMap.put("签名日期时间",date);
            }
        }
        String signName= parseString(file);
        signName = signName.replace("\n", "").replace("　","").trim();
        if(StringUtil.isNotEmpty(signName) && signName.length()<15){
            retMap.put("医师签名",signName);
        }
        String ret = text;
        if(StringUtil.isNotEmpty(str)){
            String[] split = text.split(str);
            ret = text.substring(text.lastIndexOf(split[1]));
            String[] notes = ret.split("记录");
            ret = ret.substring(ret.lastIndexOf(notes[1]));
        }
            if(StringUtil.isNotEmpty(retMap.get("医师签名")) && ret.contains(retMap.get("医师签名"))){
                ret = ret.substring(0, ret.lastIndexOf(retMap.get("医师签名")));
            }
            String flag  = "/";
            if(ret.endsWith("//")){
                flag = "//";
            }

        String flagStr = "注意事项：";
        if(file.contains("处理意见：")){
            flagStr = "处理意见：";
        }
        if(!file.contains(flagStr) && file.contains("查房指出")){
            flagStr = "查房指出";
        }
        if(!file.contains(flagStr) && file.contains("查房后指出：")){
            flagStr = "查房后指出：";
        }
        if(!file.contains(flagStr) && file.contains("拟今予")){
            flagStr = "拟今予";
        }
        if(!file.contains(flagStr) && file.contains("今予")){
            flagStr = "今予";
        }
        if(!file.contains(flagStr) && file.contains("拟今日")){
            flagStr = "拟今日";
        }
        if(!file.contains(flagStr) && file.contains("继续予")){
            flagStr = "继续予";
        }
        if(file.contains(flagStr)){
            String doctorAdvice = file.substring(file.lastIndexOf(flagStr)+flagStr.length());
            if(file.contains("拟今日")||file.contains("拟今予")){
                 doctorAdvice = file.substring(file.lastIndexOf(flagStr));
            }
            if(StringUtil.isNotEmpty(retMap.get("医师签名")) && doctorAdvice.contains(retMap.get("医师签名"))){
                doctorAdvice = doctorAdvice.substring(0, doctorAdvice.lastIndexOf(retMap.get("医师签名")));
            }
                if(doctorAdvice.endsWith(flag)){
                    doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf(flag));
                }
                if(doctorAdvice.contains("修正诊断")){
                    doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf("修正诊断"));
                }
                if(doctorAdvice.contains("补充诊断")){
                    doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf("补充诊断"));
                }
            if(doctorAdvice.contains("AE及合并用药")){
                doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf("AE及合并用药"));
                if(doctorAdvice.contains("。")){
                    doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf("。"));
                }
            }
            if(doctorAdvice.contains("药物号：")){
                doctorAdvice =  doctorAdvice.substring(0,doctorAdvice.lastIndexOf("药物号："));
                String sub = file.substring(file.lastIndexOf("药物号："));
                if(sub.contains("。")){
                    sub =  sub.split("。")[0];
                    doctorAdvice = doctorAdvice+sub;
                }
            }
                if(doctorAdvice.startsWith("，")){
                    doctorAdvice =  doctorAdvice.substring("，".length());
                }
            retMap.put("医嘱内容",doctorAdvice);
            if(StringUtils.isNotEmpty(retMap.get("医嘱内容")) && retMap.get("医嘱内容").replace("\n","").replace("　","").trim().equals("。")){
                retMap.remove("医嘱内容");
            }
        }
            if(ret.endsWith(flag)){
                ret =  ret.substring(0,ret.lastIndexOf(flag));
            }
            if(StringUtil.isNotEmpty(ret)){
                retMap.put("住院病程",ret);
            }
    }

    public static void lastSovle(List<DailyCourseRecordDoc> dailyCourseRecordDocs , Map<String,String> rep) {
        if(!dailyCourseRecordDocs.isEmpty() && dailyCourseRecordDocs.get(0)!=null && dailyCourseRecordDocs.get(0)
                .getDailyCourseRecordLabel()!=null){
            DailyCourseRecordLabel dailyCourseRecordLabel = dailyCourseRecordDocs.get(0).getDailyCourseRecordLabel();
            String doctorAdviceRecord = dailyCourseRecordLabel.getDoctorAdviceRecord();
            if(StringUtil.isNotEmpty(doctorAdviceRecord)){
                if(StringUtil.isEmpty(rep.get("医嘱内容"))){
                    rep.put("医嘱内容",doctorAdviceRecord);
                }
            }
        }
    }


    /**
     * 抽取文本中的第一个时间
     *
     * @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;
    }

    // 取/前
    public static String parseStr(String text){
        if(text.contains("/")){
            text = text.substring(0,text.lastIndexOf("/"));
            text= parseStr(text);
        }
        return text;
    }
}