package org.diagbot.common.push.filter.rule;


import org.diagbot.common.push.bean.Rule;
import org.diagbot.common.push.bean.SearchData;
import org.diagbot.common.push.cache.ApplicationCacheUtil;
import org.diagbot.nlp.rule.module.PreResult;
import org.diagbot.nlp.rule.pretreat.*;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PretreatmentRule {
    public void rule(SearchData searchData) throws java.io.IOException {
        //患者基本信息 性别 年龄
        if (!StringUtils.isEmpty(searchData.getNormal())) {
            searchData.setNormal(add2PreResultList(new PretreatmentNormal(), searchData.getNormal(), "normal", searchData));
        }
        //症状数据
        if (!StringUtils.isEmpty(searchData.getSymptom())) {
            searchData.setSymptom(add2PreResultList(new PretreatmentSymptom(), searchData.getSymptom(), "symptom", searchData));
        }
        //体征数据
        if (!StringUtils.isEmpty(searchData.getVital())) {
            searchData.setVital(add2PreResultList(new PretreatmentVital(), searchData.getVital(), "vital", searchData));
        }
        //lis结构化信息
        if (searchData.getLisArr() != null && searchData.getLisArr().size() > 0) {
            searchData.setLis(add2PreResultList(searchData.getLisArr(), searchData.getLis(), "lis", searchData));
        } else if (!StringUtils.isEmpty(searchData.getLis())) {
            searchData.setLis(add2PreResultList(new PretreatmentLis(), searchData.getLis(), "lis", searchData));
        }
        //pacs数据
        if (!StringUtils.isEmpty(searchData.getPacs())) {
            searchData.setPacs(add2PreResultList(new PretreatmentPacs(), searchData.getPacs(), "pacs", searchData));
        }
        //其他史
        if (!StringUtils.isEmpty(searchData.getOther())) {
            searchData.setOther(add2PreResultList(new PretreatmentOther(), searchData.getOther(), "other", searchData));
        }
        //开具诊断
        if (!StringUtils.isEmpty(searchData.getDiag())) {
            add2PreResultList(new PretreatmentDiag(), searchData.getDiag(), "diag", searchData);
        }
        //开具辅检化验
        if (!StringUtils.isEmpty(searchData.getLisOrder())) {
            add2PreResultList(new PretreatmentMakeList(), searchData.getLisOrder(), "lisOrder", searchData);
        }
        //开具辅检化验
        if (!StringUtils.isEmpty(searchData.getPacsOrder())) {
            add2PreResultList(new PretreatmentMakeList(), searchData.getPacsOrder(), "pacsOrder", searchData);
        }
    }

    private String add2PreResultList(Pretreatment pretreatment, String content, String ruleType, SearchData searchData) throws java.io.IOException {
        List<PreResult> preResultList = pretreatment.analyze(content);
        return add2PreResultList(preResultList, content, ruleType, searchData);
    }

    private String add2PreResultList(List<PreResult> preResultList, String content, String ruleType, SearchData searchData) throws java.io.IOException {
        Map<String, List<Rule>> kl_rule_filter_map = ApplicationCacheUtil.getKl_rule_filter_map();
        //符合条件的规则
        Map<String, List<Rule>> accord_rule_map = searchData.getRules();
        List<Rule> accord_rules = null;
        if (preResultList != null) {
            for (PreResult result : preResultList) {
                //规则库中匹配
                if (kl_rule_filter_map.get(result.getUniqueName()) != null) {
                    List<Rule> rules = kl_rule_filter_map.get(result.getUniqueName());
                    if (rules == null) {
                        continue;
                    }
                    for (Rule rule : rules) {
                        boolean isSuit = suitRule(result, rule, content);
                        if (isSuit) {
                            rule.setOriginText(result.getUniqueName() + ":" + result.getValue() + result.getUnits());
                            if (accord_rule_map.get(ruleType) == null) {
                                accord_rules = new ArrayList<>();
                            } else {
                                accord_rules = accord_rule_map.get(ruleType);
                            }
                            accord_rules.add(rule);
                            accord_rule_map.put(ruleType, accord_rules);
                            searchData.setRules(accord_rule_map);

                            content = content + (rule.getRemind() == null ? "" : rule.getRemind());
                        }
                    }
                }
            }

        }
        return content;
    }

    private boolean suitRule(PreResult result, Rule rule, String content) {
        //标准值最优先匹配
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getEq_value())) {
            if (compareEqual(result.getValue(), rule.getEq_value())) {
                return true;
            } else {
                return false;
            }
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMax_value()) && org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMin_value())) {
            if (compareMin(result.getValue(), rule.getMax_value(), rule.getMax_operator()) //比最大值小
                    && compareMax(result.getValue(), rule.getMin_value(), rule.getMin_operator()) //比最小值大
                    && result.getUnits().equals(rule.getMin_unit())
                    && result.getUnits().equals(rule.getMax_unit())) {
                return true;
            } else {
                return false;
            }
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMin_value())) {
            if (compareMin(result.getValue(), rule.getMin_value(), rule.getMin_operator())   //比最小值小
                    && result.getUnits().equals(rule.getMin_unit())) {
                return true;
            } else {
                return false;
            }
        } else if(org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMax_value()))  {
            if (compareMax(result.getValue(), rule.getMax_value(), rule.getMax_operator())   //比最大值大
                    && result.getUnits().equals(rule.getMax_unit())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }


    private boolean compareEqual(String c1, String c2) {
        if (!StringUtils.isEmpty(c1) && !StringUtils.isEmpty(c2)
                && c1.equals(c2)) {
            return true;
        }
        return false;
    }

    private boolean compareMin(String c1, String c2, String operator) {
        if (!StringUtils.isEmpty(c1) && !StringUtils.isEmpty(c2) && !StringUtils.isEmpty(operator)) {
            try {
                if (operator.contains("=")) {
                    return Double.valueOf(c1) <= Double.valueOf(c2);
                } else {
                    return Double.valueOf(c1) < Double.valueOf(c2);
                }
            } catch (Exception e) {
            }
        }
        return false;
    }

    private boolean compareMax(String c1, String c2, String operator) {
        if (!StringUtils.isEmpty(c1) && !StringUtils.isEmpty(c2) && !StringUtils.isEmpty(operator)) {
            try {
                if (operator.contains("=")) {
                    return Double.valueOf(c1) >= Double.valueOf(c2);
                } else {
                    return Double.valueOf(c1) > Double.valueOf(c2);
                }
            } catch (Exception e) {
            }
        }
        return false;
    }

}
