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.util.MapUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @Description:病危通知书
 * @author: wsy
 * @time: 2021/3/08 11:29
 */
@Slf4j
public class CriticallyIllNoticeDocTran extends TargetTran {
    @Override
    public Map<String, String> convert(String text) {
        Map<String, String> structureMap = cutWord(text);
        Map<String, String> retMap = new HashMap<String, String>();
        CommonAnalysisUtil.mapKeyContrastCommon(structureMap, keyContrasts, retMap);
        retMap.entrySet().removeIf(entry -> StringUtil.isBlank(entry.getValue()));
        retMap.entrySet().removeIf(entry ->
                !entry.getValue().matches("[\\d]+年[\\d]+月[\\d]+日[\\d]+时[\\d]+分") && ("病危（重）通知日期时间".equals(entry.getKey()) || "法定代理人签名日期时间".equals(entry.getKey()))
        );
        return retMap;
    }

    private Map<String, String> cutWord(String text) {
        Map<String, String> sourceMap = Maps.newHashMap();
        String dateStr = "";
        text = text.replace("年   月   日   时   分", "")
                .replace("年　月　日  时  分","");
        List<String> titles = CommonAnalysisUtil.sortTitles(
                Lists.newArrayList("科别", "住院号", "入院日期", "入住院日期", "姓名", "性别", "年龄", "职业", "临床诊断","病危通知书", "病情概要", "病重情况", "病情危重情况", "主要抢救措施"),
                text
        );
        CommonAnalysisUtil.cutByTitles(text, titles, 0, sourceMap);
        List<String> keys = Lists.newArrayList("病情概要", "主要抢救措施");
        if (sourceMap != null) {
            if (sourceMap.containsKey("主要抢救措施")) {
                dateStr = sourceMap.get("主要抢救措施");
                sourceMap.put("主要抢救措施", dateStr.substring(0, Math.max(0, dateStr.indexOf("病人家属签名"))));
            }else if(sourceMap.containsKey("病危通知书")){
                dateStr = sourceMap.get("病危通知书");
                sourceMap.put("病危通知书", dateStr.substring(0, Math.max(0, dateStr.indexOf("病人家属签名"))));
            }
            joinWord(sourceMap, keys);
            List<String> listTitle = Lists.newArrayList(
                    "病人家属签名", "关系", "签名日期", "通知时间", "经治医师", "科主任"
            );
            CommonAnalysisUtil.cutByTitlesNoColon(dateStr, listTitle, 0, sourceMap);
            CommonAnalysisUtil.removeKey(sourceMap, "病情概要", "主要抢救措施");
            //判断日期是否正确

            disDate(sourceMap, "关系", "时间");
        }
        return sourceMap;
    }

    private void disDate(Map sourceMap, String... args) {
        Map<String, String> sourceMap_ = MapUtil.copyMap(sourceMap);
        if (sourceMap.containsKey(args[0]) && sourceMap_.get(args[0]).contains(args[1])) {
            int index = sourceMap_.get(args[0]).indexOf(args[1]);
            sourceMap.put(args[0], sourceMap_.get(args[0]).substring(0, index));
            sourceMap.put(args[0] + args[1], sourceMap_.get(args[0]).substring(index).replace(args[1], "").replaceAll("[:：]", ""));
        }
    }


    private void joinWord(Map<String, String> sourceMap, List<String> keys) {
        StringBuffer sb = new StringBuffer();
        for (String key : keys) {
            if (sourceMap.containsKey(key)) {
                sb.append(key + ":" + sourceMap.get(key)).append("\r\n");
            }
        }
        sourceMap.put("病情概括及主要抢救措施", sb.toString());
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "科别=病区名称",
            "科室=科室名称",
            "姓名=患者姓名",
            "病房=病房号",
            "病床=病床号",
            "年龄=年龄（岁）",
            "病重情况=病危（重）通知内容",
            "病危通知书=病危（重）通知内容",
            "病情危重情况=病危（重）通知内容",
            "通知时间=病危（重）通知日期时间",
            "病人家属签名=法定代理人签名",
            "关系=法定代理人与患者关系",
            "时间=法定代理人签名日期时间",
            "关系时间=法定代理人签名日期时间",
            "签名日期=法定代理人签名日期时间",
            "经治医师=医师签名"
    );
}