import { defineStore } from 'pinia'
import { store } from '@/store'
import { ACCESS_TOKEN, CURRENT_USER, IM_TOKEN } from '@/store/mutation-types'
import { ResultEnum } from '@/enums/httpEnum'

import { getUserInfo, login, api_logout } from '@/api/system/user'
import { storage } from '@/utils/storage'

export interface IUserState {
  token: string
  imToken: string
  username: string
  welcome: string
  avatar: string
  permissions: any[]
  info: any
}

export const useUserStore = defineStore({
  id: 'app-user',
  state: (): IUserState => ({
    token: storage.get(ACCESS_TOKEN, ''),
    imToken: storage.get(IM_TOKEN, ''),
    username: '',
    welcome: '',
    avatar: '',
    permissions: [],
    info: storage.get(CURRENT_USER, {})
  }),
  getters: {
    getToken(): string {
      return this.token
    },
    getImToken(): string {
      return this.imToken
    },
    getAvatar(): string {
      return this.avatar
    },
    getNickname(): string {
      return this.username
    },
    getPermissions(): [any][] {
      return this.permissions
    },
    getUserInfo(): object {
      return this.info
    }
  },
  actions: {
    setToken(token: string) {
      this.token = token
    },
    setImToken(token: string) {
      this.imToken = token
    },
    setAvatar(avatar: string) {
      this.avatar = avatar
    },
    setPermissions(permissions: any) {
      this.permissions = permissions
    },
    setUserInfo(info: any) {
      this.info = info
    },
    // 登录
    async login(userInfo: any) {
      try {
        const response = (await login(userInfo)) as any
        const { data, code } = response
        if (code === ResultEnum.SUCCESS) {
          const ex = 7 * 24 * 60 * 60 * 1000
          storage.set(ACCESS_TOKEN, data.token_type + ' ' + data.access_token, ex)
          storage.set(CURRENT_USER, data, ex)
          this.setToken(data.token_type + ' ' + data.access_token)
          this.setImToken(data.imToken)
          this.setUserInfo(data)
        }
        return Promise.resolve(response)
      } catch (e) {
        return Promise.reject(e)
      }
    },

    // 获取用户信息
    GetInfo() {
      const that = this
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            const result = res.data
            that.setUserInfo(result)
            that.setAvatar(result.account.avatar)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 登出
    async logout() {
      // try {
      //   const response = (await login(userInfo)) as any
      //   const { data, code } = response
      //   if (code === ResultEnum.SUCCESS) {
      //     const ex = 7 * 24 * 60 * 60 * 1000
      //     storage.set(ACCESS_TOKEN, data.token_type + ' ' + data.access_token, ex)
      //     storage.set(CURRENT_USER, data, ex)
      //     this.setToken(data.token_type + ' ' + data.access_token)
      //     this.setImToken(data.imToken)
      //     this.setUserInfo(data)
      //   }
      //   return Promise.resolve(response)
      // } catch (e) {
      //   return Promise.reject(e)
      // }
      try {
        await api_logout({})
      } catch {}
      this.setPermissions([])
      this.setUserInfo('')
      storage.remove(ACCESS_TOKEN)
      storage.remove(CURRENT_USER)
      return Promise.resolve('')
    }
  }
})

// Need to be used outside the setup
export function useUserStoreWidthOut() {
  return useUserStore(store)
}
