package com.dcs.hnyz.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;

public enum OperationType implements IEnum<String> {

    GT("gt", ">") {
        @Override
        public boolean apply(String value, String threshold) {
            Double val = tryParseDouble(value);
            Double th = tryParseDouble(threshold);
            return val != null && th != null && val > th;
        }
    },
    GE("ge", ">=") {
        @Override
        public boolean apply(String value, String threshold) {
            Double val = tryParseDouble(value);
            Double th = tryParseDouble(threshold);
            return val != null && th != null && val >= th;
        }
    },
    LT("lt", "<") {
        @Override
        public boolean apply(String value, String threshold) {
            Double val = tryParseDouble(value);
            Double th = tryParseDouble(threshold);
            return val != null && th != null && val < th;
        }
    },
    LE("le", "<=") {
        @Override
        public boolean apply(String value, String threshold) {
            Double val = tryParseDouble(value);
            Double th = tryParseDouble(threshold);
            return val != null && th != null && val <= th;
        }
    },
    EQ("eq", "==") {
        @Override
        public boolean apply(String value, String threshold) {
            // 自动识别为数字、布尔、字符串相等
            if (isBoolean(value) && isBoolean(threshold)) {
                return Boolean.parseBoolean(value) == Boolean.parseBoolean(threshold);
            }
            Double val = tryParseDouble(value);
            Double th = tryParseDouble(threshold);
            if (val != null && th != null) {
                return val.doubleValue() == th.doubleValue();
            }
            return value.equals(threshold); // 字符串比较
        }
    },
    RANGE("range", "range") {
        @Override
        public boolean apply(String value, String threshold) {
            if (threshold == null || value == null) return false;
            String[] parts = threshold.split("-");
            if (parts.length != 2) return false;
            Double val = tryParseDouble(value);
            Double min = tryParseDouble(parts[0].trim());
            Double max = tryParseDouble(parts[1].trim());
            return val != null && min != null && max != null && val >= min && val <= max;
        }
    },
    CUSTOM("custom", "custom") {
        @Override
        public boolean apply(String value, String threshold) {
            // TODO: 自定义规则实现
            return false;
        }
    };

    private final String code;
    private final String symbol;

    OperationType(String code, String symbol) {
        this.code = code;
        this.symbol = symbol;
    }

    @Override
    @JsonValue
    public String getValue() {
        return this.code;
    }

    public String getSymbol() {
        return this.symbol;
    }

    public static OperationType fromCode(String code) {
        for (OperationType type : values()) {
            if (type.code.equalsIgnoreCase(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("Invalid operator code: " + code);
    }

    /**
     * 判断是否满足条件
     * @param actualValue 实际值
     * @param threshold 阈值
     * @return
     */
    public abstract boolean apply(String actualValue, String threshold);

    /**
     * 尝试将字符串解析为Double，如果解析失败则返回null
     * @param str
     * @return
     */
    protected static Double tryParseDouble(String str) {
        if (str == null) return null;
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 判断字符串是否为布尔类型
     * @param str
     * @return
     */
    protected static boolean isBoolean(String str) {
        return "true".equalsIgnoreCase(str) || "false".equalsIgnoreCase(str);
    }
}
