package org.diagbot.bigdata.work;


import org.diagbot.bigdata.common.ApplicationCacheUtil;
import org.diagbot.common.javabean.Rule;
import org.diagbot.common.push.filter.PreResult;
import org.diagbot.common.push.filter.pretreat.Pretreatment;
import org.diagbot.common.push.filter.pretreat.PretreatmentSymptom;
import org.diagbot.common.push.filter.pretreat.PretreatmentVital;
import org.diagbot.common.work.CrisisDetail;
import org.diagbot.common.work.LisDetail;
import org.diagbot.common.work.ResponseData;
import org.diagbot.common.work.SearchData;
import org.diagbot.pub.Constants;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PretreatmentFilter {
    public void crisisFilter(SearchData searchData, ResponseData responseData) throws java.io.IOException {
        //将数值类型内容全部转换为标准术语  依据kl_rule提供规则

        List<CrisisDetail> allCrisisDetailList = new ArrayList<>();
        //症状数据
        if (!StringUtils.isEmpty(searchData.getSymptom())) {
            searchData.setSymptom(add2PreResultList(new PretreatmentSymptom(), searchData.getSymptom(), allCrisisDetailList));
        }
        //体征数据
        if (!StringUtils.isEmpty(searchData.getVital())) {
            searchData.setVital(add2PreResultList(new PretreatmentVital(), searchData.getVital(), allCrisisDetailList));
        }
        //lis文本非结构化数据
        if (!StringUtils.isEmpty(searchData.getLis())) {
            searchData.setLis(add2PreResultList(new PretreatmentVital(), searchData.getLis(), allCrisisDetailList));
        }
        //lis结构化信息
        if (searchData.getLisArr() != null && searchData.getLisArr().size() > 0) {
            List<PreResult> preResultList = new ArrayList<>();
            for (LisDetail detail : searchData.getLisArr()) {
                PreResult result = new PreResult();
                result.setName(detail.getName());
                result.setDetailName(detail.getDetailName());
                result.setUnits(detail.getUnits());
                result.setMaxValue(String.valueOf(detail.getMaxValue()));
                result.setMinValue(String.valueOf(detail.getMinValue()));
                result.setOtherValue(detail.getOtherValue());
                result.setUniqueName(detail.getUniqueName());
                result.setValue(String.valueOf(detail.getValue()));
                preResultList.add(result);
            }
            searchData.setLis(add2PreResultList(preResultList, searchData.getLis(), allCrisisDetailList));
        }
        //pacs数据
        if (!StringUtils.isEmpty(searchData.getPacs())) {
            searchData.setPacs(add2PreResultList(new PretreatmentVital(), searchData.getPacs(), allCrisisDetailList));
        }
        responseData.setCrisisDetails(allCrisisDetailList);
    }

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

    private String add2PreResultList(List<PreResult> preResultList, String content, List<CrisisDetail> allCrisisDetailList) throws java.io.IOException {
        Map<String, List<Rule>> kl_rule_filter_map = ApplicationCacheUtil.getKl_rule_filter_map();
        if (preResultList != null) {
            for (PreResult result : preResultList) {
                //规则库中匹配
                if (kl_rule_filter_map.get(result.getDetailName()) != null) {
                    List<Rule> rules = kl_rule_filter_map.get(result.getDetailName());
                    if (rules == null) {
                        continue;
                    }
                    for (Rule rule : rules) {
                        if (Constants.COMMON_STRING_1.equals(rule.getSet_status())) {   //需要匹配大类
                            if (StringUtils.isEmpty(rule.getSet_name()) || !result.getName().equals(rule.getSet_name())) {
                                continue;
                            }
                        }
                        content = conceptConvert(result, rule, content);
                        //危急值提醒
                        if (Constants.COMMON_STRING_1.equals(rule.getType_value())) {
                            conceptCrisis(result, rule, allCrisisDetailList);
                        }
                    }
                }
            }

        }
        return content;
    }

    private String conceptConvert(PreResult result, Rule rule, String content) {
        //标准值最优先匹配 暂时忽略单位
        if (compareEqual(result.getValue(), rule.getSet_value())) {
            content = content + rule.getSet_concept_text();
        }
        if (compareMin(result.getValue(), rule.getMin_value())) {
            content = content + rule.getMin_concept_text();
        }
        if (compareMax(result.getValue(), rule.getMax_value())) {
            content = content + rule.getMax_concept_text();
        }
        return content;
    }

    private void conceptCrisis(PreResult result, Rule rule, List<CrisisDetail> allCrisisDetailList) {
        boolean crisis = false;
        CrisisDetail crisisDetail = new CrisisDetail();
        crisisDetail.setOriginText(result.getDetailName() + " = " + result.getValue() + result.getUnits());
        if (compareEqual(result.getValue(), rule.getSet_value())) {
            crisisDetail.setStandardText(result.getDetailName() + " = " + rule.getSet_value() + rule.getUnit());
            crisisDetail.setRemindText(rule.getSetRemind());
            crisis = true;
        }
        if (compareMin(result.getValue(), rule.getMin_value())) {
            crisisDetail.setStandardText(result.getDetailName() + " <= " + rule.getMin_value() + rule.getUnit());
            crisisDetail.setRemindText(rule.getMinRemind());
            crisis = true;
        }
        if (compareMax(result.getValue(), rule.getMax_value())) {
            crisisDetail.setStandardText(result.getDetailName() + " >= " + rule.getMax_value() + rule.getUnit());
            crisisDetail.setRemindText(rule.getMaxRemind());
            crisis = true;
        }
        if (crisis) {
            allCrisisDetailList.add(crisisDetail);
        }
    }

    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) {
        if (!StringUtils.isEmpty(c1) && !StringUtils.isEmpty(c2)) {
            try {
                return Double.valueOf(c1) < Double.valueOf(c2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

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