import api from '@/api'
import i18n, { LangTypes, t } from '@/locale'
import { ILogin, LoginRes, User, UserState } from '@/models/userTypes'
import { request } from '@/utils/request'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: (): UserState => {
    return {
      theme: 'light',
      lang: 'zh-Hans',
      agreePrivacy: false,
      isInstall: false,
      token: '',
      curTabBar: 'home',
      tabBar: [
        {
          icon: 'i-custom-home',
          text: t('tabbar.home'),
          code: 'home',
          show: true,
        },
        {
          icon: 'i-custom-work',
          text: t('tabbar.work'),
          code: 'work',
          show: false,
        },
        {
          icon: 'i-custom-share',
          text: t('tabbar.share'),
          code: 'share',
          show: false,
        },
        {
          icon: 'i-custom-mine',
          text: t('tabbar.mine'),
          code: 'mine',
          show: false,
        },
      ],
      userActions: [],
      user: new User({}),
    }
  },
  persist: {
    paths: ['agreePrivacy', 'isInstall', 'user', 'token', 'userActions', 'theme', 'lang'],
  },
  actions: {
    initThemeAction() {
      uni.getSystemInfo({
        success: (res) => {
          if (res && this.theme === 'system') {
            this.theme = res.osTheme ?? 'light'
          }
        },
      })
    },
    setLangAction(val: LangTypes) {
      uni.setLocale(val)
      i18n.global.locale = val
      this.lang = val
    },
    setTabBarAction(val: string) {
      this.curTabBar = val
      // 将当前tabbar的show设置为true
      this.tabBar.forEach((item) => {
        if (item.code === val && !item.show) {
          item.show = true
        }
      })
    },
    loginAction(data: ILogin): Promise<LoginRes> {
      return new Promise((resolve, reject) => {
        request
          .post(api.login, data)
          .then((res) => {
            if (res && res.data) {
              this.agreePrivacy = true
              this.isInstall = true
              this.user = res.data.user
              this.token = res.data.token
              resolve(res.data)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    logoutAction(): Promise<boolean> {
      return new Promise((resolve, reject) => {
        request
          .post(api.logout)
          .then((res) => {
            if (res) {
              this.setTabBarAction('home')
              this.token = ''
              this.user = {} as User
              this.userActions = []
              uni.reLaunch({
                url: '/pages-sub/login/index',
              })
              resolve(true)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    getUserInfoAction(): Promise<any> {
      return new Promise((resolve, reject) => {
        request
          .get(api.Me.info)
          .then((res) => {
            if (res && res.data) {
              this.userActions = res.data.actions
              resolve(res.data)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
  },
})
