import Cookies from 'js-cookie'

const TokenKey = 'token'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token: string) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function setSystemInfo(data: any) {
  localStorage.setItem('sysinfo', JSON.stringify(data));
  // 使用的事base64加密的，解决之后的值  sysPasswordChangePeriod + "|" + isSecurityControlEnabled + "|" + isRsaEnabled+ "|" + _tag+ "|" + isSSOEnabled
  // 顺序是，密码变更周期，是否启动安全控制，是否启用rsa，中间你需要根据 | 切割一下
  // console.log(window.atob(data.target).split('|'))
  const [sysPasswordChangePeriod, isSecurityControlEnabled, isRsaEnabled, _tag, isSSOEnabled, isEnterprise, btnEnabled, colEnabled, uploadFileWay] = window.atob(data.target).split('|')
  // isEnterprise 是否为企业版，0代表专业版  1代表企业版
  // 安全开关是否开启 按钮权限，列表权限，rsa权限在开启安全权限下才使用
  console.log('安全开关是否开启', isSecurityControlEnabled)
  console.log('密码是否加密', isRsaEnabled)
  const enabled = Number(isSecurityControlEnabled)
  sessionStorage.setItem('isSecurityControlEnabled', enabled ? '1' : '');
  sessionStorage.setItem('isRsaEnabled', (enabled && Number(isRsaEnabled)) ? '1' : '');
  sessionStorage.setItem('sysPasswordChangePeriod', sysPasswordChangePeriod);
  sessionStorage.setItem('isSSOEnabled', Number(isSSOEnabled) ? '1' : '');
  sessionStorage.setItem('isEnterprise', Number(isEnterprise) ? '1' : '');

  localStorage.setItem('btnNoAuth', (enabled && Number(btnEnabled)) ? '' : '1');
  localStorage.setItem('colNoAuth', (enabled && Number(colEnabled)) ? '' : '1');
  localStorage.setItem('uploadFileWay', uploadFileWay || '0');
}

export function getSystemInfo() {
  return JSON.parse(localStorage.sysinfo || '{}')
}

export function setTenantInfo(data?: any) {
  if (!data) return localStorage.removeItem('temantInfo')
  localStorage.setItem('temantInfo', JSON.stringify(data));
}

export function getTenantInfo() {
  if (localStorage.temantInfo) {
    return JSON.parse(localStorage.temantInfo)
  } else {
    return false
  }
}

export function cacheClear() {
  const tenant = getTenantInfo()
  localStorage.clear()
  sessionStorage.clear()
  setTenantInfo(tenant)
}
