package org.springblade.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 腾讯验签工具类
 *
 * @author :王建
 * @since :2024-02-28 10:44
 */
public class JsonDataSecurity {

    //    private static final String SECRET_KEY = "YourSecretKey123";
    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 对JSON对象按照键名排序并加密
    public static String encryptJsonData(Object jsonData, String salt) throws Exception {
        String jsonString = objectMapper.writeValueAsString(jsonData);
        Map<String, Object> sortedMap = sortJsonObject(jsonString);
        String sortedJsonString = objectMapper.writeValueAsString(sortedMap);
        return encryptString(sortedJsonString, salt);
    }

    // 解密并反序列化为JSON对象
//    public static Map<String, Object> decryptAndParseJsonData(String encryptedData) throws Exception {
//        String decryptedString = decryptString(encryptedData);
//        return objectMapper.readValue(decryptedString, HashMap.class);
//    }

    // 按照键名字母顺序对JSON对象进行排序
    private static Map<String, Object> sortJsonObject(String jsonString) throws Exception {
        Map<String, Object> jsonMap = objectMapper.readValue(jsonString, HashMap.class);
        Map<String, Object> sortedMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        sortedMap.putAll(jsonMap);
        return sortedMap;
    }

    // 字符串加密方法
    private static String encryptString(String valueToEnc, String salt) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        byte[] encryptedValue = cipher.doFinal(valueToEnc.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedValue);
    }

    // 字符串解密方法
//    private static String decryptString(String encryptedValue) throws Exception {
//        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8), "AES");
//        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
//
//        byte[] original = Base64.getDecoder().decode(encryptedValue);
//        byte[] decryptedValue = cipher.doFinal(original);
//        return new String(decryptedValue, StandardCharsets.UTF_8);
//    }

//    public static void main(String[] args) throws Exception {
//        JsonDataSecurity xx = new JsonDataSecurity();
//
//        List<String> l = new ArrayList<>();
//        l.add("1");
//        l.add("2");
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("userId", "sdfsdfsdfd");
//        jsonObject.put("authCode", l);
//        String encryptStr = JsonDataSecurity.encryptJsonData(jsonObject);
//        System.out.println("对JSON对象按照键名排序并加密:"+encryptStr);
//
//        Map<String, Object>  json =  JsonDataSecurity.decryptAndParseJsonData(encryptStr);
//        System.out.println("解密并反序列化为JSON对象"+ JSONUtil.toJsonStr(json));
//    }

}
