import Cookies from 'js-cookie'
import { Base64 } from 'js-base64';
import settings from '@/settings';

const TokenKey = settings.projectName + '-CSMS-TOKEN'
const AuthRoutesKey = settings.projectName + '-ApacheResource'
const RoleName = settings.projectName + '-Player'
const UserName = settings.projectName + '-Gamer'
const EmailName = settings.projectName + '-MailBox'

/**
 * 获取AccessToken
 */
export function getToken() {
  if (settings.enableSessionAuth) {
    return sessionStorage.getItem(TokenKey);
  } else {
    return localStorage.getItem(TokenKey);
  }
}

/**
 * 设置AccessToken
 */
export function setToken(token) {
  if (settings.enableSessionAuth) {
    sessionStorage.setItem(TokenKey, token);
  } else {
    localStorage.setItem(TokenKey, token);
  }
}

/**
 * 移除AccessToken
 */
export function removeToken() {
  if (settings.enableSessionAuth) {
    sessionStorage.removeItem(TokenKey);
  } else {
    localStorage.removeItem(TokenKey);
  }
}

export function getAuthRoutes(back) {
  if (settings.enableOnlineAuth) {
    const apiUser = require('../api/user').default;
    apiUser.refreshAuthMenu().then(res => {
      if (res.data) {
        setAuthRoutes(res.data)
        back(res.data)
      } else {
        back([])
      }
    }).catch(err => {
      back(false)
    })
  } else {
    back(getAuthRoutesLocale())
  }
}

/**
 * 获取权限列表
 */
export function getAuthRoutesLocale() {
  const value = settings.enableSessionAuth ? sessionStorage.getItem(AuthRoutesKey) : localStorage.getItem(AuthRoutesKey);
  if (value) {
    return JSON.parse(Base64.decode(value))
  }
  return []
}

/**
 * 设置权限列表
 */
export function setAuthRoutes(routes) {
  if (routes) {
    const routerString = JSON.stringify(routes)
    const routerBase64 = Base64.encode(routerString)
    if (settings.enableSessionAuth) {
      sessionStorage.setItem(AuthRoutesKey, routerBase64);
    } else {
      localStorage.setItem(AuthRoutesKey, routerBase64)
    }
  }
}

/**
 * 移除权限列表
 */
export function removeAuthRoutes() {
  if (settings.enableSessionAuth) {
    sessionStorage.removeItem(AuthRoutesKey);
  } else {
    localStorage.removeItem(AuthRoutesKey);
  }
}

/**
 * 获取用户角色
 */
export function getRoleName() {
  const name = settings.enableSessionAuth ? sessionStorage.getItem(RoleName) : localStorage.getItem(RoleName);
  if (name) {
    return Base64.decode(name)
  } else {
    return "";
  }
}

/**
 * 设置用户角色
 */
export function setRoleName(roleName) {
  if (settings.enableSessionAuth) {
    sessionStorage.setItem(RoleName, Base64.encode(roleName));
  } else {
    localStorage.setItem(RoleName, Base64.encode(roleName))
  }
}

/**
 * 获取用户名
 */
export function getUserName() {
  const name = settings.enableSessionAuth ? sessionStorage.getItem(UserName) : localStorage.getItem(UserName);
  if (name) {
    return Base64.decode(name)
  } else {
    return "";
  }
}

/**
 * 设置用户名
 */
export function setUserName(name) {
  if (settings.enableSessionAuth) {
    sessionStorage.setItem(UserName, Base64.encode(name));
  } else {
    localStorage.setItem(UserName, Base64.encode(name))
  }
}

/**
 * 获取邮箱地址
 */
export function getEmail() {
  const name = settings.enableSessionAuth ? sessionStorage.getItem(EmailName) : localStorage.getItem(EmailName);
  if (name) {
    return Base64.decode(name)
  } else {
    return "";
  }
}

/**
 * 设置邮箱地址
 */
export function setEmail(email) {
  if (settings.enableSessionAuth) {
    sessionStorage.setItem(EmailName, Base64.encode(email));
  } else {
    localStorage.setItem(EmailName, Base64.encode(email));
  }
}