import Cookies from 'js-cookie'

const SystemType = 'protal'

// Cookie键名常量
const COOKIE_KEYS = {
  TOKEN: `${SystemType}-Server-Token`,
  USER_TENANT_INFO: `${SystemType}-User-TenantInfo`,
  USER_ID: `${SystemType}-UserId`,
  LOGIN_TOKEN: `${SystemType}-Login-Token`,
  USERNAME: `${SystemType}-Username`,
  PASSWORD: `${SystemType}-Password`,
  REMEMBER_ME: `${SystemType}-RememberMe`,
}

// 默认Cookie选项
const DEFAULT_COOKIE_OPTIONS = {
  expires: 30, // 30天过期
}

// 请求接口token
export function getToken() {
  return Cookies.get(COOKIE_KEYS.TOKEN)
}

export function setToken(token) {
  return Cookies.set(COOKIE_KEYS.TOKEN, token)
}

export function removeToken() {
  return Cookies.remove(COOKIE_KEYS.TOKEN)
}

// 用户扩展数据
export function getUserTenantInfo() {
  return Cookies.get(COOKIE_KEYS.USER_TENANT_INFO)
}

export function setUserTenantInfo(tenantInfo) {
  return Cookies.set(COOKIE_KEYS.USER_TENANT_INFO, tenantInfo)
}

export function removeUserTenantInfo() {
  return Cookies.remove(COOKIE_KEYS.USER_TENANT_INFO)
}

// userId缓存
export function setUserId(userId) {
  return Cookies.set(COOKIE_KEYS.USER_ID, userId)
}

export function removeUserId() {
  return Cookies.remove(COOKIE_KEYS.USER_ID)
}

// 登陆token缓存
export function getLoginToken() {
  return Cookies.get(COOKIE_KEYS.LOGIN_TOKEN)
}

export function setLoginToken(token) {
  return Cookies.set(COOKIE_KEYS.LOGIN_TOKEN, token)
}

export function removeLoginToken() {
  return Cookies.remove(COOKIE_KEYS.LOGIN_TOKEN)
}

// 用户账号密码缓存
export function getUsernameToken() {
  return Cookies.get(COOKIE_KEYS.USERNAME)
}

export function setUsernameToken(token) {
  return Cookies.set(COOKIE_KEYS.USERNAME, token, DEFAULT_COOKIE_OPTIONS)
}

export function removeUsernameToken() {
  return Cookies.remove(COOKIE_KEYS.USERNAME)
}

export function getPasswordToken() {
  return Cookies.get(COOKIE_KEYS.PASSWORD)
}

export function setPasswordToken(token) {
  return Cookies.set(COOKIE_KEYS.PASSWORD, token, DEFAULT_COOKIE_OPTIONS)
}

export function removePasswordToken() {
  return Cookies.remove(COOKIE_KEYS.PASSWORD)
}

export function getRememberMeToken() {
  return Cookies.get(COOKIE_KEYS.REMEMBER_ME)
}

export function setRememberMeToken(token) {
  return Cookies.set(COOKIE_KEYS.REMEMBER_ME, token, DEFAULT_COOKIE_OPTIONS)
}

export function removeRememberMeToken() {
  return Cookies.remove(COOKIE_KEYS.REMEMBER_ME)
}

// 清空所有用户相关缓存
export function clearAllUserData() {
  removeToken()
  removeUserTenantInfo()
  removeUserId()
  removeLoginToken()
  removeUsernameToken()
  removePasswordToken()
  removeRememberMeToken()
}

// 获取所有用户信息
export function getAllUserInfo() {
  return {
    token: getToken(),
    userTenantInfo: getUserTenantInfo(),
    userId: Cookies.get(COOKIE_KEYS.USER_ID),
    loginToken: getLoginToken(),
    username: getUsernameToken(),
    rememberMe: getRememberMeToken(),
  }
}

export default {
  getToken,
  setToken,
  removeToken,
  getUserTenantInfo,
  setUserTenantInfo,
  removeUserTenantInfo,
  setUserId,
  removeUserId,
  getLoginToken,
  setLoginToken,
  removeLoginToken,
  getUsernameToken,
  setUsernameToken,
  removeUsernameToken,
  getPasswordToken,
  setPasswordToken,
  removePasswordToken,
  getRememberMeToken,
  setRememberMeToken,
  removeRememberMeToken,
  clearAllUserData,
  getAllUserInfo,
}
