package org.example.mybatisplus.model.auto;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Map;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用逻辑模型
 *
 * @author jimmy
 * @version 1.0.0
 * @date 2024/04/13 13:13
 */
@Data
@NoArgsConstructor
public class CommonLogicModel implements Serializable {
    private static final long serialVersionUID = -6389402890130074848L;

    /**
     * 规则字段类型
     */
    private CommonType.LogicType logic;

    /**
     * 指标值
     */
    private String targetValue;

    public static CommonLogicModel of(CommonType.LogicType logic, String targetValue) {
        return new CommonLogicModel(logic, targetValue);
    }

    public static CommonLogicModel of(Map<String, Object> params) {
        return new CommonLogicModel(Enum.valueOf(CommonType.LogicType.class, params.get("logic").toString()),
            params.get("targetValue").toString());
    }

    protected CommonLogicModel(CommonType.LogicType logic, String targetValue) {
        this.logic = logic;
        this.targetValue = targetValue;
    }

    @SuppressWarnings({"checkstyle:MethodName", "checkstyle:ReturnCount"})
    protected boolean calculate_internal(String value) {
        switch (logic) {
            case CONTAIN:
                final String[] keywords = targetValue.split(StrUtil.COMMA);
                return StrUtil.containsAny(value, keywords);
            case NOT_CONTAIN:
                final String[] notKeywords = targetValue.split(StrUtil.COMMA);
                return !StrUtil.containsAny(value, notKeywords);
            case EQ_ANY:
                final String[] eqKeys = targetValue.split(StrUtil.COMMA);
                return StrUtil.equalsAny(value, eqKeys);
            case GT:
                BigDecimal val4 = new BigDecimal(value);
                BigDecimal target4 = new BigDecimal(targetValue);
                return val4.compareTo(target4) > 0;
            case GE:
                BigDecimal val = new BigDecimal(value);
                BigDecimal target = new BigDecimal(targetValue);
                return val.compareTo(target) >= 0;
            case NE:
                return !targetValue.equals(value);
            case EQ:
                if (NumberUtil.isNumber(value)) {
                    return new BigDecimal(value).compareTo(new BigDecimal(targetValue)) == 0;
                }
                return targetValue.equals(value);
            case LT:
                BigDecimal val1 = new BigDecimal(value);
                BigDecimal target1 = new BigDecimal(targetValue);
                return val1.compareTo(target1) < 0;
            case LE:
                BigDecimal val5 = new BigDecimal(value);
                BigDecimal target5 = new BigDecimal(targetValue);
                return val5.compareTo(target5) <= 0;
            case BETWEEN:
                final String[] btVals = targetValue.split(StrUtil.COMMA);
                BigDecimal start = new BigDecimal(btVals[0]);
                BigDecimal end = new BigDecimal(btVals[1]);
                BigDecimal val3 = new BigDecimal(value);
                return val3.compareTo(start) >= 0 && val3.compareTo(end) <= 0;
            default:
                return false;
        }
    }
}
