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

function jsapiSign() {
    setTimeout(() => {
        if (/micromessenger/i.test(navigator.userAgent)) {
            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'
                        ]
                    });
                    wx.error(function (res) {
                        console.log(res);
                    });
                    wx.ready(function () {
                        wx.updateAppMessageShareData({
                            title: '第九空间',
                            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: '/',
        component: () => import('../views/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/discover',
                name: 'discover',
                component: () => import('../views/Discover.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/creator',
                name: 'creator',
                component: () => import('../views/Creator.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/store',
                name: 'store',
                component: () => import('../views/Store.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/mine',
                name: 'mine',
                component: () => import('../views/Mine.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'userLogin',
        component: () => import('../views/account/Login.vue'),
        meta: {
            pageType: Page.Login,
            menuPage: true,
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'userRegister',
        component: () => import('../views/account/Register.vue'),
        meta: {
            pageType: Page.Login,
            menuPage: true,
            title: '注册'
        }
    },
    {
        path: '/forget',
        name: 'userForget',
        component: () => import('../views/account/Forget.vue'),
        meta: {
            pageType: Page.Login,
            // 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')
    },
    {
        path: '/changeText',
        name: 'userChangeText',
        component: () => import('../views/account/ChangeText.vue')
    },
    {
        path: '/waiting',
        name: 'userWaiting',
        component: () => import('../views/account/Waiting.vue')
    },
    {
        path: '/security',
        name: 'security',
        component: () => import('../views/account/Security.vue')
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../views/account/About.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/connect',
        name: 'connect',
        component: () => import('../views/account/Connect.vue')
    },
    {
        path: '/tradingPassword',
        name: 'tradingPassword',
        component: () => import('../views/account/TradingPassword.vue')
    },
    {
        path: '/productDetail',
        name: 'productDetail',
        component: () => import('../views/product/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/productDetail2',
        name: 'productDetail2',
        component: () => import('../views/product/DetailMode.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/productList',
        name: 'productList',
        component: () => import('../views/product/List.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/productSearch',
        name: 'productSearch',
        component: () => import('../views/product/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorList',
        name: 'creatorList',
        component: () => import('../views/creator/List.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorSearch',
        name: 'creatorSearch',
        component: () => import('../views/creator/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/assetDetail',
        name: 'assetDetail',
        component: () => import('../views/asset/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/Consignment',
        name: 'Consignment',
        component: () => import('../views/asset/Consignment.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/assetSearch',
        name: 'assetSearch',
        component: () => import('../views/asset/Search.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/giveSearch',
        name: 'giveSearch',
        component: () => import('../views/asset/GiveSearch.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/submit',
        name: 'submit',
        component: () => import('../views/Submit.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/couponList',
        name: 'couponList',
        component: () => import('../views/order/CouponList.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/givesubmit',
        name: 'givesubmit',
        component: () => import('../views/Givesubmit.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/order/Orders.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/orderDetail',
        name: 'orderDetail',
        component: () => import('../views/order/Detail.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineFollows',
        name: 'mineFollows',
        component: () => import('../views/user/Follows.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineFollowers',
        name: 'mineFollowers',
        component: () => import('../views/user/Followers.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineLikes',
        name: 'mineLikes',
        component: () => import('../views/user/Likes.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineBanks',
        name: 'mineBanks',
        component: () => import('../views/user/Banks.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineBanksAdd',
        name: 'mineBanksAdd',
        component: () => import('../views/user/BanksAdd.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineAddress',
        name: 'mineAddress',
        component: () => import('../views/user/Address.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineAddressEdit',
        name: 'mineAddressEdit',
        component: () => import('../views/user/AddressEdit.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineWallet',
        name: 'mineWallet',
        component: () => import('../views/user/Wallet.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/minePoint',
        name: 'minePoint',
        component: () => import('../views/user/Point.vue'),
        meta: {
            title: '第九空间'
        }
    }
];

const router = createRouter({
    history: process.env.NODE_ENV === 'app' ? createWebHashHistory() : createWebHistory(process.env.BASE_URL),
    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;
    }
    console.log(to);

    if (to.query.invitor) {
        sessionStorage.setItem('invitor', to.query.invitor);
    }

    if (!store.state.userInfo && to.meta.pageType !== Page.Login) {
        store
            .dispatch('getUserInfo')
            .then(() => {
                next(backNext(to));
            })
            .catch(() => {
                if (to.meta.pageType != Page.Every && to.path !== '/collectionDetail' && to.path !== '/castingDetail') {
                    Dialog.confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录'
                    })
                        .then(() => {
                            next('/login');
                        })
                        .catch(() => {
                            next(false);
                        });
                } else {
                    next(backNext(to));
                }
            });
    } else {
        next(backNext(to));
    }
});
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;
