package com.sonic.bear.utils;

import java.util.Random;
import java.util.UUID;

public class UUIDGenerator {

    private static final String SPLITOR = "-";
    private static final String BLANK = "";

    public static String BASE_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public static String[] BASE_CHARS = 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", "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "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"};

    /** UUID 32位 */
    public static String generateLongUuid() {
        return generateLongUuid(true);
    }

    /** UUID 32位 */
    public static String generateLongUuid(boolean isUpperCase) {
        String uuid = UUID.randomUUID().toString().replace(SPLITOR, BLANK);
        if (isUpperCase) {
            return uuid.toUpperCase();
        }
        return uuid.toLowerCase();
    }

    /** UUID转为8位 */
    public static String generateShortUuid() {
        StringBuilder shortBuffer = new StringBuilder(8);
        String uuid = UUID.randomUUID().toString().replace(SPLITOR, BLANK);
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(BASE_CHARS[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public static String generate16Uuid() {
        StringBuilder shortBuffer = new StringBuilder(16);
        String uuid = UUID.randomUUID().toString().replace(SPLITOR, BLANK);
        for (int i = 0; i < 16; i++) {
            int start = i * 2;
            int end = Math.min(i * 2 + 4, 32);
            String str = uuid.substring(start, end);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(BASE_CHARS[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    /**
     * 生成32位的token
     * @return
     */
    public static String generate32Uuid() {
        return generate16Uuid() + generate16Uuid();
    }

    /**
     * 生成48位的token
     * @return
     */
    public static String generate48Uuid() {
        return generate16Uuid() + generate16Uuid() + generate16Uuid();
    }

    /**
     * 生成64位的token
     * @return
     */
    public static String generate64Uuid() {
        return generate16Uuid() + generate16Uuid() + generate16Uuid() + generate16Uuid();
    }

    /** length表示生成字符串的长度 */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(BASE_STRING.length());
            sb.append(BASE_STRING.charAt(number));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 30000; i++) {
            System.out.println("D" + generateShortUuid() + getRandomString(0));
        }
        System.out.println(System.currentTimeMillis() - start);
    }

}