package cn.novalon.gym.manage.member.util;

import cn.novalon.gym.manage.member.config.WechatProperties;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.checkerframework.checker.units.qual.K;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * AES加密工具类
 * 
 * @author 付嘉
 * @date 2026-05-01
 */

@Slf4j
@Component
public class AesUtil {

    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";

    private static String KEY;
    private static String IV;

    @Autowired
    public void setWechatProperties(WechatProperties props) {
        KEY = props.getPhoneEncryption().getSecretKey();  // 从配置类读取
        IV = props.getPhoneEncryption().getIv();
        if(KEY == null || IV == null) throw new RuntimeException("请配置AES密钥和偏移量");
    }


    /**
     * AES 解密
     *
     * @param encryptedData 加密数据，Base64编码
     * @return 解密后的字符串
     */
    public static String decrypt(String encryptedData) {

        try {
            byte[] dataByte = Base64.getDecoder().decode(encryptedData);
            byte[] keyByte = Base64.getDecoder().decode(KEY);
            byte[] ivByte = Base64.getDecoder().decode(IV);

            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyByte, ALGORITHM);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] resultByte = cipher.doFinal(dataByte);
            return new String(resultByte, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("AES解密失败", e);
            throw new RuntimeException("解密失败", e);
        }
    }

    /**
     * AES 加密
     *
     * @param data 原始数据
     * @return Base64编码的加密数据
     */
    public static String encrypt(String data) {
        try {
            byte[] dataByte = data.getBytes(StandardCharsets.UTF_8);
            byte[] keyByte = Base64.getDecoder().decode(KEY);
            byte[] ivByte = Base64.getDecoder().decode(IV);

            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyByte, ALGORITHM);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] resultByte = cipher.doFinal(dataByte);
            return Base64.getEncoder().encodeToString(resultByte);
        } catch (Exception e) {
            log.error("AES加密失败", e);
            throw new RuntimeException("加密失败", e);
        }
    }
}
