import { emitter } from '@kit.BasicServicesKit'
import { AppStorageKeyCollect } from '../constants'

/**
 * @description 用户信息类,可结合AppStorage使用,实现响应式更换用户信息,使用时替换为后端传回的用户属性即可
 */
export class UserInfo {
  static KEY: string = 'UserInfo'
  private declare static instance: UserInfo
  private isLogin: boolean = false
  private token?: string
  //此注释下的属性可按需更改
  private memberName?: string
  private memberPhone?: string
  private memberEmail?: string
  private memberIcon?: string
  private aiCount?: number
  private userId?: number
  private memberScore?: number

  private constructor() {
  }

  /**
   * 获取用户单例
   * @returns UserInfo
   */
  static getInstance() {
    if (!UserInfo.instance) {
      UserInfo.instance = new UserInfo()
    }
    return UserInfo.instance
  }

  /**
   * 设置用户信息并且设置登录状态
   * @param _userInfo
   */
  setUserInfoAndLogin(_userInfo: UserInfo) {
    userInfo.isLogin = true
    if (_userInfo.token) {
      userInfo.token = _userInfo.token
    }
    userInfo.memberEmail = _userInfo.memberEmail
    userInfo.memberIcon = _userInfo.memberIcon
    userInfo.memberName = _userInfo.memberName
    userInfo.userId = _userInfo.userId
    userInfo.memberScore = _userInfo.memberScore
    userInfo.aiCount = _userInfo.aiCount
    userInfo.memberPhone = _userInfo.memberPhone
    emitter.emit('refreshList')
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  /**
   * @description 登出并清空用户信息
   */
  logout() {
    userInfo.isLogin = false
    userInfo.token = undefined
    userInfo.memberEmail = undefined
    userInfo.memberIcon = undefined
    userInfo.memberName = undefined
    userInfo.userId = undefined
    userInfo.memberScore = undefined
    userInfo.aiCount = undefined
    userInfo.memberPhone = undefined
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
    AppStorage.setOrCreate(AppStorageKeyCollect.TOKEN, undefined)
  }

  /**
   * 检测用户是否登录
   * @returns boolean
   */
  checkLogin() {
    return userInfo.isLogin
  }

  getToken() {
    return userInfo.token
  }


  setToken(token: string) {
    userInfo.token = token
    AppStorage.setOrCreate<string>(AppStorageKeyCollect.TOKEN, token)
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getHeadImg() {
    return userInfo.memberIcon
  }

  setHeadImg(headImg: string) {
    userInfo.memberIcon = headImg
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getEmail() {
    return userInfo.memberEmail
  }

  setEmail(email: string) {
    userInfo.memberEmail = email
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getName() {
    return userInfo.memberName
  }

  setName(name: string) {
    userInfo.memberName = name
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getId() {
    return userInfo.userId
  }

  setId(id: number) {
    userInfo.userId = id
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getGoldNum() {
    return userInfo.memberScore
  }

  setGoldNum(goldNum: number) {
    userInfo.memberScore = goldNum
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getAiNum() {
    return userInfo.aiCount
  }

  setAiNum(aiNum: number) {
    userInfo.aiCount = aiNum
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }

  getPhoneNumber() {
    return userInfo.memberPhone
  }

  setPhoneNumber(phoneNumber: string) {
    userInfo.memberPhone = phoneNumber
    AppStorage.setOrCreate(UserInfo.KEY, userInfo)
  }
}

export const userInfo = UserInfo.getInstance()
