package com.cxyxiaomo.epp.PageTable.utils;

import com.alibaba.fastjson2.JSONObject;

/**
 * Element Plus 表单验证
 * GiHub refer: https://github.com/yiminghe/async-validator
 */
public class FieldRuleBuilder {

    private String fieldDisplayName;
    private JSONObject rules;

    public static FieldRuleBuilder create(String fieldDisplayName) {
        FieldRuleBuilder builder = new FieldRuleBuilder();
        builder.rules = new JSONObject();
        builder.fieldDisplayName = fieldDisplayName;
        // 默认在 blur 时触发验证
        // builder.rules.put("trigger", "blur");
        builder.rules.put("trigger", "change");
        return builder;
    }

    public FieldRuleBuilder trigger(String trigger) {
        rules.put("trigger", trigger);
        return this;
    }

    public FieldRuleBuilder message(String message) {
        rules.put("message", message);
        return this;
    }

    public FieldRuleBuilder required() {
        rules.put("required", true);
        rules.put("message", fieldDisplayName + "不能为空");
        return this;
    }

    public FieldRuleBuilder pattern(String regEx) {
        rules.put("pattern", regEx);
        rules.put("message", fieldDisplayName + "输入不符合要求，请检查");
        return this;
    }

    // public FieldRuleBuilder number() {
    //     // rules.put("type", "number"); // 字符串类型的数字匹配不上
    //     rules.put("regexp", "^\\d+?$");
    //     rules.put("message", fieldDisplayName + "必须为数字");
    //     return this;
    // }

    public FieldRuleBuilder min(Integer min) {
        rules.put("min", min);
        rules.put("message", fieldDisplayName + "过短");
        return this;
    }

    public FieldRuleBuilder max(Integer max) {
        rules.put("max", max);
        rules.put("message", fieldDisplayName + "过长");
        return this;
    }

    // 字符串类型长度范围
    public FieldRuleBuilder minMax(Integer min, Integer max) {
        rules.put("min", min);
        rules.put("max", max);
        rules.put("message", fieldDisplayName + "长度应在 " + min + " 至 " + max + " 之间");
        return this;
    }

    // 数字类型数值范围
    // public FieldRuleBuilder range(Integer min, Integer max) {
    //     // rules.put("min", min);
    //     // rules.put("max", max);
    //     rules.put("message", fieldDisplayName + "应大于 " + min + " 且小于 " + max);
    //     return this;
    // }

    // // 数字类型数字个数
    // public FieldRuleBuilder price(Integer minLen, Integer maxLen) {
    //     rules.put("regexp", "^\\d{" + minLen + "," + maxLen + "}(\\.\\d{1,2})?$");
    //     rules.put("message", fieldDisplayName + "应大于等于 " + Math.pow(10, minLen - 1) + " ，小于等于 " + (Math.pow(10, maxLen) - 1) + " 且小数位数不超过 2 位");
    //     return this;
    // }

    // public FieldRuleBuilder regexp(String regexp) {
    //     // rules.put("type", "regexp");
    //     rules.put("pattern", regexp);
    //     rules.put("message", fieldDisplayName + "输入不符合要求，请检查");
    //     return this;
    // }

    public FieldRuleBuilder length(Integer len) {
        rules.put("min", len);
        rules.put("max", len);
        rules.put("message", fieldDisplayName + "位数不对，应为 " + len + " 位");
        return this;
    }

    public FieldRuleBuilder add(String field, Object value) {
        rules.put(field, value);
        return this;
    }

    public JSONObject build() {
        return rules;
    }
}
