// 加密配置文件

/**
 * 获取环境变量
 * @param key 变量名
 * @param defaultValue 默认值
 * @returns 环境变量值
 */
export const getEnv = (key: string, defaultValue: string = ''): string => {
  // Web环境下获取环境变量
  if (typeof process !== 'undefined' && process.env) {
    return process.env[key] || defaultValue;
  }

  return defaultValue;
};

/**
 * 存储工具类
 */
export class StorageUtil {
  /**
   * 获取存储值
   * @param key 键名
   * @param defaultValue 默认值
   * @returns 存储值
   */
  static get(key: string, defaultValue: any = ''): any {
    try {
      if (typeof localStorage !== 'undefined') {
        const value = localStorage.getItem(key);
        return value ? JSON.parse(value) : defaultValue;
      }
      return defaultValue;
    } catch (e) {
      return defaultValue;
    }
  }

  /**
   * 设置存储值
   * @param key 键名
   * @param value 值
   */
  static set(key: string, value: any): void {
    try {
      if (typeof localStorage !== 'undefined') {
        localStorage.setItem(key, JSON.stringify(value));
      }
    } catch (e) {
      console.error('存储失败:', e);
    }
  }

  /**
   * 移除存储值
   * @param key 键名
   */
  static remove(key: string): void {
    try {
      if (typeof localStorage !== 'undefined') {
        localStorage.removeItem(key);
      }
    } catch (e) {
      console.error('移除存储失败:', e);
    }
  }

  /**
   * 清空存储
   */
  static clear(): void {
    try {
      if (typeof localStorage !== 'undefined') {
        localStorage.clear();
      }
    } catch (e) {
      console.error('清空存储失败:', e);
    }
  }
}

/**
 * 加密配置
 */
export const CRYPTO_CONFIG = {
  // 是否启用加密功能
  ENABLED: true,
  // 是否启用请求加密
  ENABLE_REQUEST_ENCRYPTION: true,
  // 是否启用响应解密
  ENABLE_RESPONSE_DECRYPTION: true,
  // 公共参数配置
  COMMON_PARAMS: {
    device: 'wap',
    source: 'xinxiuweb',
    appversion: '1.0.0',
    signatureKey: 'asdasgfdwqew',
    enableSignature: true
  },
  // 加密密钥列表
  SECRETS: [
    {
      name: 'secret_key_1',
      key: 'k0f3JfxEWd3HC7pXQU8tmSkDheUXibmz',
      iv: 'Fu2wU73MBcsEZWJk',
    },
    {
      name: 'secret_key_2',
      key: 'NdEHDuAZGCQV6C0oaURvBJJWA4z2QHyG',
      iv: 'w0j9K4bswcGJKtj5',
    },
    {
      name: 'secret_key_3',
      key: 'lBsn3FrYN9hdoFqMdqqNV1dlpGutkcXk',
      iv: 'NRHqJYTNKxH8Z9fU',
    },
    {
      name: 'secret_key_4',
      key: 'eVdEc44rke6P6rfn9SgEWGPNqkcipWN7',
      iv: 'l5HNU6Q0bdc2piB3',
    },
    {
      name: 'secret_key_5',
      key: '53BSHwE29I1u4e5cJDMZGHUdi0A7L4E5',
      iv: 'zUsjrk58p2RahP08',
    },
    {
      name: 'secret_key_6',
      key: 'vgizOWReMzVJA6LEsb9N36LEzPqcFdeO',
      iv: 'e52YLbnvVv4HpGu7',
    },
  ],
};

/**
 * 获取随机密钥
 * @returns 随机密钥对象
 */
export const getRandomSecret = () => {
  const randomIndex = Math.floor(Math.random() * CRYPTO_CONFIG.SECRETS.length);
  return CRYPTO_CONFIG.SECRETS[randomIndex];
};

/**
 * 获取加密密钥配置对象
 * @returns 密钥配置对象
 */
export const getSecretKeysConfig = () => {
  const secretKeys: Record<string, any> = {};

  CRYPTO_CONFIG.SECRETS.forEach(secret => {
    secretKeys[secret.name] = {
      key: secret.key,
      iv: secret.iv
    };
  });

  return secretKeys;
};

/**
 * 获取Token
 * @returns Token字符串
 */
export const getToken = (): string => {
  return StorageUtil.get('token', '');
};

/**
 * 设置Token
 * @param token Token字符串
 */
export const setToken = (token: string): void => {
  StorageUtil.set('token', token);
};

/**
 * 获取语言设置
 * @returns 语言代码
 */
export const getLanguage = (): string => {
  return StorageUtil.get('lang', 'zh');
};

/**
 * 设置语言
 * @param lang 语言代码
 */
export const setLanguage = (lang: string): void => {
  StorageUtil.set('lang', lang);
};