package com.jiayue.ssi.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Collections;
import java.util.List;

/**
* 初始/重置密码生成
*
* @author xsl
* @since 2023/08/04
*/
public class RandomRulePwd {
    private static final String upperStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String lowerStr="abcdefghijklmnopqrstuvwxyz";
    private static final String numStr="1234567890";
    private static final String specialStr="!@$%^*()_-+=?/";


    public static String createPwd(String rule,int strLength){
        String randomString="";
        List<String> strList = Convert.toList(String.class,rule);
        Collections.sort(strList);
        rule = String.join(",",strList);
        if ("A,B,C".equals(rule)){
            while (!(StrUtil.containsAny(randomString,upperStr.split("")) && StrUtil.containsAny(randomString,lowerStr.split("")) && StrUtil.containsAny(randomString,numStr.split("")))){
                randomString = cn.hutool.core.util.RandomUtil.randomString(upperStr + lowerStr + numStr, strLength);
            }
        }
        else if ("A,B,D".equals(rule)){
            while (!(StrUtil.containsAny(randomString,upperStr.split("")) && StrUtil.containsAny(randomString,lowerStr.split("")) && StrUtil.containsAny(randomString,specialStr.split("")))){
                randomString = cn.hutool.core.util.RandomUtil.randomString(upperStr + lowerStr + specialStr, strLength);
            }
        }
        else if ("A,C,D".equals(rule)){
            while (!(StrUtil.containsAny(randomString,upperStr.split("")) && StrUtil.containsAny(randomString,numStr.split("")) && StrUtil.containsAny(randomString,specialStr.split("")))){
                randomString = cn.hutool.core.util.RandomUtil.randomString(upperStr + numStr + specialStr, strLength);
            }
        }
        else if ("B,C,D".equals(rule)){
            while (!(StrUtil.containsAny(randomString,lowerStr.split("")) && StrUtil.containsAny(randomString,numStr.split("")) && StrUtil.containsAny(randomString,specialStr.split("")))){
                randomString = RandomUtil.randomString(lowerStr + numStr + specialStr, strLength);
            }
        }
        else if ("A,B,C,D".equals(rule)){
            while (!(StrUtil.containsAny(randomString,upperStr.split("")) && StrUtil.containsAny(randomString,lowerStr.split("")) && StrUtil.containsAny(randomString,numStr.split("")) && StrUtil.containsAny(randomString,specialStr.split("")))){
                randomString = RandomUtil.randomString(upperStr + lowerStr + numStr + specialStr, strLength);
            }
        }
        return randomString;
    }
}
