import {defineStore} from 'pinia'
import {ref} from 'vue'
import {
    appMerchantGetFirstShopSettledGet,
    appMerchantGetLoginMerchantGet,
    appMerchantSwitchListGet,
    appMerchantSwitchMerchantIdGet,
    appUserGetLoginUserGet
} from '@/service'
import Config from '@/config'

export const useUserStore = defineStore(
    'user',
    () => {

        // 登录邀请码
        const invitationCode = ref('')

        // 登录token
        const token = ref('')

        // 当前所处商户的TOKEN
        const currentMerchantToken = ref('')

        // 店铺信息
        const currenMerchantInfo = ref({})

        // 是否登录
        const isLogin = ref(false)

        // 用户信息
        const userInfo = ref<Partial<IUserInfo>>({})

        // 店铺列表
        const merchants = ref([])

        // 店铺认证信息
        const firstShopSettled = ref(null)

        // 未读消息数量
        const unreadMessageCount = ref(0)

        // 当前选中菜单的索引ID
        const currentMenuID = ref(null)

        // 位置
        const location = ref<{
            location: string;
            longitude: string | number;
            latitude: string | number;
        }>({
            location: '',
            longitude: "",
            latitude: "",
        })


        watch(
            token,
            (newVal) => {
                isLogin.value = !!newVal
            },
            {
                immediate: true,
            },
        )


        const checkLogin = (): boolean => {
            console.log('isLogin.value', isLogin.value)
            if (!isLogin.value) {
                uni.navigateTo({
                    url: Config.loginPath,
                })
            }

            return isLogin.value
        }

        const clear = () => {
            userInfo.value = {}
            token.value = ''
            isLogin.value = false
            invitationCode.value = ''
            currentMerchantToken.value = ''
            currenMerchantInfo.value = {}
            merchants.value = []
            firstShopSettled.value = null
            currentMenuID.value = null
        }

        const getUserInfo = async () => {
            if (!isLogin.value) return
            try {
                const res = await appUserGetLoginUserGet({})
                userInfo.value = res.data || {}
            } catch (e) {
            }
        }

        // 查询当前用户可切换的商户
        const getSwitchableMerchants = async (callBack?: () => void) => {
            if (!isLogin.value) return
            try {
                const res = await appMerchantSwitchListGet({
                    options: {}
                })
                console.log('查询当前用户可切换的商户', res)
                merchants.value = res.data || []
                callBack?.()
            } catch (e) {
            }
        }
        // 获取切换商户信息信息
        const getSwitchableMerchantInfo = async () => {
            if (!isLogin.value) return
            if (!currentMerchantToken.value) return
            try {
                const res = await appMerchantGetLoginMerchantGet({
                    options: {}
                })
                console.log('获取切换商户信息信息', res.data)
                currenMerchantInfo.value = res.data || {}
                console.log('获取切换商户信息信息', currenMerchantInfo.value)
            } catch (e) {
            }
        }

        // 切换商户 /app/merchant/switch/{merchantId}
        const switchMerchant = async (merchantId: string) => {
            if (!isLogin.value) return
            try {
                const res = await appMerchantSwitchMerchantIdGet({
                    merchantId,
                    options: {}
                })
                console.log('切换商户', res)
            } catch (e) {
            }
        }

        // 查询商户用户的第一个入驻信息
        const getFirstShopSettled = async () => {
            if (!isLogin.value) return
            try {
                const res = await appMerchantGetFirstShopSettledGet({})
                console.log('查询商户用户的第一个入驻信息', res)
                firstShopSettled.value = res.data || null
            } catch (e) {
            }
        }

        return {
            invitationCode,
            token,
            currentMerchantToken,
            isLogin,
            userInfo,
            location,
            firstShopSettled,
            currenMerchantInfo,
            unreadMessageCount,
            currentMenuID,
            getUserInfo,
            checkLogin,
            clear,
            merchants,
            getSwitchableMerchants,
            getSwitchableMerchantInfo,
            switchMerchant,
            getFirstShopSettled,
        }
    },
    {
        persist: true,
    },
)
