import { createRouter, createWebHistory, createWebHashHistory } from '@ionic/vue-router'
import TabsPage from '../views/TabsPage.vue'
import { Page } from './Page'
import { useUserStore } from '../stores/user'
import { useSettingsStore } from '../stores/settings'
import { emitter } from '../utils/eventBus'

const router = createRouter({
    history:
        import.meta.env.VITE_APP === 'true'
            ? createWebHashHistory(import.meta.env.BASE_URL)
            : createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home',
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/',
            name: 'index',
            component: TabsPage,
            meta: {
                // allowGuest: true
            },
            children: [
                {
                    path: '/home',
                    name: 'home',
                    component: () => import('@/views/HomePage.vue'),
                    meta: {
                        allowGuest: true
                    }
                },
                {
                    path: '/watching',
                    name: 'watching',
                    component: () => import('@/views/WatchingPage.vue'),
                    meta: {}
                },
                {
                    path: '/mine',
                    name: 'mine',
                    component: () => import('@/views/MinePage.vue'),
                    meta: {}
                }
            ]
        },
        {
            path: '/history',
            name: 'history',
            component: () => import('@/views/HistoryPage.vue')
        },
        {
            path: '/wallet',
            name: 'wallet',
            component: () => import('@/views/WalletPage.vue')
        },
        {
            path: '/record',
            name: 'record',
            component: () => import('@/views/RecordPage.vue')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/LoginPage.vue'),
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/views/RegisterPage.vue'),
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/video',
            name: 'video',
            component: () => import('@/views/VideoPage.vue'),
            meta: {}
        }
    ]
})
router.beforeEach(async (to, from, next) => {
    const { user, get: getUser } = useUserStore()
    if (!to.meta.allowGuest && !user) {
        try {
            await getUser()
            next()
        } catch (error) {
            next(false)
            emitter.emit('promptLogin')
        }
    } else {
        if (!user) {
            getUser().catch(() => {})
        }
        next()
    }
})
router.afterEach((to, from) => {
    const { toggleCS } = useSettingsStore()
    toggleCS(to.meta.showCS === true)
})
window.router = router
export default router
