import {decrypt, encrypt} from "@/utils/jsencrypt";

const AccessTokenKey = 'ACCESS_TOKEN'
const RefreshTokenKey = 'REFRESH_TOKEN'

// ========== Token 相关 ==========

export function getAccessToken() {
  return localStorage.getItem(AccessTokenKey)
}

export function getRefreshToken() {
  return localStorage.getItem(RefreshTokenKey)
}

export function setToken(token) {
  localStorage.setItem(AccessTokenKey, token.accessToken)
  localStorage.setItem(RefreshTokenKey, token.refreshToken)
}

export function removeToken() {
  localStorage.removeItem(AccessTokenKey)
  localStorage.removeItem(RefreshTokenKey)
}

// ========== 账号相关 ==========

const UsernameKey = 'USERNAME'
const PasswordKey = 'PASSWORD'
const RememberMeKey = 'REMEMBER_ME'

export function getUsername() {
  return localStorage.getItem(UsernameKey)
}

export function setUsername(username) {
  localStorage.setItem(UsernameKey, username)
}

export function removeUsername() {
  localStorage.removeItem(UsernameKey)
}

export function getPassword() {
  const password = localStorage.getItem(PasswordKey)
  return password ? decrypt(password) : undefined
}

export function setPassword(password) {
  localStorage.setItem(PasswordKey, encrypt(password))
}

export function removePassword() {
  localStorage.removeItem(PasswordKey)
}

export function getRememberMe() {
  return localStorage.getItem(RememberMeKey) === 'true'
}

export function setRememberMe(rememberMe) {
  localStorage.setItem(RememberMeKey, rememberMe)
}

export function removeRememberMe() {
  localStorage.removeItem(RememberMeKey)
}

// ========== 租户相关 ==========

const TenantIdKey = 'TENANT_ID'
const TenantNameKey = 'TENANT_NAME'

export function getTenantName() {
  return localStorage.getItem(TenantNameKey)
}

export function setTenantName(username) {
  localStorage.setItem(TenantNameKey, username)
}

export function removeTenantName() {
  localStorage.removeItem(TenantNameKey)
}

export function getTenantId() {
  return localStorage.getItem(TenantIdKey)
}

export function setTenantId(username) {
  localStorage.setItem(TenantIdKey, username)
}

export function removeTenantId() {
  localStorage.removeItem(TenantIdKey)
}

export class getToken {
}

/**
 * 获取预览地址
 * @param url 请求地址不带，无需带orgin
 * @param fullfilename 文件名
 * @param watermarkTxt 水印
 */
export function getPreViewUrl(url, fullfilename, watermarkTxt) {
  // let Base64 = require('js-base64').Base64
  let connectChart = url.indexOf("?") !== -1 ? "&" : "?";
  let urlAdded =
    website.ossUrl +
    url +
    connectChart +
    "fullfilename=" +
    randomString(8) +
    "-" +
    fullfilename +
    "&Authorization=Bearer " +
    getToken();
  let preViewUrl =
    website.preViewUrl + encodeURIComponent(Base64.encode(urlAdded));
  if (watermarkTxt) {
    preViewUrl += "&watermarkTxt=" + watermarkTxt;
  }
  return preViewUrl;
}
export function getPreViewUrlForPhone(url, fullfilename, watermarkTxt) {
  console.log(url,86)
  let connectChart = '&' ;
  let urlAdded = website.ossUrl +
    url +
    connectChart +
    'fullfilename=' +
    randomString(8) +
    '-' +
    fullfilename +
    '&Authorization=Bearer ' +
    "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZGVudGl0eUlkIjoxLCJkZXB0SWQiOjEsIm5pY2tuYW1lIjoiR2xvYmFsIFVzZXIiLCJ0ZW5hbnQtaWQiOjEsInVzZXJuYW1lIjoiR2xvYmFsIFVzZXIiLCJleHAiOjE5NzU2NTcyNzZ9.VuFK-ubqp54VwSL3O8yprt7lMWkc77jfSboxUXtRqQ4";
  let preViewUrl =
    "https://ylgfscgk.ylgf.com:17031/kkPreview/onlinePreview?url=" + encodeURIComponent(Base64.encode(urlAdded))
  console.log(urlAdded,90)
  if (watermarkTxt) {
    preViewUrl += ('&watermarkTxt=' + watermarkTxt)
  }
  return preViewUrl;
}
