package com.sonic.cow.utils;



import com.sonic.cow.domain.bo.LimitBo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author code
 * @Description TODO
 * @Date 2024/3/7 13:37
 * @Version 1.0
 */
public class CheckUtils {

    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", "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"};

    /**
     * 计算小时分钟后组装异常信息
     * @param bo
     * @return
     */
    public static String getErrorMessage(LimitBo bo, String message) {
        if(!bo.getLimitBl()) {
            return "";
        }
        //计算得到总的分钟数
        Long expTime = bo.getExpTime() / 60;
        //计算小时、分钟
        Long expHour = expTime / 60;
        //计算得到剩余的分钟数
        Long expMinutes = expTime % 60;
        expHour = expHour <= 0 ? 0 : expHour;
        expMinutes = expMinutes <= 0 ? 0 : expMinutes;
        if(expHour <= 0 && expMinutes <= 0) {
            expMinutes = 1L;
        }
        return String.format(message, expHour, expMinutes);
    }

    /**
     * 生成有时间戳编码的30位的UUID
     * @return
     */
    public static String genUUIDAnEncode() {
        //获取当前时间戳
        Long time = System.currentTimeMillis() / 1000;
        //将时间戳按照每2个字符进行拆分
        List<String> timeList = splitTimestamp(time);
        //拼接出字符串的UUID，30位
        StringBuffer uuid = new StringBuffer();
        uuid.append(randomUUID(2)).append(timeList.get(0));
        uuid.append(randomUUID(2)).append(timeList.get(1));
        uuid.append(randomUUID(2)).append(timeList.get(2));
        uuid.append(randomUUID(2)).append(timeList.get(3));
        uuid.append(randomUUID(2)).append(timeList.get(4));
        return uuid.toString();
    }

    /**
     * 反解出时间戳
     * @param uuid
     * @return
     */
    public static Long uuidDecodeToTime(String uuid) {
        //从指定位置截取数字部分并转换为长整型
        String digits = uuid.substring(2, 4) + uuid.substring(6, 8) + uuid.substring(10, 12) +
                uuid.substring(14, 16) + uuid.substring(18, 20);
        return Long.valueOf(digits);
    }

    /**
     * 生成指定长度的随机字符串
     * @param size
     * @return
     */
    public static String randomUUID(int size) {
        Random random = new Random();
        StringBuffer randomStr = new StringBuffer();
        //生成10位的随机字符
        for (int i = 0; i < size; i++) {
            int index = random.nextInt(52);
            randomStr.append(BASE_CHARS[index]);
        }
        return randomStr.toString();
    }

    /**
     * 将时间戳按照每2位进行分段
     * @param timestamp
     * @return
     */
    public static List<String> splitTimestamp(long timestamp) {
        String timestampStr = String.valueOf(timestamp);
        List<String> timestampList = new ArrayList<>();
        for (int i = 0; i < timestampStr.length(); i += 2) {
            timestampList.add(timestampStr.substring(i, Math.min(i + 2, timestampStr.length())));
        }
        return timestampList;
    }


}
