import { defineStore } from "pinia"
import { store } from "../index"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { logout_gym, logout_gyt, getUserInfo_gyt, queryUserInfo_gym } from "@/api/user.api"
import { removeToken, setToken, setUserType, getUserType, removeUserType, setCODE401 } from "@/libs/auth"
import router, { resetRouter } from "@/router"
import { httpAjax, httpAjaxLoading } from "@/plugin/httpAjax"
import { menus_gym, menus_gyt } from "@/config/menus"

interface userDataType {
   appKey: string
   data: any
}
interface userType {
   userInfo: {
      avatar?: string
      username?: string
      realName?: string
      phone?: string
   }
   roles?: "GYM" | "GYT"
}

const useStore = defineStore("user", {
   state: (): userType => {
      return {
         userInfo: {}, //用户信息
         roles: undefined //用户角色
      }
   },
   actions: {
      /** 登录 */
      async login({ data, appKey }: userDataType) {
         // GYM 和 GYT 区分   获取token的区分
         setToken(appKey === "GYM" ? data : data.access_token)
         setUserType(appKey)
         return Promise.resolve()
      },
      /** 获取用户信息 */
      async getUserInfo(): Promise<menuType[]> {
         const userType = getUserType()
         if (!userType) {
            return Promise.reject({
               code: "500",
               data: null,
               message: "获取用户信息出错!"
            })
         }
         // GYM 和 GYT 区分 code验证错误码的区分
         userType === "GYM" || setCODE401(5000)
         // GYM 和 GYT 区分 获取用户信息的接口区分
         const userInfoRes = await httpAjaxLoading(userType === "GYM" ? queryUserInfo_gym : getUserInfo_gyt)
         if (userInfoRes.code !== 200) {
            return Promise.reject(userInfoRes)
         }
         // GYM 和 GYT 区分 用户信息数据不一样区分
         this.userInfo = handleUserInfo(userType, userInfoRes.data)
         this.roles = userType as any
         // GYM 和 GYT 区分  区分菜单然后分配路由
         return Promise.resolve(userType === "GYM" ? menus_gym : menus_gyt)
      },
      /** 退出登录 */
      async loginOut() {
         // GYM 和 GYT 区分  区分退出登录
         const logoutRes = await httpAjax(this.roles === "GYM" ? logout_gym : logout_gyt)
         if (logoutRes.code !== 200) {
            return Promise.reject(logoutRes)
         }
         this.resetUser()
         return Promise.resolve(logoutRes)
      },
      /** 清空所有登录信息，退出 */
      resetUser() {
         this.userInfo = {}
         this.roles = undefined
         removeToken()
         removeUserType()
         resetRouter()
         router.push({
            path: "/login"
         })
      }
   }
})
export default () => {
   return useStore(store)
}

function handleUserInfo(userType: string, userInfo: Record<string, any>) {
   let avatar, username, realName, phone
   if (userType === "GYM") {
      avatar = userInfo.avatar
      username = userInfo.username
      realName = userInfo.realName
      phone = userInfo.phone
   } else {
      avatar = userInfo.avatar
      username = userInfo.nickname
      realName = userInfo.realName
      phone = userInfo.phone
   }
   return {
      avatar,
      username,
      realName,
      phone
   }
}
