package com.jiayue.ssi.util;

import com.jiayue.ssi.backenum.PasswordRuleEnum;

/**
 * 密码规则验证工具
 *
 * @author xsl
 * @since 2023/06/06
 */
public class PasswordRuleUtil {
    /**
     * 根据规则验证密码
     *
     * @param pwdRule 密码规则，多个规则用逗号分隔
     * @param pwd     密码
     * @return
     */
    public static String ruleRegx(String pwdRule, String pwd) {
        String[] rules = pwdRule.split(",");
        if (rules.length<3){
            return "密码规则至少3种以上组合！";
        }
        String retName = "";
        for (int i = 0; i < rules.length; i++) {
            // 获取正则
            String regx = PasswordRuleEnum.valueOf(rules[i]).getMessage();
            if (regx == null) {
                // 正则不存在，传入的校验规则有问题
                return "密码规则配置有问题，不能校验！";
            } else {
                retName = retName + PasswordRuleEnum.valueOf(rules[i]).getCode() + "、";
            }
        }

        for (int i = 0; i < rules.length; i++) {
            if (!pwd.matches(PasswordRuleEnum.valueOf(rules[i]).getMessage())){
                return "密码不满足"+retName.substring(0,retName.length()-1)+"组合";
            }
        }
        return "true";
    }

    public static void main(String[] args) {
        System.out.println(ruleRegx("A,B,C", "abcde111W"));
    }
}
