package com.caimei.www.utils;

import java.util.Random;

public class RandomCodeGenerator {

    // TODO 与WWW的RandomCodeGenerator是重复的,后续需要删除www的RandomCodeGenerator
    private static char codeSequence[] = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
            'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7',
            '8', '9'
    };

    private static char intSequence[] = {
            '2', '3', '4', '5', '6', '7',
            '8', '9'
    };

    private static char stringSequence[] = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
            'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z'
    };

    private static char newStringSequence[] = {
            'A', 'a', 'B', 'b', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h',
            'L', 'N', 'n', 'Q', 'q', 'R', 'r', 'T', 't', 'Y', 'y'
    };
//	C,i，j，k，M,O,P,S,U,V,W,X,Z,l

    public static String generateCode(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < codeSequence.length && i < length; ++i) {
            sb.append(codeSequence[random.nextInt(codeSequence.length)]);
        }

        return sb.toString();
    }

    public static String generateCodeInt(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < intSequence.length && i < length; ++i) {
            sb.append(intSequence[random.nextInt(intSequence.length)]);
        }

        return sb.toString();
    }

    public static String generateCodeString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < stringSequence.length && i < length; ++i) {
            sb.append(stringSequence[random.nextInt(stringSequence.length)]);
        }

        return sb.toString();
    }


    public static String getRandomCharAndNumr(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static String generateAccount(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < newStringSequence.length && i < length; ++i) {
            sb.append(newStringSequence[random.nextInt(newStringSequence.length)]);
        }
        return sb.toString();
    }
}
