package org.diagbot.rule.lis;

import org.diagbot.common.push.bean.SearchData;
import org.diagbot.nlp.rule.module.PreResult;
import org.diagbot.nlp.util.Constants;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: HUJING
 * @Date: 2019/9/25 13:33
 */
public class LisApplication {
    private static String up = "升高";
    private static String down = "降低";
    private static String normal = "正常";

    public void lisConvertToInputs(List<PreResult> preResultList, SearchData searchData) {
        if (preResultList == null || preResultList.size() == 0) {
            return;
        }
        String covertValue = "";
        for (PreResult result : preResultList) {
            if (!StringUtils.isEmpty(result.getUniqueName())) {
                //value是数值类型则进行转换
                if (!StringUtils.isEmpty(result.getValue()) && isNumber(result.getValue())) {
                    if (!StringUtils.isEmpty(result.getMaxValue()) && !StringUtils.isEmpty(result.getMinValue())
                            && isNormal(result.getValue(), result.getMaxValue(), result.getMinValue())) {
                        covertValue = result.getUniqueName() + normal;
                    } else if (!StringUtils.isEmpty(result.getMaxValue())
                            && compareMax(result.getValue(), result.getMaxValue())) {
                        covertValue = result.getUniqueName() + up;
                    } else if (!StringUtils.isEmpty(result.getMinValue())
                            && compareMin(result.getValue(), result.getMinValue())) {
                        covertValue = result.getUniqueName() + down;
                    }
                } else if (!StringUtils.isEmpty(result.getOtherValue())) {
                    //otherValue是文本类型则直接与UniqueName拼接
                    covertValue = result.getUniqueName() + result.getOtherValue();
                }

                Map<String, String> map = new HashMap<>();
                map.put("featureType", Constants.feature_type_lis);
                map.put("featureName", covertValue);
                map.put("property", Constants.word_property_LIS_Result);
                map.put("concept", covertValue);
                //全是有
                map.put("negative", Constants.default_negative);
                if (searchData.getInputs().get(map.get("featureName")) == null) {
                    searchData.getInputs().put(map.get("featureName"), map);
                }
                if (searchData.getGraphInputs().get(map.get("featureName")) == null) {
                    searchData.getGraphInputs().put(map.get("featureName"), map);
                }
            }
        }
    }


    private boolean isNormal(String value, String maxValue, String minValue) {
        try {
            return Double.valueOf(value) < Double.valueOf(maxValue) && Double.valueOf(value) > Double.valueOf(minValue);
        } catch (Exception e) {
        }
        return false;
    }

    private boolean compareMin(String value, String minValue) {
        try {
            return Double.valueOf(value) < Double.valueOf(minValue);
        } catch (Exception e) {
        }
        return false;
    }

    private boolean compareMax(String value, String maxValue) {
        try {
            return Double.valueOf(value) > Double.valueOf(maxValue);
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 只要是整数或者是小数就是true
     * @param value
     * @return
     */
    public static boolean isNumber(String value) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        return Pattern.matches("^[-\\+]?\\d+(\\.\\d+)?",value);
    }
}
