import crypto from "@/utils/crypto";

/** 说明
 *
 * 1. localStorage 存在 BUG, 直接存入对象会自动转为 JSONString, 若是返回数据type会执行
 */

const SS = {};
const LS = {};
const CK = {};


// -------------------------------- //
//           crypto - KEY           //
// -------------------------------- //

// 使用 bus/storage 将 key 声明为常量，便于追踪和管理
const TOKEN_KEY = "AUTH_TOKEN";
const TOKEN_CRY = "TOKEN*NEKOT";

SS.GET_TOKEN = function () {
  const token = this.GET(TOKEN_KEY);
  if (!token) return token;
  return crypto.DDES(token, TOKEN_CRY);
};

SS.SET_TOKEN = function (token) {
  return this.SET(TOKEN_KEY, crypto.EDES(token, TOKEN_CRY));
};

SS.REMOVE_TOKEN = function () {
  return this.REMOVE(TOKEN_KEY);
};

const USR_PWD_KEY = "USR_PWD";

LS.GET_USR_PWD = function () {
  const data = this.GET(USR_PWD_KEY);
  if (!data) return data;
  return crypto.DDES(data, USR_PWD_KEY);
};

LS.SET_USR_PWD = function (data) {
  return this.SET(USR_PWD_KEY, crypto.EDES(data, USR_PWD_KEY));
};

LS.REMOVE_USR_PWD = function () {
  return this.REMOVE(USR_PWD_KEY);
};

// -------------------------------- //
//     sessionStorage - string      //
// -------------------------------- //

SS.SET = function (key, data) {
  if (typeof data == "object") data = JSON.stringify(data);
  sessionStorage.setItem(key, data);
};

SS.GET = function (key) {
  let data = sessionStorage.getItem(key);
  if (/"/.test(data)) data = JSON.parse(data);
  return data;
};

SS.REMOVE = function (key) {
  sessionStorage.removeItem(key);
};

SS.CLEAR = function () {
  sessionStorage.clear();
};

// -------------------------------- //
//        localStorage - Obj;       //
// -------------------------------- //

LS.SET = function (key, data) {
  if (typeof data == "object") data = JSON.stringify(data);
  localStorage.setItem(key, data);
};

LS.GET = function (key) {
  let data = localStorage.getItem(key);
  if (/"/.test(data)) data = JSON.parse(data);
  return data;
};

LS.REMOVE = function (key) {
  localStorage.removeItem(key);
};

LS.CLEAR = function () {
  localStorage.clear();
};

// -------------------------------- //
//         lcookies - expires       //
// -------------------------------- //

CK.SET = function (name, value, seconds) {
  var expires = "";
  if (seconds != 0) {
    //设置cookie生存时间
    var date = new Date();
    date.setTime(date.getTime() + seconds * 1000);
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
};

CK.GET = function (name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(";"); // 把cookie分割成组
  for (let i = 0; i < ca.length; i++) {
    var c = ca[i]; // 取得字符串
    while (c.charAt(0) == " ") {
      // 判断一下字符串有没有前导空格
      c = c.substring(1, c.length); // 有的话，从第二位开始取
    }
    if (c.indexOf(nameEQ) == 0) {
      // 如果含有需要的name
      return unescape(c.substring(nameEQ.length, c.length)); // 解码并截取我们要值
    }
  }
  return false;
};

CK.REMOVE = function (name) {
  this.setCookie(name, "", -1);
};

/* export default 导出会加一层 default 包裹:  import 导入时默认已解构
import { SS as storage } from "storage"; // import { SS } from "storage"; */
export { SS, LS, CK };
