package org.diagbot.common.push.cache;

import org.apache.commons.lang3.StringUtils;
import org.diagbot.nlp.participle.cfg.Configuration;
import org.diagbot.nlp.participle.cfg.DefaultConfig;

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

public class CacheUtil {
    public static Map<String,Map<String,String>> sexAgeMap=null;
    public static Map<String,String> vitalMap=null;
    public static Map<String,String> disgSortMap=null;
    public static Map<String,String> diagClassifyMap = null;
    public static Map<String,Integer> diagClassifyJiMap = null;
    //诊断科室衍射
    public static Map<String, String> doc_result_mapping_diag_map = null;

    //疾病科室，获取缓存
    public static Map<String,String> getDiagDepartCache(){
        if (doc_result_mapping_diag_map == null) {
            createDiagDepartCache();
        }
        return doc_result_mapping_diag_map;
    }

    public static Map<String,String> createDiagDepartCache(){
        Configuration configuration = new DefaultConfig();
        doc_result_mapping_diag_map = new HashMap<>();
        List<String> contentList = configuration.readFileContents("graph_diag_2_dept.dict");
        for (String s:contentList) {
            String[] splits = s.split("\\|");
            doc_result_mapping_diag_map.put(splits[0],splits[1]);
        }
        return doc_result_mapping_diag_map;
    }

    //疾病分级解析，获取缓存
    public static Map<String,Integer> getDiagClassifyJiCache(){
        if (diagClassifyJiMap == null) {
            createDiagClassifyJiCache();
        }
        return diagClassifyJiMap;
    }

    public static Map<String,Integer> createDiagClassifyJiCache(){
        Configuration configuration = new DefaultConfig();
        diagClassifyJiMap = new HashMap<>();
        List<String> contentList = configuration.readFileContents("graph_diag_normalize.dict");
        String diagName =null,bigdiagName=null;
        String[] diagSort =null;String[] split = null;
        for (String s:contentList) {
            String[] splits = s.split("\\|");
            if (splits.length >= 2) {
                diagName = splits[0].trim();//疾病本体
                bigdiagName = splits[1].trim();//疾病大类
                split = bigdiagName.split("\\、");
                for (String dis:split) {
                    diagClassifyJiMap.put(dis, 2);
                }
                diagClassifyJiMap.put(diagName,1);
                if (splits.length == 3) {
                    diagSort = splits[2].split("\\、");
                    for (String dis : diagSort) {
                        if (StringUtils.isNotEmpty(dis.trim())) {
                            diagClassifyJiMap.put(dis.trim(), 3);
                        }
                    }
                }
            }
        }
        return diagClassifyJiMap;
    }

    //疾病归一解析，获取缓存
    public static Map<String,String> getDiagClassifyCache(){
        if (diagClassifyMap == null) {
            createDiagClassifyCache();
        }
        return diagClassifyMap;
    }

    public static Map<String,String> createDiagClassifyCache(){
        Configuration configuration = new DefaultConfig();
        diagClassifyMap = new HashMap<>();
        List<String> contentList = configuration.readFileContents("graph_diag_normalize.dict");
        String diagName =null,bigdiagName=null;
        String[] diagSort =null;String[] split =null;
        for (String s:contentList) {
            String[] splits = s.split("\\|");
            if (splits.length >= 2) {
                diagName = splits[0].trim();//疾病本体
                bigdiagName = splits[1].trim();//疾病大类
                split = bigdiagName.split("\\、");
                for (String dis:split
                        ) {
                    diagClassifyMap.put(dis, diagName);
                }

                if (splits.length == 3) {
                    diagSort = splits[2].split("\\、");
                    for (String dis : diagSort) {
                        if (StringUtils.isNotEmpty(dis.trim())) {
                            diagClassifyMap.put(dis.trim(), bigdiagName);
                        }
                    }
                }
            }
        }
        return diagClassifyMap;
    }

    public static Map<String,String> getDiagSortCache(){
        if(disgSortMap == null){
            createDiagSortCache();
        }
        return disgSortMap;
    }
    public static Map<String,String> createDiagSortCache(){
        disgSortMap = new HashMap<>();

        Configuration configuration = new DefaultConfig();
        List<String> contentList = configuration.readFileContents("graph_diag_classify.dict");
        for (String s:contentList) {
            String[] splits = s.split("\\|");
            String diagName = splits[0];
            String diagType = splits[1];
            disgSortMap.put(diagName,diagType);
        }
        return disgSortMap;
    }

    public static Map<String,Map<String,String>> getSexAgeCache(){
        if (sexAgeMap == null) {
            createSexAgeCache();
        }
        return sexAgeMap;
    }

    public static Map<String,Map<String,String>> createSexAgeCache(){
        Configuration configuration = new DefaultConfig();
        sexAgeMap = new HashMap<>();
        List<String> contentList = configuration.readFileContents("graph_sex_age_filter.dict");
        for (String s:contentList) {
            Map<String,String> contentMap = new HashMap<>();
            String[] splits = s.split("\\|");
            String sexTyep = splits[1];
            String min_age = splits[2];
            String max_age = splits[3];
            contentMap.put("sexType",sexTyep);
            contentMap.put("min_age",min_age);
            contentMap.put("max_age",max_age);
            sexAgeMap.put(splits[0],contentMap);
        }
        return sexAgeMap;
    }

    public static Map<String,String> getVitalCache(){
        if (vitalMap == null) {
            createVitalCache();
        }
        return vitalMap;
    }

    public static Map<String,String> createVitalCache(){
        Configuration configuration = new DefaultConfig();
        vitalMap = new HashMap<>();
        List<String> contentList = configuration.readFileContents("graph_vital_convert.dict");
        for (String s:contentList) {
            String[] splits = s.split("\\|");
            vitalMap.put(splits[0],splits[1]);
        }
        return vitalMap;
    }
}
