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({
                            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: '/',
        name: 'index',
        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: {
                    title: '传奇数藏',
                    needBack: true,
                    menuPage: true
                }
            },
            {
                path: '/mineMessages',
                name: 'mineMessages',
                component: () => import('../views/user/Messages.vue'),
                meta: {
                    title: '消息中心'
                }
            },
            {
                path: '/mine',
                name: 'mine',
                component: () => import('../views/Mine.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '传奇数藏'
                }
            }
        ]
    },
    {
        path: '/hall',
        name: 'hall',
        component: () => import('../views/hall/Detail.vue'),
        meta: {
            pageType: Page.Every,
            menuPage: true,
            isHall: true,
            title: '传奇数藏'
        }
    },
    {
        path: '/hallEdit',
        name: 'hallEdit',
        component: () => import('../views/hall/Edit.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏',
            isHall: true,
            tabColor: '#201B15'
        }
    },
    {
        path: '/productAdd',
        name: 'productAdd',
        component: () => import('../views/hall/ProductAdd.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏',
            isHall: true,
            tabColor: '#201B15'
        }
    },
    {
        path: '/login',
        name: 'userLogin',
        component: () => import('../views/account/Login.vue'),
        meta: {
            pageType: Page.Login,
            title: ' ',
            tabColor: '#201B15'
        }
    },
    {
        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: '注册',
            tabColor: '#201B15'
        }
    },
    {
        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'),
        meta: {
            title: ' '
        }
    },
    {
        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'),
        meta: {
            title: ' '
        }
    },
    {
        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: '/introduction',
        name: 'introduction',
        component: () => import('../views/account/Introduction.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏'
        }
    },
    {
        path: '/question',
        name: 'question',
        component: () => import('../views/account/Question.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏'
        }
    },
    {
        path: '/connect',
        name: 'connect',
        component: () => import('../views/account/Connect.vue'),
        meta: {
            pageType: Page.Every,
            title: ' '
        }
    },
    {
        path: '/tradingPassword',
        name: 'tradingPassword',
        component: () => import('../views/account/TradingPassword.vue')
    },
    {
        path: '/productDetail',
        name: 'productDetail',
        component: () => import('../views/product/Detail.vue'),
        meta: {
            pageType: Page.Every,
            tabColor: '#201B15'
        }
    },
    {
        path: '/collectionGift',
        name: 'collectionGift',
        component: () => import('../views/product/collectionGift.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏'
        }
    },
    {
        path: '/giftConfirmation',
        name: 'giftConfirmation',
        component: () => import('../views/product/giftConfirmation.vue')
        // meta: {
        //     pageType: Page.Every,
        //     title: '传奇数藏'
        // }
    },
    // {
    //     path: '/consignment',
    //     name: 'consignment',
    //     component: () => import('../views/product/consignment.vue'),
    //     meta: {
    //         pageType: Page.Every,
    //         title: '传奇数藏'
    //     }
    // },
    {
        path: '/productOrderDetails',
        name: 'productOrderDetails',
        component: () => import('../views/product/orderDetails.vue'),
        meta: {
            pageType: Page.Every,
            menuPage: true,
            title: '传奇数藏',
            tabColor: '#201B15'
        }
    },
    {
        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: '传奇数藏',
            tabColor: '#201B15'
        }
    },
    {
        path: '/Consignment',
        name: 'Consignment',
        component: () => import('../views/asset/Consignment.vue'),
        meta: {
            pageType: Page.Every,
            title: '挂售上架'
        }
    },
    {
        path: '/ConsignmentSuccess',
        name: 'ConsignmentSuccess',
        component: () => import('../views/asset/ConsignmentSuccess.vue'),
        meta: {
            pageType: Page.Every
        }
    },
    {
        path: '/consignmentAgreement',
        name: 'consignmentAgreement',
        component: () => import('../views/asset/Agreement.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: 'GAS费用'
        }
    },
    {
        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: '/activityOrders',
        name: 'activityOrders',
        component: () => import('../views/order/ActivityOrders.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/activityOrderDetail',
        name: 'activityOrderDetail',
        component: () => import('../views/order/ActivityDetail.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/activityList',
        name: 'activityList',
        component: () => import('../views/activity/List1.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏',
            tabColor: '#201B15'
        }
    },
    {
        path: '/activityDetail',
        name: 'activityDetail',
        component: () => import('../views/activity/Detail1.vue'),
        meta: {
            pageType: Page.Every,
            title: '传奇数藏',
            tabColor: '#201B15'
        }
    },
    {
        path: '/activitySubmit',
        name: 'activitySubmit',
        component: () => import('../views/activity/Submit.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/chooseProduct',
        name: 'chooseProduct',
        component: () => import('../views/activity/ChooseProduct.vue'),
        meta: {
            title: '传奇数藏',
            needBack: true
        }
    },
    {
        path: '/mineFollows',
        name: 'mineFollows',
        component: () => import('../views/user/Follows.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/mineLove',
        name: 'mineLove',
        component: () => import('../views/user/myLove.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/authenticationStatus',
        name: 'authenticationStatus',
        component: () => import('../views/user/authenticationStatus.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: {}
    },
    {
        path: '/mineAddress',
        name: 'mineAddress',
        component: () => import('../views/user/Address.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/mineMyOrder',
        name: 'mineMyOrder',
        component: () => import('../views/user/myOrder.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/mineWallet',
        name: 'mineWallet',
        component: () => import('../views/user/Wallet.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/mineBankCard',
        name: 'mineBankCard',
        component: () => import('../views/user/bankCard.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/mineExchange',
        name: 'mineExchange',
        component: () => import('../views/user/Exchange.vue'),
        meta: {
            title: '藏品记录'
        }
    },
    {
        path: '/mineAccountSecurity',
        name: 'mineAccountSecurity',
        component: () => import('../views/user/accountSecurity.vue'),
        meta: {
            title: '传奇数藏'
        }
    },
    {
        path: '/realName',
        name: 'realName',
        component: () => import('../views/user/realName.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) {
                                console.log(to)
                                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;