import { defineStore } from "pinia"
import { store } from "../index"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { logout_gym, logout_gyt, logout_klx, getUserInfo_gyt, queryUserInfo_gym, getUserInfo_klx } 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, menus_klx } from "@/config/menus"

interface userDataType {
   appKey: string
   data: any
}
interface userType {
   userInfo: {
      avatar?: string
      username?: string
      realName?: string
      phone?: string
      subjectId?: string
   }
   roles?: rolesType
}

const useStore = defineStore("user", {
   state: (): userType => {
      return {
         userInfo: {}, //用户信息
         roles: undefined //用户角色
      }
   },
   actions: {
      /** 登录 */
      async login({ data, appKey }: userDataType) {
         // GYM,GYT,KLX 区分 获取token的区分
         const tokenObj = {
            GYT: data.access_token,
            GYM: data,
            KLX: data
         }
         setToken(tokenObj[appKey as rolesType])
         setUserType(appKey)
         return Promise.resolve()
      },
      /** 获取用户信息 */
      async getUserInfo(): Promise<menuType[]> {
         const userType = getUserType()
         if (!userType) {
            return Promise.reject({
               code: "500",
               data: null,
               message: "获取用户信息出错!"
            })
         }
         // GYM,GYT,KLX 区分 code验证错误码的区分 管乐团是5000,其他的是403
         userType === "GYT" && setCODE401(5000)
         // GYM,GYT,KLX 区分 获取用户信息的接口区分
         const userInfoApi = {
            GYT: getUserInfo_gyt,
            GYM: queryUserInfo_gym,
            KLX: getUserInfo_klx
         }
         const userInfoRes = await httpAjaxLoading(userInfoApi[userType])
         if (userInfoRes.code !== 200) {
            return Promise.reject(userInfoRes)
         }
         // GYM,GYT,KLX 区分 用户信息数据不一样区分
         this.userInfo = handleUserInfo(userType, userInfoRes.data)
         this.roles = userType
         // GYM,GYT,KLX 区分  区分菜单然后分配路由
         const menusObj = {
            GYT: menus_gyt,
            GYM: menus_gym,
            KLX: menus_klx
         }
         return Promise.resolve(menusObj[userType])
      },
      /** 退出登录 */
      async loginOut() {
         // GYM,GYT,KLX 区分  区分退出登录
         const logoutApi = {
            GYT: logout_gyt,
            GYM: logout_gym,
            KLX: logout_klx
         }
         const logoutRes = await httpAjax(logoutApi[this.roles!])
         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: rolesType, userInfo: Record<string, any>) {
   let avatar, username, realName, phone, subjectId
   if (["GYM", "KLX"].includes(userType)) {
      avatar = userInfo.avatar
      username = userInfo.username
      realName = userInfo.realName
      phone = userInfo.phone
      subjectId = userInfo.subjectId
   } else {
      avatar = userInfo.avatar
      username = userInfo.nickname
      realName = userInfo.realName
      phone = userInfo.phone
      subjectId = userInfo.subjectId
   }
   return {
      avatar,
      username,
      realName,
      phone,
      subjectId
   }
}
