import { login } from "@/api/login";
import { loginYiLi } from "@/api/yiLi";
import Cookies from 'js-cookie'

const TokenKey = 'System-Platform-55-Token';
const ExpiresInKey = 'System-Platform-55-Expires-In';
const TokenKeyYiLi = 'System-Platform-55-Token-YiLi';
const ExpiresInKeyYiLi = 'System-Platform-55-Expires-In-YiLi';

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

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

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

export function getExpiresIn() {
  return Cookies.get(ExpiresInKey) || -1
}

export function setExpiresIn(time) {
  return Cookies.set(ExpiresInKey, time)
}

export function removeExpiresIn() {
  return Cookies.remove(ExpiresInKey)
}

export function autoLogin() {
  deleteCookie('JSESSIONID');
  deleteCookie('rememberMe');
  login().then(res => {
    setToken(res.data.token)
    window.location.reload();
  });
}

export function getTokenYiLi() {
  return Cookies.get(TokenKeyYiLi)
}

export function setTokenYiLi(token) {
  return Cookies.set(TokenKeyYiLi, token)
}

export function removeTokenYiLi() {
  return Cookies.remove(TokenKeyYiLi)
}

export function getExpiresInYiLi() {
  return Cookies.get(ExpiresInKeyYiLi) || -1
}

export function setExpiresInYiLi(time) {
  return Cookies.set(ExpiresInKeyYiLi, time)
}

export function removeExpiresInYiLi() {
  return Cookies.remove(ExpiresInKeyYiLi)
}
export function autoLoginYiLi() {
  loginYiLi().then(res => {
    deleteCookie('JSESSIONID');
    deleteCookie('rememberMe');
    setTokenYiLi(res.access_token);
    window.location.reload();
  });
}
export function deleteCookie(name) {
  document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/;';
}
