import tool from './tool'
import Vue from 'vue'
import router from '@/router'
import baseInfo from './baseInfo'

const userInfo = {
  clear () {
    localStorage.removeItem('userId')
    localStorage.removeItem('userName')
    localStorage.removeItem('accessToken')
    localStorage.removeItem('accessTokenExpiresIn')
    localStorage.removeItem('accessTokenUpdateAt')
    localStorage.removeItem('refreshToken')
    localStorage.removeItem('refreshTokenExpiresIn')
    localStorage.removeItem('refreshTokenUpdateAt')
    localStorage.removeItem('loginTimestamp')
    localStorage.removeItem('baseData')
    baseInfo.clear()
  },
  hasLogin () {
    let accessToken = this.accessToken()
    if (!accessToken) {
      return false
    }
    // 检测会员的refreshToken是否已经失效，如果已经失效，那么则清空登录信息，返回失败
    let refreshToken = this.refreshToken()
    let refreshTokenExpiresIn = this.refreshTokenExpiresIn()
    let refreshTokenUpdateAt = this.refreshTokenUpdateAt()
    if (refreshToken && refreshTokenExpiresIn && parseInt(refreshTokenExpiresIn) - 30 + parseInt(refreshTokenUpdateAt) > parseInt(tool.getTimestamp())) {
      return true
    } else {
      this.clear()
      // 跳转到登录页
      if (router.currentRoute.path !== '/login') {
        router.push('/login')
      }
      return false
    }
  },
  userId (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('userId')
    }
    if (arg[0] === '') {
      localStorage.removeItem('userId')
      return
    }
    localStorage.setItem('userId', arg[0])
  },
  userName (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('userName')
    }
    if (arg[0] === '') {
      localStorage.removeItem('userName')
      return
    }
    localStorage.setItem('userName', arg[0])
  },
  accessToken (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('accessToken')
    }
    if (arg[0] === '') {
      localStorage.removeItem('accessToken')
      return
    }
    localStorage.setItem('accessToken', arg[0])
  },
  accessTokenUpdateAt (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('accessTokenUpdateAt')
    }
    if (arg[0] === '') {
      localStorage.removeItem('accessTokenUpdateAt')
      return
    }
    localStorage.setItem('accessTokenUpdateAt', arg[0])
  },
  accessTokenExpiresIn (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('accessTokenExpiresIn')
    }
    if (arg[0] === '') {
      localStorage.removeItem('accessTokenExpiresIn')
      return
    }
    localStorage.setItem('accessTokenExpiresIn', arg[0])
  },
  refreshToken (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('refreshToken')
    }
    if (arg[0] === '') {
      localStorage.removeItem('refreshToken')
      return
    }
    localStorage.setItem('refreshToken', arg[0])
  },
  refreshTokenUpdateAt (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('refreshTokenUpdateAt')
    }
    if (arg[0] === '') {
      localStorage.removeItem('refreshTokenUpdateAt')
      return
    }
    localStorage.setItem('refreshTokenUpdateAt', arg[0])
  },
  refreshTokenExpiresIn (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('refreshTokenExpiresIn')
    }
    if (arg[0] === '') {
      localStorage.removeItem('refreshTokenExpiresIn')
      return
    }
    localStorage.setItem('refreshTokenExpiresIn', arg[0])
  },
  loginTimestamp (...arg) {
    if (arg.length === 0) {
      return localStorage.getItem('loginTimestamp')
    }
    if (arg[0] === '') {
      localStorage.removeItem('loginTimestamp')
      return
    }
    localStorage.setItem('loginTimestamp', arg[0])
  },
  baseData (...arg) {
    if (arg.length === 0) {
      return JSON.parse(localStorage.getItem('baseData'))
    }
    if (arg[0] === '') {
      localStorage.removeItem('baseData')
      return
    }
    localStorage.setItem('baseData', JSON.stringify(arg[0]))
  },
  updateLoginAllInfo (response) {
    // 更新本地accessToken
    userInfo.accessToken(response.accessToken)
    userInfo.accessTokenExpiresIn(response.accessTokenExpiresIn)
    userInfo.accessTokenUpdateAt(response.accessTokenUpdateAt)
    userInfo.refreshToken(response.refreshToken)
    userInfo.refreshTokenExpiresIn(response.refreshTokenExpiresIn)
    userInfo.refreshTokenUpdateAt(response.refreshTokenUpdateAt)
    userInfo.loginTimestamp(response.accessTokenUpdateAt)
  },
  logout() {
    this.clear()
    // 跳转到登录页
    if (router.currentRoute.path !== '/login') {
      router.push('/login')
    }
  },
}

export default userInfo