import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { setupPageGuard } from './permission'
import { ChatLayout } from '@/views/chat/layout'
import { fetchJsapiSign } from '@/api'
import { useStorage } from '@vueuse/core'
import HomeView from '@/views/page/HomeView.vue'
import Chat from '@/views/chat/Chat.vue'

function jsapiSign() {
    const isWechat = /micromessenger/i.test(navigator.userAgent)
    if (!isWechat) return
    const ios = /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
    const signUrl = ios ? sessionStorage.entryUrl : location.href
    console.log('fetchJsapiSign', signUrl)
    fetchJsapiSign(signUrl).then((res: any) => {
        wx.config({
            ...res,
            debug: useStorage('showConsole', 0).value > new Date().getTime(),
            jsApiList: ['chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData']
        })
        wx.ready(function () {
            wx.updateAppMessageShareData({
                title: '走马AI助手',
                desc: '人工智能技术驱动的自然语言处理工具',
                link: location.origin + '/ui',
                imgUrl: 'https://cdn.raex.vip/image/2023-05-09-16-18-37TFOBVNIt.png',
                success: function () {}
            })
            wx.updateTimelineShareData({
                title: '走马AI助手——人工智能技术驱动的自然语言处理工具',
                imgUrl: 'https://cdn.raex.vip/image/2023-05-09-16-18-37TFOBVNIt.png',
                link: location.origin + '/ui'
            })
        })
    })
}

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: {
            name: 'home'
        }
    },
    {
        path: '/home',
        name: 'home',
        component: HomeView,
        meta: {
            public: true
        }
    },
    {
        path: '',
        name: 'Root',
        component: ChatLayout,
        children: [
            {
                path: 'chat/:uuid?',
                name: 'Chat',
                component: Chat
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/page/LoginView.vue')
    },
    {
        path: '/vip',
        name: 'vip',
        component: () => import('@/views/page/VipView.vue')
    },
    {
        path: '/mine',
        name: 'mine',
        component: () => import('@/views/page/MineView.vue')
    },
    {
        path: '/mask',
        name: 'mask',
        component: () => import('@/views/page/MaskView.vue')
    },
    {
        path: '/rule',
        name: 'rule',
        component: () => import('@/views/page/RuleView.vue')
    },
    {
        path: '/balance',
        name: 'balance',
        component: () => import('@/views/page/BalanceView.vue')
    },
    {
        path: '/commission',
        name: 'commission',
        component: () => import('@/views/page/CommissionView.vue')
    },
    {
        path: '/moments',
        name: 'moments',
        component: () => import('@/views/page/MomentsView.vue')
    },
    {
        path: '/momentsDetail',
        name: 'momentsDetail',
        component: () => import('@/views/page/MomentsDetailView.vue')
    },
    {
        path: '/agent',
        name: 'agent',
        component: () => import('@/views/page/AgentView.vue')
    },
    {
        path: '/info',
        name: 'info',
        component: () => import('@/views/page/InfoView.vue')
    },
    {
        path: '/paperGen',
        name: 'paperGen',
        component: () => import('@/views/page/PaperGen.vue'),
        meta: {
            public: true
        }
    },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/exception/404/index.vue')
    },
    {
        path: '/500',
        name: '500',
        component: () => import('@/views/exception/500/index.vue')
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'notFound',
        redirect: '/404'
    }
]

export const router = createRouter({
    history: createWebHistory(import.meta.env.VITE_GLOB_APP_PUBLIC_PATH),
    routes,
    sensitive: true,
    scrollBehavior: () => ({ left: 0, top: 0 })
})

setupPageGuard(router)
router.afterEach(() => {
    jsapiSign()
})

export async function setupRouter(app: App) {
    app.use(router)
    await router.isReady()
}
