import { ref } from "vue"
import store from "@/store"
import { defineStore } from "pinia"
import { usePermissionStore } from "./permission"
import { useTagsViewStore } from "./tags-view"
import { getToken, removeToken, setToken } from "@/utils/cache/cookies"
import router, { resetRouter } from "@/router"
import { loginApi } from "@/api/login"
import { type ILoginRequestData } from "@/api/login/types/login"
import { type RouteRecordRaw } from "vue-router"
import asyncRouteSettings from "@/config/async-route"

export const useUserStore = defineStore("user", () => {
  /** Token即用户id */
  const token = ref<number>(getToken() || -1)
  const roles = ref<string[]>([])
  const username = ref<string>("")

  const permissionStore = usePermissionStore()
  const tagsViewStore = useTagsViewStore()

  /** 设置角色数组 */
  const setRoles = (value: string[]) => {
    roles.value = value
  }
  /** 登录 */
  const login = (loginData: ILoginRequestData) => {
    return new Promise((resolve, reject) => {
      loginApi(loginData)
        .then((res) => {
          if (res.status == 202) {
            setToken(res.data.data.id)
            resolve(res.data.data)
            return
          }
          reject(res.data.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
  /** 获取用户详情 */
  const getInfo = () => {
    roles.value = asyncRouteSettings.defaultRoles
    roles.value = ["admin"]
    console.log("getInfo()")
    // return new Promise((resolve, reject) => {
    //   getUserInfoApi()
    //     .then((res) => {
    //       const data = res.data
    //       username.value = data.username
    //       // 验证返回的 roles 是否是一个非空数组
    //       if (data.roles && data.roles.length > 0) {
    //         roles.value = data.roles
    //       } else {
    //         // 塞入一个没有任何作用的默认角色，不然路由守卫逻辑会无限循环
    //         roles.value = asyncRouteSettings.defaultRoles
    //       }
    //       resolve(res)
    //     })
    //     .catch((error) => {
    //       reject(error)
    //     })
    // })
  }
  /** 切换角色 */
  const changeRoles = async (role: string) => {
    console.log(role)
    await getInfo()
    permissionStore.setRoutes(roles.value)
    resetRouter()
    permissionStore.dynamicRoutes.forEach((item: RouteRecordRaw) => {
      router.addRoute(item)
    })
    _resetTagsView()
  }
  /** 登出 */
  const logout = () => {
    removeToken()
    token.value = ""
    roles.value = []
    resetRouter()
    _resetTagsView()
  }
  /** 重置 Token */
  const resetToken = () => {
    removeToken()
    token.value = ""
    roles.value = []
  }
  /** 重置 visited views 和 cached views */
  const _resetTagsView = () => {
    tagsViewStore.delAllVisitedViews()
    tagsViewStore.delAllCachedViews()
  }

  return { token, roles, username, setRoles, login, getInfo, changeRoles, logout, resetToken }
})

/** 在 setup 外使用 */
export function useUserStoreHook() {
  return useUserStore(store)
}
