package com.jiayue.ssi.util;

import cn.hutool.crypto.SmUtil;

/**
* 自定义正则验证
*
* @author xsl
* @since 2023/03/16
*/
public class RegexUtil {

    private static String[] KEYBOARD_SLOPE_ARR={"1qaz", "2wsx", "3edc", "4rfv", "5tgb", "6yhn", "7ujm", "8ik,", "9ol.", "0p;/",
            "/;p0", ".lo9", ",ki8", "mju7", "nhy6", "bgt5", "vfr4", "cde3", "xsw2", "zaq1", "4esz", "5rdx", "6tfc",
            "7ygv", "8uhb", "9ijn", "0okm", "-pl,", "=[;.", ".;[=", ",lp-", "mko0", "nji9", "bhu8", "vgy7", "cft6",
            "xdr5", "zse4","!qaz", "@wsx", "#edc","$rfv", "%tgb", "^yhn", "&ujm", "*ik<", "(ol>", ")p:?", "?:p)",
            ">lo(", "<ki*", "mju&", "nhy^", "bgt%", "vfr$", "cde#", "xsw@", "zaq!", "$esz", "%rdx", "^tfc", "&ygv",
            "*uhb", "(ijn", ")okm", "_pl<", "+{:>", ">:{+", "<lp_", "mko)", "nji(", "bhu*", "vgy&", "cft^", "xdr%", "zse$"};


    private static String KEYBOARD_METADATA[] = {"qwertyuiopasdfghjklzxcvbnm",
            "1234567890-=",
            "=-0987654321",
            "qwertyuiop[]\\asdfghjkl;'zxcvbnm,./",
            "abcdefghijklmnopqrstuvwxyz",
            "zyxwvutsrqponmlkjihgfedcba",
    };
    /**
     * 含有连续4位相同的数字或字母
     * @param str
     * @return
     */
    public static boolean sameReg(String str){
        String regStr = "([0-9a-zA-Z])\1{3}";
        return str.matches(regStr);
    }

    /**
     * 含有4位键盘斜向方向连续的字符
     * @return
     */
    public static boolean keyboardSlopeArr(String password){
        //将所有输入字符转为小写
        String t_password = password.toLowerCase();
        int n = t_password.length();

        boolean flag = false;
        int arrLen = KEYBOARD_SLOPE_ARR.length;
        int limit_num = 4;

        for(int i=0; i+limit_num<=n; i++) {
            String str = t_password.substring(i, i+limit_num);
            String distinguishStr = password.substring(i, i+limit_num);
            for(int j=0; j<arrLen; j++) {
                String configStr = KEYBOARD_SLOPE_ARR[j];
                String revOrderStr = new StringBuffer(KEYBOARD_SLOPE_ARR[j]).reverse().toString();
                //检测包含字母(区分大小写)
                if (false) {
                    //考虑 大写键盘匹配的情况
                    String UpperStr = KEYBOARD_SLOPE_ARR[j].toUpperCase();
                    if((configStr.indexOf(distinguishStr) != -1) || (UpperStr.indexOf(distinguishStr) != -1)) {
                        flag = true;
                        return flag;
                    }
                    //考虑逆序输入情况下 连续输入
                    String revUpperStr = new StringBuffer(UpperStr).reverse().toString();
                    if((revOrderStr.indexOf(distinguishStr) != -1) || (revUpperStr.indexOf(distinguishStr) != -1)) {
                        flag = true;
                        return flag;
                    }
                }else {
                    if(configStr.indexOf(str) != -1) {
                        flag = true;
                        return flag;
                    }
                    //考虑逆序输入情况下 连续输入
                    if(revOrderStr.indexOf(str) != -1) {
                        flag = true;
                        return flag;
                    }
                }
            }
        }
        return flag;
    }
    /**
     * 含有4位横向连续的字母
     * @param str
     * @return
     */
    public static boolean keyboardHorizontalReg(String str){
        int size = str.length();
        String temp = "";
        int offet=0;
        for(String s : KEYBOARD_METADATA) {//提前定义的连续字符串
            offet=0;
            for(int i=4;i<=size;i++) {//直接从最大允许的字符开始截取字符
                temp = str.toLowerCase().substring(offet++,i);
                if(s.contains(temp)) {//出现连续字符，直接返回true了
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 大写字母、小写字母、数字、特殊字符三种以上的组合,长度8-20位
     * @param str
     * @return
     */
    public static boolean checkPwd(String str){
        String regStr = "^(?![a-zA-Z]+$)(?![a-z\\d]+$)(?![a-z!@#\\$%]+$)(?![A-Z\\d]+$)(?![A-Z!@#\\$%]+$)(?![\\d!@#\\$%]+$)[a-zA-Z\\d!@#\\$%]{8,20}$";
        return str.matches(regStr);
    }

    public static void main(String[] args) {
        // 不能含有连续4位相同的数字或字母
//        System.out.println(RegexUtil.sameReg("444"));
        // 不能含有4位连续的字母
//        System.out.println(RegexUtil.strResverReg("efgh"));
        // 不能含有4位键盘横向方向连续的字符
//        System.out.println(RegexUtil.keyboardSlopeArr());

//        System.out.println(keyboardSlopeArr(")P:?"));
//        System.out.println(keyboardHorizontalReg("abcde"));
        System.out.println(SmUtil.sm3("admin").toUpperCase());

//        System.out.println(RegexUtil.checkPwd("@xk12111rrrrrrrrrrrr"));
    }
}
