import { encrypt, decrypt } from './crypto';

function safeGet(data: any) {
  try {
    return JSON.parse(data);
  } catch (e) {
    return null;
  }
}

/**
 * 如果key = 'x-token' 或者 'x-user-info' 需要加密和解密。
 */
class WebStorage {
  private readonly SECRET_KEY: string;
  private _storage: any;
  constructor(storage: any) {
    // 用于加密字的符串
    this.SECRET_KEY = '7NlEQ@10';
    this._storage = storage;
  }

  randomID(randomLength = 8) {
    return Number(
      Math.random().toString().substr(3, randomLength) + Date.now(),
    ).toString(36);
  }

  get(key: string) {
    let data = this._storage.getItem(key);
    // 解密
    if (key === 'x-token') {
      data = decrypt(data, this.SECRET_KEY);
    }
    return safeGet(data);
  }

  set(key: string, value: any) {
    let valueStr = JSON.stringify(value);
    // 加密
    if (key === 'x-token') {
      valueStr = encrypt(valueStr, this.SECRET_KEY);
    }
    this._storage.setItem(key, valueStr);
  }

  remove(key: string) {
    this._storage.removeItem(key);
  }

  clear() {
    this._storage.clear();
  }
}

class MemoryStorage {
  private $dataMap: Map<any, any>;
  constructor() {
    this.$dataMap = new Map();
  }
  get(key: string) {
    this.$dataMap.get(key);
  }

  set(key: string, value: any) {
    this.$dataMap.set(key, value);
  }

  remove(key: string) {
    this.$dataMap.delete(key);
  }
}

class CookieStorage {
  get(key: string) { }

  set(key: string, value: any) { }

  remove(key: string) { }
}

export const storage = {
  local: new WebStorage(localStorage),
  session: new WebStorage(sessionStorage),
  memory: new MemoryStorage(),
  cookie: new CookieStorage(),
};

export default storage;
