package com.ssyf.third.bmiss.core;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加密配置类
 */
@Data
@Accessors(chain = true)
public class EncryptionConfig {
    private boolean enabled = true;
    private boolean enableRequestEncryption = true;
    private boolean enableResponseDecryption = false;
    private String algorithm = "AES-CBC";
    private String defaultKey;
    private String key;
    private String iv;
    private boolean signatureEnabled = true;
    private String signatureKey;
    private List<SecretConfig> secrets;
    private Map<String, Object> commonParams;
    
    public EncryptionConfig() {
        this.commonParams = new HashMap<>();
        this.commonParams.put("device", "wap");
        this.commonParams.put("source", "xinxiuweb");
        this.commonParams.put("appversion", "1.0.0");
        this.commonParams.put("signature_key", "asdasgfdwqew");
        this.commonParams.put("enable_signature", true);
    }
    
    public EncryptionConfig(Map<String, Object> data) {
        this();
        this.enabled = (boolean) data.getOrDefault("enabled", true);
        this.enableRequestEncryption = (boolean) data.getOrDefault("enable_request_encryption", true);
        this.enableResponseDecryption = (boolean) data.getOrDefault("enable_response_decryption", false);
        this.algorithm = (String) data.getOrDefault("algorithm", "AES-CBC");
        this.defaultKey = (String) data.getOrDefault("default_secret_key", "secret_key_1");
        this.key = (String) data.getOrDefault("key", "");
        this.iv = (String) data.getOrDefault("iv", "");
        this.signatureEnabled = (boolean) data.getOrDefault("signature_enabled", true);
        this.signatureKey = (String) data.getOrDefault("signature_key", "asdasgfdwqew");
        
        // 处理secrets配置
        if (data.containsKey("secrets")) {
            List<Map<String, String>> secretsData = (List<Map<String, String>>) data.get("secrets");
            this.secrets = new ArrayList<>();
            for (Map<String, String> secretData : secretsData) {
                SecretConfig secret = new SecretConfig();
                secret.setName(secretData.get("name"));
                secret.setKey(secretData.get("key"));
                secret.setIv(secretData.get("iv"));
                this.secrets.add(secret);
            }
        }
        
        if (data.containsKey("common_params")) {
            this.commonParams = (Map<String, Object>) data.get("common_params");
        }
    }
    
    public String getDefaultKey() {
        return defaultKey != null ? defaultKey : key;
    }
    
    /**
     * 根据密钥名称获取密钥配置
     * 
     * @param name 密钥名称
     * @return 密钥配置，如果不存在则返回null
     */
    public SecretConfig getSecretByName(String name) {
        if (secrets == null) {
            return null;
        }
        for (SecretConfig secret : secrets) {
            if (name.equals(secret.getName())) {
                return secret;
            }
        }
        return null;
    }
    
    /**
     * 获取默认密钥配置
     * 
     * @return 默认密钥配置
     */
    public SecretConfig getDefaultSecret() {
        if (secrets != null && !secrets.isEmpty()) {
            // 优先使用配置的默认密钥
            SecretConfig defaultSecret = getSecretByName(defaultKey);
            if (defaultSecret != null) {
                return defaultSecret;
            }
            // 如果没有找到默认密钥，使用第一个密钥
            return secrets.get(0);
        }
        // 如果没有配置多密钥，使用单个密钥配置
        SecretConfig secret = new SecretConfig();
        secret.setName("default");
        secret.setKey(key);
        secret.setIv(iv);
        return secret;
    }
    
    /**
     * 获取签名密钥
     * 
     * @return 签名密钥
     */
    public String getSignatureKey() {
        return signatureKey != null ? signatureKey : "asdasgfdwqew";
    }
}