package org.diagbot.bigdata.common;

import org.diagbot.bigdata.dao.model.ResultMappingFilter;
import org.diagbot.bigdata.util.BigDataConstants;
import org.diagbot.nlp.participle.cfg.Configuration;
import org.diagbot.nlp.participle.cfg.DefaultConfig;
import org.diagbot.nlp.util.NlpCache;
import org.diagbot.pub.utils.security.EncrypDES;

import javax.servlet.ServletContext;
import java.util.*;

public class ApplicationCacheUtil {

    //词库同义词定义
    public static Map<String, String> standard_info_synonym_map = null;
    //词库大小类定义
    public static Map<String, String> standard_info_classify_map = null;
    //树形结构存储大小类
    public static Map<String, NlpCache.Node> standard_info_type_tree_map = null;
    //体征衍射
    public static Map<String, String> doc_result_mapping_vital_map = null;
    //诊断科室衍射
    public static Map<String, String> doc_result_mapping_diag_map = null;
    //特征性别 年龄过滤等
    public static Map<String, Map<String, ResultMappingFilter>> doc_result_mapping_filter_map = null;

    public static Map<String, String> getStandard_info_synonym_map() {
        if (standard_info_synonym_map == null) {
            standard_info_synonym_map = NlpCache.getStandard_info_synonym_map();
        }
        return standard_info_synonym_map;
    }

    public static Map<String, String> getStandard_info_classify_map() {
        if (standard_info_classify_map == null) {
            standard_info_classify_map = NlpCache.getStandard_info_classify_map();
        }
        return standard_info_classify_map;
    }

    public static Map<String, NlpCache.Node> getStandard_info_type_tree_map() {
        if (standard_info_type_tree_map == null) {
            standard_info_type_tree_map = NlpCache.getStandard_info_type_tree_map();
        }
        return standard_info_type_tree_map;
    }

    public static Map<String, String> getDoc_result_mapping_vital_map() {
        if (doc_result_mapping_vital_map == null) {
            Configuration configuration = new DefaultConfig();
            doc_result_mapping_vital_map = configuration.loadMapDict("doc_result_mapping_vital.dict");
        }
        return doc_result_mapping_vital_map;
    }

    public static Map<String, String> getDoc_result_mapping_diag_map() {
        if (doc_result_mapping_diag_map == null) {
            Configuration configuration = new DefaultConfig();
            doc_result_mapping_diag_map = configuration.loadMapDict("doc_result_mapping_diag.dict");
        }
        return doc_result_mapping_diag_map;
    }

    public static Map<String, Map<String, ResultMappingFilter>> getDoc_result_mapping_filter_map() {
        Configuration configuration = new DefaultConfig();
        if (doc_result_mapping_filter_map == null) {
            List<String> fileContents = configuration.readFileContents("doc_result_mapping_filter.dict");
            String[] line_string;
            List<ResultMappingFilter> resultMappingFilters = new ArrayList<>();
            try {
                for (int i = 0; i < fileContents.size(); i++) {
                    line_string = org.apache.commons.lang3.StringUtils.split(fileContents.get(i), "\\|");
                    if (line_string.length == 5) {
                        ResultMappingFilter resultMappingFilter = new ResultMappingFilter();
                        resultMappingFilter.setFeatureName(line_string[0]);
                        resultMappingFilter.setFeatureType(line_string[1]);
                        resultMappingFilter.setSex(line_string[2]);
                        resultMappingFilter.setAgeStart(Integer.parseInt(line_string[3]));
                        resultMappingFilter.setAgeEnd(Integer.parseInt(line_string[4]));
                        resultMappingFilters.add(resultMappingFilter);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            doc_result_mapping_filter_map = new HashMap<>();
            Map<String, ResultMappingFilter> filterMap = null;
            for (ResultMappingFilter resultMappingFilter : resultMappingFilters) {
                filterMap = doc_result_mapping_filter_map.get(resultMappingFilter.getFeatureType());
                if (filterMap == null) {
                    filterMap = new HashMap<>();
                }
                filterMap.put(resultMappingFilter.getFeatureName(), resultMappingFilter);
                doc_result_mapping_filter_map.put(resultMappingFilter.getFeatureType(), filterMap);
            }
        }
        return doc_result_mapping_filter_map;
    }
}
