package org.xyzh.common.utils.validation.method;

import java.util.regex.Pattern;

/**
 * @description 密码校验方法
 * @filename PasswordValidateMethod.java
 * @author yslg
 * @copyright yslg
 * @since 2025-11-02
 */
public class PasswordValidateMethod implements ValidateMethod {
    
    private final int minLength;
    private final int maxLength;
    private final Boolean requireUpperCase;
    private final Boolean requireLowerCase;
    private final Boolean requireDigit;
    private final Boolean requireSpecialChar;
    
    /**
     * @description 默认密码规则：6-20位，必须包含字母和数字
     */
    public PasswordValidateMethod() {
        this(6, 20, false, false, true, false);
    }
    
    /**
     * @description 自定义密码规则
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @param requireUpperCase 是否需要大写字母
     * @param requireLowerCase 是否需要小写字母
     * @param requireDigit 是否需要数字
     * @param requireSpecialChar 是否需要特殊字符
     */
    public PasswordValidateMethod(int minLength, int maxLength, 
                                   Boolean requireUpperCase, Boolean requireLowerCase,
                                   Boolean requireDigit, Boolean requireSpecialChar) {
        this.minLength = minLength;
        this.maxLength = maxLength;
        this.requireUpperCase = requireUpperCase;
        this.requireLowerCase = requireLowerCase;
        this.requireDigit = requireDigit;
        this.requireSpecialChar = requireSpecialChar;
    }
    
    @Override
    public Boolean validate(Object value) {
        if (value == null || !(value instanceof String)) {
            return false;
        }
        
        String password = (String) value;
        
        // 长度校验
        if (password.length() < minLength || password.length() > maxLength) {
            return false;
        }
        
        // 大写字母校验
        if (requireUpperCase && !Pattern.compile("[A-Z]").matcher(password).find()) {
            return false;
        }
        
        // 小写字母校验
        if (requireLowerCase && !Pattern.compile("[a-z]").matcher(password).find()) {
            return false;
        }
        
        // 数字校验
        if (requireDigit && !Pattern.compile("[0-9]").matcher(password).find()) {
            return false;
        }
        
        // 特殊字符校验
        if (requireSpecialChar && !Pattern.compile("[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>/?]").matcher(password).find()) {
            return false;
        }
        
        return true;
    }
    
    @Override
    public String getErrorMessage() {
        StringBuilder msg = new StringBuilder("密码必须是");
        msg.append(minLength).append("-").append(maxLength).append("位");
        
        if (requireUpperCase || requireLowerCase || requireDigit || requireSpecialChar) {
            msg.append("，且包含");
            Boolean first = true;
            
            if (requireUpperCase) {
                msg.append("大写字母");
                first = false;
            }
            if (requireLowerCase) {
                if (!first) msg.append("、");
                msg.append("小写字母");
                first = false;
            }
            if (requireDigit) {
                if (!first) msg.append("、");
                msg.append("数字");
                first = false;
            }
            if (requireSpecialChar) {
                if (!first) msg.append("、");
                msg.append("特殊字符");
            }
        }
        
        return msg.toString();
    }
    
    @Override
    public String getName() {
        return "密码校验";
    }
}

