package com.ho.common.tools.util;

import cn.hutool.core.convert.Convert;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;

/**
 * @Description mqtt token工具类
 * Author yule
 * Date 2022/12/7 11:14
 */
@Component
@Slf4j
public class MqttUtil {

//    @Value("${secret.loginSecret}")
//    String loginSecret;

    public String getToken(){

        String token = null;
        //对当前时间进行加密
        long time = System.currentTimeMillis() / 300000;

        String loginSecret = "6131231@#42a765#";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKey = new SecretKeySpec(loginSecret.getBytes("utf-8"), "HmacSHA256");
            sha256_HMAC.init(secretKey);
            byte[] bytes = sha256_HMAC.doFinal(String.valueOf(time).getBytes("utf-8"));
            String hexStr = Convert.toHex(bytes);
            //截取到加密后的前20个字符
            token =  "v1:" + hexStr.substring(0, 20);

        }  catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (token == null) {
              throw  new BusinessException(BaseResponseCode.MQTT_TOKEN_GENERATE_FALL);
            }
        }
        return token;
    }

    public String getToken(long time){

        String token = null;
        //对当前时间进行加密
        String loginSecret = "6131231@#42a765#";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKey = new SecretKeySpec(loginSecret.getBytes("utf-8"), "HmacSHA256");
            sha256_HMAC.init(secretKey);
            byte[] bytes = sha256_HMAC.doFinal(String.valueOf(time).getBytes("utf-8"));
            String hexStr = Convert.toHex(bytes);
            //截取到加密后的前20个字符
            token =  "v1:" + hexStr.substring(0, 20);

        }  catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (token == null) {
                throw  new BusinessException(BaseResponseCode.MQTT_TOKEN_GENERATE_FALL);
            }
        }
        return token;
    }

    /**
     * 随机生成cmdId(mqtt的命令编码)
     */
    public String getCmdId(){
        ArrayList<Character> letterList = new ArrayList<>();
        for (int i = 'a'; i <='z' ; i++) {
            letterList.add((char)i);
        }for (int i = 'A'; i <='Z' ; i++) {
            letterList.add((char)i);
        }
        char[] letter = new char[52];
        for (int i = 0; i < letterList.size(); i++) {
            letter[i] = letterList.get(i);
        }

        ArrayList<Character> numList = new ArrayList<>();
        for (int i = '0'; i <= '9'; i++) {
            numList.add((char)i);
        }
        char[] num = new char[10];
        for (int i = 0; i < numList.size(); i++) {
            num[i] = numList.get(i);
        }
        String letterString = RandomStringUtils.random(5,letter);
        String numString = RandomStringUtils.random(5,num);
        return letterString+numString;
    }

    public static void main(String[] args) {
        MqttUtil mqttUtil = new MqttUtil();
        String token = mqttUtil.getToken();
        log.info(token);
    }
}
