package org.xyzh.common.utils.validation;

import org.xyzh.common.utils.validation.method.ValidateMethod;
import org.xyzh.common.utils.validation.method.ValidateMethodType;

import java.util.function.Predicate;

/**
 * @description 校验参数对象，定义字段的校验规则
 * @filename ValidationParam.java
 * @author yslg
 * @copyright yslg
 * @since 2025-11-02
 */
public class ValidationParam {
    
    /**
     * @description 字段名称
     */
    private String fieldName;
    
    /**
     * @description 字段中文名称（用于错误提示）
     */
    private String fieldLabel;
    
    /**
     * @description 是否必传
     */
    private Boolean required;
    
    /**
     * @description 字段类型
     */
    private Class<?> fieldType;
    
    /**
     * @description 最小长度（字符串）
     */
    private Integer minLength;
    
    /**
     * @description 最大长度（字符串）
     */
    private Integer maxLength;
    
    /**
     * @description 最小值（数字）
     */
    private Number minValue;
    
    /**
     * @description 最大值（数字）
     */
    private Number maxValue;
    
    /**
     * @description 正则表达式
     */
    private String pattern;
    
    /**
     * @description 正则表达式描述（用于错误提示）
     */
    private String patternDesc;
    
    /**
     * @description 自定义校验函数
     */
    private Predicate<Object> customValidator;
    
    /**
     * @description 自定义校验失败消息
     */
    private String customErrorMessage;
    
    /**
     * @description 是否允许为空字符串（默认不允许）
     */
    private Boolean allowEmpty = false;
    
    /**
     * @description 校验方法（使用预定义的校验方法）
     */
    private ValidateMethod validateMethod;
    
    /**
     * @description 校验方法类型枚举
     */
    private ValidateMethodType validateMethodType;
    
    /**
     * @description 校验方法配置参数（用于需要自定义参数的校验方法）
     */
    private Object[] methodParams;

    // 私有构造函数，使用Builder模式
    private ValidationParam() {
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getFieldLabel() {
        return fieldLabel;
    }

    public Boolean isRequired() {
        return required;
    }

    public Class<?> getFieldType() {
        return fieldType;
    }

    public Integer getMinLength() {
        return minLength;
    }

    public Integer getMaxLength() {
        return maxLength;
    }

    public Number getMinValue() {
        return minValue;
    }

    public Number getMaxValue() {
        return maxValue;
    }

    public String getPattern() {
        return pattern;
    }

    public String getPatternDesc() {
        return patternDesc;
    }

    public Predicate<Object> getCustomValidator() {
        return customValidator;
    }

    public String getCustomErrorMessage() {
        return customErrorMessage;
    }

    public Boolean isAllowEmpty() {
        return allowEmpty;
    }
    
    public ValidateMethod getValidateMethod() {
        return validateMethod;
    }
    
    public ValidateMethodType getValidateMethodType() {
        return validateMethodType;
    }
    
    public Object[] getMethodParams() {
        return methodParams;
    }

    /**
     * @description Builder类用于构建ValidationParam对象
     */
    public static class Builder {
        private ValidationParam param = new ValidationParam();

        public Builder fieldName(String fieldName) {
            param.fieldName = fieldName;
            return this;
        }

        public Builder fieldLabel(String fieldLabel) {
            param.fieldLabel = fieldLabel;
            return this;
        }

        public Builder required(Boolean required) {
            param.required = required;
            return this;
        }

        public Builder required() {
            param.required = true;
            return this;
        }

        public Builder fieldType(Class<?> fieldType) {
            param.fieldType = fieldType;
            return this;
        }

        public Builder minLength(Integer minLength) {
            param.minLength = minLength;
            return this;
        }

        public Builder maxLength(Integer maxLength) {
            param.maxLength = maxLength;
            return this;
        }

        public Builder minValue(Number minValue) {
            param.minValue = minValue;
            return this;
        }

        public Builder maxValue(Number maxValue) {
            param.maxValue = maxValue;
            return this;
        }

        public Builder pattern(String pattern) {
            param.pattern = pattern;
            return this;
        }

        public Builder patternDesc(String patternDesc) {
            param.patternDesc = patternDesc;
            return this;
        }

        public Builder customValidator(Predicate<Object> customValidator) {
            param.customValidator = customValidator;
            return this;
        }

        public Builder customErrorMessage(String customErrorMessage) {
            param.customErrorMessage = customErrorMessage;
            return this;
        }

        public Builder allowEmpty(Boolean allowEmpty) {
            param.allowEmpty = allowEmpty;
            return this;
        }
        
        public Builder validateMethod(ValidateMethod validateMethod) {
            param.validateMethod = validateMethod;
            return this;
        }
        
        public Builder validateMethod(ValidateMethodType methodType) {
            param.validateMethodType = methodType;
            return this;
        }
        
        public Builder validateMethod(ValidateMethodType methodType, Object... params) {
            param.validateMethodType = methodType;
            param.methodParams = params;
            return this;
        }

        public ValidationParam build() {
            if (param.fieldName == null || param.fieldName.isEmpty()) {
                throw new IllegalArgumentException("fieldName不能为空");
            }
            if (param.fieldLabel == null || param.fieldLabel.isEmpty()) {
                param.fieldLabel = param.fieldName;
            }
            return param;
        }
    }

    /**
     * @description 创建Builder对象
     * @return Builder
     */
    public static Builder builder() {
        return new Builder();
    }
}

