package com.cslg.ppa.common.utils;

import java.security.SecureRandom;
import java.util.Random;

public class RandomUtil {

    // 定义可用来生成邀请码的字符集
    private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    // 定义邀请码的长度
    private static final int CODE_LENGTH = 8;

    private static String[] STR_ARR = new String[] { "a", "b", "c", "d", "e",
            "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
            "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "0"};

    /**
     * 随机验证码
     * @return
     */
    public static String getSixRandom(){
        return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 5)));
    }

    /**
     * 随机生成code码的方法
     * @param length
     * @return
     */
    public static String generateRandomString(int length) {
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(STR_ARR[rand.nextInt(STR_ARR.length)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机邀请码的方法
     *
     * @return 生成的邀请码
     */
    public static String generateInvitationCode() {
        SecureRandom random = new SecureRandom();
        StringBuilder codeBuilder = new StringBuilder(CODE_LENGTH);

        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            codeBuilder.append(CHARACTERS.charAt(index));
        }

        return codeBuilder.toString();
    }
}
