/**
 * 加密工具初始化模块
 * 用于在应用启动时配置和初始化加密功能
 */

import { CryptoTool, createCryptoTool } from './cryptoInterceptor';
import { CRYPTO_CONFIG, getSecretKeysConfig, getToken } from '../config';

/**
 * 加密初始化配置
 */
export interface CryptoInitConfig {
  // 是否启用加密功能
  enabled?: boolean;
  // 是否启用请求加密
  enableRequestEncryption?: boolean;
  // 是否启用响应解密
  enableResponseDecryption?: boolean;
  // 自定义加密工具配置
  customConfig?: any;
}

/**
 * 加密工具管理器
 */
export class CryptoManager {
  private cryptoTool: CryptoTool | null = null;
  private initialized: boolean = false;

  /**
   * 初始化加密功能
   */
  init(config?: CryptoInitConfig): CryptoTool {
    if (this.initialized && this.cryptoTool) {
      return this.cryptoTool;
    }

    const cryptoConfig = {
      enabled: config?.enabled ?? CRYPTO_CONFIG.ENABLED,
      enableRequestEncryption: config?.enableRequestEncryption ?? CRYPTO_CONFIG.ENABLE_REQUEST_ENCRYPTION,
      enableResponseDecryption: config?.enableResponseDecryption ?? CRYPTO_CONFIG.ENABLE_RESPONSE_DECRYPTION,
      secretKeys: getSecretKeysConfig(),
      commonParamsConfig: CRYPTO_CONFIG.COMMON_PARAMS,
      ...config?.customConfig
    };

    // 创建加密工具
    this.cryptoTool = createCryptoTool(cryptoConfig);
    
    // 设置Token
    this.cryptoTool.setToken(getToken());

    this.initialized = true;
    
    console.log('加密功能初始化完成', {
      enabled: cryptoConfig.enabled,
      requestEncryption: cryptoConfig.enableRequestEncryption,
      responseDecryption: cryptoConfig.enableResponseDecryption
    });

    return this.cryptoTool;
  }

  /**
   * 获取加密工具实例
   */
  getCryptoTool(): CryptoTool | null {
    return this.cryptoTool;
  }

  /**
   * 启用加密功能
   */
  enable(): void {
    if (this.cryptoTool) {
      this.cryptoTool.enable();
    }
  }

  /**
   * 禁用加密功能
   */
  disable(): void {
    if (this.cryptoTool) {
      this.cryptoTool.disable();
    }
  }

  /**
   * 检查加密功能是否已初始化
   */
  isInitialized(): boolean {
    return this.initialized;
  }

  /**
   * 检查加密功能是否启用
   */
  isEnabled(): boolean {
    return this.cryptoTool ? this.cryptoTool.isEnabled() : false;
  }

  /**
   * 重新配置加密功能
   */
  reconfigure(config: CryptoInitConfig): void {
    if (this.cryptoTool) {
      const cryptoConfig = {
        enabled: config.enabled ?? this.cryptoTool.isEnabled(),
        enableRequestEncryption: config.enableRequestEncryption ?? true,
        enableResponseDecryption: config.enableResponseDecryption ?? true,
        secretKeys: getSecretKeysConfig(),
        commonParamsConfig: CRYPTO_CONFIG.COMMON_PARAMS,
        ...config.customConfig
      };

      this.cryptoTool.setConfig(cryptoConfig);
      
      console.log('加密功能重新配置完成', cryptoConfig);
    }
  }

  /**
   * 销毁加密功能
   */
  destroy(): void {
    if (this.cryptoTool) {
      this.cryptoTool.disable();
      this.cryptoTool = null;
    }
    
    this.initialized = false;
    
    console.log('加密功能已销毁');
  }
}

/**
 * 全局加密管理器实例
 */
export const cryptoManager = new CryptoManager();

/**
 * 初始化加密功能（便捷函数）
 */
export const initCrypto = (config?: CryptoInitConfig): CryptoTool => {
  return cryptoManager.init(config);
};

/**
 * 检查加密功能是否已初始化
 */
export const isCryptoInitialized = (): boolean => {
  return cryptoManager.isInitialized();
};

/**
 * 检查加密功能是否启用
 */
export const isCryptoEnabled = (): boolean => {
  return cryptoManager.isEnabled();
};

/**
 * 启用加密功能
 */
export const enableCrypto = (): void => {
  cryptoManager.enable();
};

/**
 * 禁用加密功能
 */
export const disableCrypto = (): void => {
  cryptoManager.disable();
};

export default cryptoManager;