package com.ibeetl.admin.flow.utils;

public class RandomUtil {

    public final static int RANDOM_STRING_MODE = 0;

    public final static int LOWER_LETTER = 0;
    public final static int LOWER_UPPER_LETTER = 1;
    public final static int LOWER_UPPER_NUMBER = 2;
    public final static int LOWER_UPPER_SIGN = 3;
    public final static int NUMBER = 4;

    // 生成复杂码函数
    public static String random(int length, int mode) {
        String[] character;
        switch (mode) {
            case LOWER_LETTER:
                character = new String[]{"qwertyuiopasdfghjklzxcvbnm"};
                break;
            case LOWER_UPPER_LETTER:
                character = new String[]{"qwertyuiopasdfghjklzxcvbnm", "QWERTYUIOPASDFGHJKLZXCVBNM"};
                break;
            case LOWER_UPPER_NUMBER:
                character = new String[]{"qwertyuiopasdfghjklzxcvbnm", "QWERTYUIOPASDFGHJKLZXCVBNM", "0123456789"};
                break;
            default:
                character = new String[]{"qwertyuiopasdfghjklzxcvbnm", "QWERTYUIOPASDFGHJKLZXCVBNM", "0123456789", "~!@#$%^&*()_+{}|<>?:{}"};
                break;
        };

        char[] chs = new char[length];

        for (int i = 0; i < character.length; i++) {
            int idx = (int) (Math.random() * character[i].length());
            chs[i] = character[i].charAt(idx);
        }

        for (int i = character.length; i < length; i++) {
            int arrIdx = (int) (Math.random() * character.length);
            int strIdx = (int) (Math.random() * character[arrIdx].length());
            chs[i] = character[arrIdx].charAt(strIdx);
        }

        for (int i = 0; i < 1000; i++) {
            int idx1 = (int) (Math.random() * chs.length);
            int idx2 = (int) (Math.random() * chs.length);

            if (idx1 == idx2) {
                continue;
            }

            char tempChar = chs[idx1];
            chs[idx1] = chs[idx2];
            chs[idx2] = tempChar;
        }

        return new String(chs);
    }

    public static void main(String[] args) {
        // System.out.println(random(5, LOWER_LETTER));
        // System.out.println(random(5, LOWER_UPPER_LETTER));
        System.out.println(random(5, LOWER_UPPER_NUMBER));
        // System.out.println(random(5, LOWER_UPPER_SIGN));
    }
}
