package cn.pluss.platform.util;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.*;

import static cn.pluss.platform.util.RSAUtil.encryptByPublicKey;

/**
 * AES加密工具类
 *
 * @author ACGkaka
 * @since 2021-06-18 19:11:03
 */
public class TokenUtil {

    //APPID
    private static final String APP_ID = "ZF544";
    //USERCODE
    private static final String USER_CODE = "ZF544";
    //APPSECRET
    private static final String APP_SECRET = "2022bsjZF544GAH";
    //sn
    private static final String SN = "BSJQG01YJ0001";
    public static final String PUBLIC_KEY ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCKjak6oTfMxO3hWZ8CnMgUxDXHtsHQLZf4GgxzbjVYIN93UtuycdHypfUEh18s8sWwHRvBfeJ4mG0QbLazMphUY7ju9ox/qyB+tmJtZO5fCUi1StnSh17fJRw2kBlD9dKtBBKHJw0PXC/d6ATRtbbbFerzgLtUz70SvUxooUQHtQIDAQAB";
    /**
     * 获取TOKEN值
     * @param timestamp 时间戳，13位
     * @param requestId 请求ID，自定义
     * @return
     */
    public static Map<String, String> getToken(String timestamp, String requestId,String appId) {
        String token = "";
        String encode = "";
        SortedMap<String, Object> map = new TreeMap();
        map.put("appId", appId);
        map.put("timestamp", timestamp);
        map.put("requestId", requestId);
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            Object value = next.getValue();
            token += key + value;
            encode += key + "=" + value + "&";
        }
        System.out.println(token);
        Map<String, String> finalMap = new HashMap<>();
        finalMap.put("ENCODE",encode);
        System.out.println(token + APP_SECRET);
        finalMap.put("TOKEN", MD5Util.md5(token + APP_SECRET).toUpperCase());
        return finalMap;
    }
    public static Map<String, String> getToken(String timestamp, String requestId, String appId, String reqData)throws Exception {
        System.out.println(appId);
        System.out.println(reqData);
        SortedMap<String, Object> map = new TreeMap();
        map.put("appId", appId);
        map.put("timestamp", timestamp);
        map.put("requestId", requestId);
        map.put("reqData",convertObject(JSONObject.parseObject(reqData,Map.class)));
        String encode= convertMap(map);
        Map<String, String> finalMap = new HashMap<>();
        finalMap.put("ENCODE",encode);

        String t=encode.concat(APP_SECRET);
        finalMap.put("TOKEN", MD5Util.md5(t).toUpperCase());
        return finalMap;
    }


    public static  String convertObject(Map<String,Object> map){
        String[] sortedKeys = map.keySet().toArray(new String[]{});
        Arrays.sort(sortedKeys);// 排序请求参数
        StringBuilder s2 = new StringBuilder();
        s2.append("{");
        for (String key : sortedKeys) {
            s2.append("\"").append(key).append("\"").append(":").append("\"").append(map.get(key)).append("\"").append(",");
        }
        s2.deleteCharAt(s2.length() - 1);
        s2.append("}");
        return s2.toString();
    }


    public static  String convertMap(Map<String,Object> map){
        String[] sortedKeys = map.keySet().toArray(new String[]{});
        Arrays.sort(sortedKeys);// 排序请求参数
        StringBuilder s2 = new StringBuilder();
        for (String key : sortedKeys) {
            s2.append(key).append(map.get(key));
        }
        return s2.toString();
    }



    public static void main(String[] args) throws Exception{
        String s = String.valueOf(System.currentTimeMillis());
        System.out.println(s);
        String s1 = UUID.randomUUID().toString();
        System.out.println(s1);
        String param = "{\"orderNumber\":\"LKL_Z_20230612170005985\"}";
        Map<String, String> token = getToken(s,s1 , APP_ID,param);
        System.out.println(token);

        System.out.println("");


    }

}
