package org.diagbot.bigdata.common;

import org.diagbot.bigdata.dao.model.ResultMappingFilter;
import org.diagbot.bigdata.util.BigDataConstants;
import org.diagbot.common.javabean.Rule;
import org.diagbot.nlp.participle.ParticipleUtil;
import org.diagbot.nlp.participle.cfg.Configuration;
import org.diagbot.nlp.participle.cfg.DefaultConfig;
import org.diagbot.nlp.participle.word.Lexeme;
import org.diagbot.nlp.participle.word.LexemePath;
import org.diagbot.nlp.util.NegativeEnum;
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, 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, List<Map<String, String>>> kl_result_mapping_standword_map = null;
    // 规则
    public static Map<String, List<Rule>> rule_filter_map = null;
    public static Map<String, List<Rule>> kl_rule_filter_map = null;


    public static Map<String, 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;
    }

//    /**
//     * 現已無用
//     * @return
//     */
//    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) {
            createDoc_result_mapping_diag_map();
        }
        return doc_result_mapping_diag_map;
    }

    public static Map<String, String> createDoc_result_mapping_diag_map() {
        Configuration configuration = new DefaultConfig();
        doc_result_mapping_diag_map = configuration.loadMapDict("bigdata_diag_2_dept.dict");
        return doc_result_mapping_diag_map;
    }

    public static Map<String, Map<String, ResultMappingFilter>> getDoc_result_mapping_filter_map() {
        if (doc_result_mapping_filter_map == null) {
            createDoc_result_mapping_filter_map();
        }
        return doc_result_mapping_filter_map;
    }

    public static Map<String, Map<String, ResultMappingFilter>> createDoc_result_mapping_filter_map() {
        Configuration configuration = new DefaultConfig();
        List<String> fileContents = configuration.readFileContents("bigdata_sex_age_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;
    }

    public static Map<String, List<Map<String, String>>> getKl_result_mapping_standword_map() {
        if (kl_result_mapping_standword_map == null) {
            createKl_result_mapping_standword_map();
        }
        return kl_result_mapping_standword_map;
    }

    public static Map<String, List<Map<String, String>>> createKl_result_mapping_standword_map() {
        kl_result_mapping_standword_map = new HashMap<>();
        Configuration configuration = new DefaultConfig();
        List<String> fileContents = configuration.readFileContents("bigdata_value_analyze.dict");
        List<Map<String, String>> standWordObjValList = null;
        Map<String, String> standWordObjVal = null;
        String operation = ">=|≥|>|大于|＞|超过|<=|≤|<|小于|＜|少于";
        try {
            for (String fileContent : fileContents) {
                LexemePath<Lexeme> lexemes = null;
                String op = "";
                String[] fileContentSplit = null;
                //每一个标准词根据大于小于符号切开，不然进行分词时还是会得到原本的标准词
                if (fileContent.contains(">") || fileContent.contains("大于")
                        || fileContent.contains("＞") || fileContent.contains("超过")) {
                    op = ">";
                    fileContentSplit = fileContent.split(operation);
                } else if (fileContent.contains("<") || fileContent.contains("小于")
                        || fileContent.contains("＜") || fileContent.contains("少于")) {
                    op = "<";
                    fileContentSplit = fileContent.split(operation);
                } else if (fileContent.contains(">=") || fileContent.contains("≥")){
                    op = ">=";
                    fileContentSplit = fileContent.split(operation);
                } else if (fileContent.contains("<=") || fileContent.contains("≤")) {
                    op = "<=";
                    fileContentSplit = fileContent.split(operation);
                } else {
                    continue;
                }
                LexemePath<Lexeme> lexemeWord = null;
                //每一个标准词切开后进行分词
                for (String fileContentWords : fileContentSplit) {
                    lexemeWord = ParticipleUtil.participle(fileContentWords);
                    if (lexemeWord != null) {
                        if (null == lexemes) {
                            lexemes = lexemeWord;
                        } else {
                            for (Lexeme lexeme : lexemeWord) {
                                lexemes.add(lexeme);
                            }
                        }
                    }
                }
                String standWordObjKey = "";
                standWordObjValList = new ArrayList<>();
                standWordObjVal = new HashMap<>();
                int i = 0;
                for (Lexeme lexeme : lexemes) {
                    i++;
                    if (lexeme.getProperty().contains(",")) {
                        setProterty(lexeme); //如果分词后词性有多个，只选一个(暂时只处理症状,体征)
                    }
                    NegativeEnum lexemeNegativeEnum = NegativeEnum.parseOfValue(lexeme.getProperty());
                    if (lexemeNegativeEnum == NegativeEnum.SYMPTOM || lexemeNegativeEnum == NegativeEnum.CAUSE
                            || lexemeNegativeEnum == NegativeEnum.VITAL_INDEX
                            || lexemeNegativeEnum == NegativeEnum.DIAG_STAND) {
                        if (!kl_result_mapping_standword_map.containsKey(lexeme.getText())) {
                            kl_result_mapping_standword_map.put(lexeme.getText(), standWordObjValList);
                        } else {
                            standWordObjKey = lexeme.getText();
                        }
                    }
                    if (lexemeNegativeEnum == NegativeEnum.DIGITS) {
                        standWordObjVal.put("value", lexeme.getText());
                    }
                    if (lexemeNegativeEnum == NegativeEnum.UNIT
                            || lexemeNegativeEnum == NegativeEnum.EVENT_TIME
                            || lexemeNegativeEnum == NegativeEnum.OTHER) {
                        standWordObjVal.put("unit", lexeme.getText().toLowerCase());
                    }
                    if (lexemes.size() == i) {
                        standWordObjVal.put("op", op);
                        standWordObjVal.put("standword", fileContent);
                        if (kl_result_mapping_standword_map.containsKey(standWordObjKey)) {
                            kl_result_mapping_standword_map.get(standWordObjKey).add(standWordObjVal);
                        } else {
                            standWordObjValList.add(standWordObjVal);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kl_result_mapping_standword_map;
    }

    public static Map<String, List<Rule>> getKl_rule_filter_map() {
        if (kl_rule_filter_map == null) {
            create_kl_rule_filter_map();
        }
        return kl_rule_filter_map;
    }

    public static void create_kl_rule_filter_map() {
        kl_rule_filter_map = new HashMap<>();

        Configuration configuration = new DefaultConfig();
        List<String> fileContents = configuration.readFileContents("bigdata_rule_filter.dict");

        List<Rule> rules = null;
        for (String line:fileContents) {
            String[] content = line.split("\\|");
            Rule rule = new Rule();
            if (content.length == 14) {
                rule.setIdx_name(content[0]);
                rule.setSet_name(content[1]);
                rule.setSet_status(content[2]);
                rule.setMin_value(content[3]);
                rule.setMin_concept_text(content[4]);
                rule.setMax_value(content[5]);
                rule.setMax_concept_text(content[6]);
                rule.setSet_value(content[7]);
                rule.setSet_concept_text(content[8]);
                rule.setUnit(content[9]);
                rule.setType_value(content[10]);
                rule.setMinRemind(content[11]);
                rule.setMaxRemind(content[12]);
                rule.setSetRemind(content[13]);
                if (kl_rule_filter_map.get(rule.getIdx_name()) == null) {
                    rules = new ArrayList<>();
                } else {
                    rules = kl_rule_filter_map.get(rule.getIdx_name());
                }
                rules.add(rule);
                kl_rule_filter_map.put(rule.getIdx_name(), rules);
            }
        }
    }

    public static void setProterty(Lexeme lexeme) {
        for (String featureType : lexeme.getProperty().split(",")) {
            switch (featureType) {
                case "1":
                    lexeme.setProperty("1");
                    break;
                case "33":
                    lexeme.setProperty("33");
                    break;
                case "70":
                    lexeme.setProperty("70");
                    break;

            }
        }
    }
}
