import { createRouter, createWebHashHistory } from 'vue-router'
import { $read } from '@/commons/utils'

import Login from '@/pages/Login.vue'
import Main from '@/pages/Main.vue'
import Game1 from '@/pages/game1/Game.vue'
import Game2 from '@/pages/game2/Game.vue'
import Game3 from '@/pages/game3/Game3.vue'
import Game4 from '@/pages/game4/Game4.vue'
import Game5 from '@/pages/game5/Game5.vue'
import Game6 from '@/pages/game6/Game.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: () => ($read('token', '') ? '/main' : '/login'),
        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
        },
        {
            path: '/main',
            name: 'Main',
            component: Main,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game1',
            name: 'Game1',
            component: Game1,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game2',
            name: 'Game2',
            component: Game2,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game3',
            name: 'Game3',
            component: Game3,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game4',
            name: 'Game4',
            component: Game4,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game5',
            name: 'Game5',
            component: Game5,
            meta: {
                requiresAuth: true,
            },
        },
        {
            path: '/game6',
            name: 'Game6',
            component: Game6,
            meta: {
                requiresAuth: true,
            },
        },
        // {
        //     path: '/:pathMatch(.*)*',
        //     redirect: '/',
        // },
    ],
})

router.beforeEach((to) => {
    const token = $read('token', '')

    if (to.meta.requiresAuth && !token) {
        return '/'
    }

    return true
})

export default router
