import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import store from '../store';
import { Page } from './Page';
import { Dialog } from 'vant';
import http from '../plugins/http';

jsapiSign();
function jsapiSign() {
    return;
    // eslint-disable-next-line no-unreachable
    setTimeout(() => {
        if (/micromessenger/i.test(navigator.userAgent)) {
            if (/localhost|(192\.168)/i.test(location.host)) {
                return;
            }
            let isIOS = /iphone|ipad/i.test(navigator.userAgent);
            http.http
                .get('/wx/jsapiSign', { url: isIOS ? store.state.firstUrl : location.origin + location.href })
                .then(res => {
                    wx.config({
                        debug: false,
                        appId: res.appId,
                        timestamp: res.timestamp,
                        nonceStr: res.nonceStr,
                        signature: res.signature,
                        jsApiList: [
                            'chooseWXPay',
                            'updateAppMessageShareData',
                            'updateTimelineShareData',
                            'hideAllNonBaseMenuItem',
                            'scanQRCode'
                        ],
                        openTagList: ['wx-open-launch-weapp']
                    });
                    wx.error(function (res) {
                        console.log(res);
                    });
                    wx.ready(function () {
                        wx.updateAppMessageShareData({
                            desc: '全球首个基于区块链的游戏资产集换中心',
                            link: location.origin + '/9th',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/nft/2021-11-05-15-58-30YwqLzMjy.jpg'
                        });
                        wx.updateTimelineShareData({
                            title: '绿洲宇宙-全球首个基于区块链的游戏资产集换中心',
                            link: location.origin + '/9th',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/nft/2021-11-05-15-58-30YwqLzMjy.jpg'
                        });
                    });
                })
                .catch(e => {});
        }
    }, 200);
}
jsapiSign();
const routes = [
    {
        path: '/',
        redirect: 'home'
    },
    {
        path: '/',
        name: 'index',
        component: () => import('../views/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true
                }
            },
            {
                path: '/shop',
                name: 'shop',
                component: () => import('../views/Shop.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true
                }
            },
            {
                path: '/mine',
                name: 'mine',
                component: () => import('../views/Mine.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true
                }
            }
        ]
    },
    {
        path: '/roomCreate',
        name: 'roomCreate',
        component: () => import('../views/RoomCreate.vue'),
        meta: {
            title: '创建房间'
        }
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('../views/Search.vue'),
        meta: {
            pageType: Page.Every,
            menuPage: true
        }
    },
    {
        path: '/room',
        name: 'room',
        component: () => import('../views/Room.vue'),
        meta: {
            fullPage: true
        }
    },
    {
        path: '/roomDes',
        name: 'roomDes',
        component: () => import('../views/RoomDes.vue'),
        meta: {
            title: 'QQ建房说明'
        }
    },
    {
        path: '/roomResult',
        name: 'roomResult',
        component: () => import('../views/RoomResult.vue'),
        meta: {
            title: '查看战绩',
            fullPage: true,
            darkBar: true
        }
    },
    {
        path: '/exchange',
        name: 'exchange',
        component: () => import('../views/Exchange.vue'),
        meta: {
            title: '兑换详情'
        }
    },
    {
        path: '/exchangeCollection',
        name: 'exchangeCollection',
        component: () => import('../views/ExchangeCollection.vue'),
        meta: {
            title: '藏品兑换'
        }
    },
    {
        path: '/userPoint',
        name: 'userPoint',
        component: () => import('../views/user/Point.vue'),
        meta: {
            title: '绿洲核'
        }
    },
    {
        path: '/userPass',
        name: 'userPass',
        component: () => import('../views/user/Pass.vue'),
        meta: {
            title: '通行证'
        }
    },
    {
        path: '/userPassList',
        name: 'userPassList',
        component: () => import('../views/user/PassList.vue'),
        meta: {
            title: '通行证明细'
        }
    },
    {
        path: '/records',
        name: 'records',
        component: () => import('../views/user/Records.vue'),
        meta: {
            title: '我的战绩'
        }
    },
    {
        path: '/login',
        name: 'userLogin',
        component: () => import('../views/account/Login.vue'),
        meta: {
            pageType: Page.Login,
            title: '登录'
        }
    },
    {
        path: '/agreement',
        name: 'agreement',
        component: () => import('../views/account/Agreement.vue'),
        meta: {
            pageType: Page.Login,
            title: '协议'
        }
    },
    {
        path: '/register',
        name: 'userRegister',
        component: () => import('../views/account/Register.vue'),
        meta: {
            pageType: Page.Login,
            title: '注册'
        }
    },
    {
        path: '/forget',
        name: 'userForget',
        component: () => import('../views/account/Forget.vue'),
        meta: {
            // menuPage: true,
            title: '修改登录密码'
        }
    },
    {
        path: '/changePhone',
        name: 'changePhone',
        component: () => import('../views/account/ChangePhone.vue'),
        meta: {
            title: '修改密码'
        }
    },
    {
        path: '/verified',
        name: 'userVerified',
        component: () => import('../views/account/Verified.vue')
    },
    {
        path: '/verifiedSuc',
        name: 'verifiedSuc',
        component: () => import('../views/account/VerifiedSuc.vue')
    },
    {
        path: '/verifiedSucs',
        name: 'verifiedSucs',
        component: () => import('../views/account/VerifiedSucs.vue')
    },
    // {
    //     path: '/Authentication',
    //     name: 'Authentication',
    //     component: () => import('../views/account/Authentication.vue')
    // },
    {
        path: '/setting',
        name: 'userSetting',
        component: () => import('../views/account/Setting.vue'),
        meta: {
            title: '编辑资料'
        }
    },
    {
        path: '/changeText',
        name: 'userChangeText',
        component: () => import('../views/account/ChangeText.vue'),
        meta: {
            title: '修改昵称'
        }
    },
    {
        path: '/waiting',
        name: 'userWaiting',
        component: () => import('../views/account/Waiting.vue'),
        meta: {
            title: '实名认证'
        }
    },
    {
        path: '/security',
        name: 'security',
        component: () => import('../views/account/Security.vue'),
        meta: {
            title: '系统设置'
        }
    },
    {
        path: '/account',
        name: 'account',
        component: () => import('../views/user/Account.vue'),
        meta: {
            title: '关联账户'
        }
    },
    {
        path: '/collections',
        name: 'collections',
        component: () => import('../views/user/Collections.vue'),
        meta: {
            title: '藏品兑换记录'
        }
    },
    {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/user/Reports.vue'),
        meta: {
            title: '异常申报纪录'
        }
    },
    {
        path: '/report',
        name: 'report',
        component: () => import('../views/user/Report.vue'),
        meta: {
            title: '反馈纪录'
        }
    },
    {
        path: '/reportAdd',
        name: 'reportAdd',
        component: () => import('../views/user/ReportAdd.vue'),
        meta: {
            title: '异常申报'
        }
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../views/account/About.vue'),
        meta: {
            pageType: Page.Every
        }
    },
    {
        path: '/faceAuth',
        name: 'faceAuth',
        component: () => import('../views/account/FaceAuth.vue'),
        meta: {
            title: '实名认证'
        }
    },
    {
        path: '/introduction',
        name: 'introduction',
        component: () => import('../views/account/Introduction.vue'),
        meta: {
            title: '关于我们'
        }
    }
];

const router = createRouter({
    history:
        process.env.VUE_APP_CORDOVA === 'true'
            ? createWebHashHistory()
            : createWebHistory(process.env.VUE_APP_PUBLIC_PATH),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 };
    }
});

router.beforeEach((to, from, next) => {
    if (/^\/http/.test(to.path)) {
        let url = to.path.replace('/', '');
        let params = [];
        if (to.query) {
            for (let key in to.query) {
                // eslint-disable-next-line no-prototype-builtins
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`);
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`;
        }
        window.open(url);
        return;
    }

    if (to.query.invitor) {
        sessionStorage.setItem('invitor', to.query.invitor);
    }
    if (to.meta.pageType != Page.Every && to.path !== '/collectionDetail' && to.path !== '/castingDetail') {
        if (!store.state.userInfo && to.meta.pageType !== Page.Login) {
            store
                .dispatch('getUserInfo')
                .then(() => {
                    next(backNext(to));
                })
                .catch(() => {
                    Dialog.confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录'
                    })
                        .then(() => {
                            let url = '/login';
                            if (to.meta.needBack) {
                                store.commit('setLoginBackUrl', to.fullPath);
                            }
                            next('/login');
                        })
                        .catch(() => {
                            next(false);
                        });
                });
        } else {
            next(backNext(to));
        }
    } else {
        next(backNext(to));
        !!store.state.userInfo || store.dispatch('getUserInfo');
    }
});
router.afterEach((to, from) => {
    jsapiSign();
});

function backNext(to) {
    if (to.path === '/collectionDetail') {
        to.path = '/productDetail';
        return to;
    } else if (to.path === '/castingDetail') {
        to.path = '/creatorDetail';
        return to;
    } else {
        return;
    }
}
export default router;
