package org.diagbot.rule.crisis;

import org.diagbot.common.push.bean.CrisisDetail;
import org.diagbot.common.push.bean.Rule;
import org.diagbot.common.push.bean.RuleApp;
import org.diagbot.common.push.bean.SearchData;
import org.diagbot.common.push.cache.ApplicationCacheUtil;
import org.springframework.util.StringUtils;

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

/**
 * Created by louhr on 2019/9/2.
 */
public class CrisisApplication {
    private String key = "crisis";

    public Map<String, List<CrisisDetail>> crisisContent(SearchData searchData) {
        Map<String, List<Rule>> rules = searchData.getRules();
        if (rules == null) {
            return new HashMap<String, List<CrisisDetail>>();
        }
        Map<String, RuleApp> ruleApps = ApplicationCacheUtil.getKl_rule_app_filter_map();

        Map<String, List<CrisisDetail>> crisisMap = new HashMap<>();
        List<CrisisDetail> crisisList = null;
        for (Map.Entry<String, RuleApp> app : ruleApps.entrySet()) {
            if (StringUtils.isEmpty(app.getValue().getRuleIds())) {
                continue;
            }
            String[] ruleIds = app.getValue().getRuleIds().split(",");
            String standardText = "", originText = "";
            boolean allRuleIdsSuit = true;
            for (String ruleId : ruleIds) {
                boolean isFindRule = false;
                for (Map.Entry<String, List<Rule>> suitRules : rules.entrySet()) {
                    for (Rule suitRule : suitRules.getValue()) {
                        if (ruleId.equals(suitRule.getId())) {
                            isFindRule = true;
                            standardText += suitRule.getPub_name() + ";";
                            originText += suitRule.getOriginText() + ";";
                            break;
                        }
                    }
                }
                if (!isFindRule) {
                    allRuleIdsSuit = false;
                    break;
                }
            }
            //所有规则都满足
            if (allRuleIdsSuit) {
                CrisisDetail crisisDetail = new CrisisDetail();
                crisisDetail.setOriginText(originText);
                crisisDetail.setStandardText(standardText);
                crisisDetail.setRemindText(app.getValue().getRemind());

                crisisList = crisisMap.get(key);
                if (crisisList == null) {
                    crisisList = new ArrayList<>();
                }
                boolean isHave = false;
                for (CrisisDetail c : crisisList) {
                    if (c.getRemindText().equals(crisisDetail.getRemindText())) {
                        isHave = true;
                    }
                }
                if (!isHave) {
                    crisisList.add(crisisDetail);
                }
                crisisMap.put(key, crisisList);
            }
        }
        return crisisMap;
    }

    private String mergeStandardText(Rule rule) {
        //标准值最优先匹配
        StringBuffer sb = new StringBuffer();
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getEq_value())) {
            sb.append(rule.getPub_name()).append(rule.getEq_operator()).append(rule.getEq_value());
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMax_value()) && org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMin_value())) {
            sb.append(rule.getPub_name()).append(rule.getMin_operator()).append(rule.getMin_value())
                    .append(rule.getMin_unit()).append(";")
                    .append(rule.getPub_name()).append(rule.getMax_operator()).append(rule.getMax_value())
                    .append(rule.getMax_unit());
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMin_value())) {
            sb.append(rule.getPub_name()).append(rule.getMin_operator()).append(rule.getMin_value())
                    .append(rule.getMin_unit());
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(rule.getMax_value())) {
            sb.append(rule.getPub_name()).append(rule.getMax_operator()).append(rule.getMax_value())
                    .append(rule.getMax_unit());
        }
        return sb.toString();
    }

}
