package com.czg.utils;

import cn.hutool.core.lang.id.NanoId;
import cn.hutool.core.util.IdUtil;
import com.czg.enums.OrderNoPrefixEnum;

/**
 * @author ww
 * @description
 */
public class CzgRandomUtils {

    private static final char[] DEFAULT_ALPHABET =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final char[] BASE_NUMBER =
            "0123456789".toCharArray();

    private static final char[] BASE_NUMBER_NO_ZERO =
            "123456789".toCharArray();

    /**
     * 默认长度
     */
    public static final int DEFAULT_SIZE = 12;


    /**
     * 随机生成指定长度的字符串
     *
     * @return 随机字符串
     */
    public static String randomString() {
        return NanoId.randomNanoId(null, DEFAULT_ALPHABET, DEFAULT_SIZE);
    }


    /**
     * 随机生成指定长度的字符串
     *
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomString(int length) {
        return NanoId.randomNanoId(null, DEFAULT_ALPHABET, length);
    }


    /**
     * 随机生成指定长度的数字 不带前缀
     *
     * @param length        订单号长度
     * @param isFirstNoZero 是否第一个字符不能为0
     * @return 订单号
     */
    public static String randomNumber(int length, boolean isFirstNoZero) {
        if (isFirstNoZero) {
            return NanoId.randomNanoId(null, BASE_NUMBER_NO_ZERO, 1) + NanoId.randomNanoId(null, BASE_NUMBER, length - 1);
        }
        return NanoId.randomNanoId(null, BASE_NUMBER, length);
    }

    /**
     * 随机生成指定长度的数字
     *
     * @param prefixEnum    订单号前缀枚举
     * @param length        订单号长度
     * @param isFirstNoZero 是否第一个字符不能为0
     * @return 订单号
     */
    public static String randomNumber(OrderNoPrefixEnum prefixEnum, int length, boolean isFirstNoZero) {
        if (isFirstNoZero) {
            return prefixEnum.getValue() + NanoId.randomNanoId(null, BASE_NUMBER_NO_ZERO, 1) + NanoId.randomNanoId(null, BASE_NUMBER, length - 1);
        }
        return prefixEnum.getValue() + NanoId.randomNanoId(null, BASE_NUMBER, length);
    }

    public static String snowflake(OrderNoPrefixEnum prefixEnum) {
        return prefixEnum.getValue() + IdUtil.getSnowflakeNextId();
    }

    public static String snowflake() {
        return IdUtil.getSnowflakeNextId() + "";
    }
}
