import Vue from 'vue'
import Router from 'vue-router'
import { PCMenu } from "../config/menu";
import { checkWork, checkLogin,getPanoInfo } from '@/api'
import { $alert } from '@/components/shared/message'
import store from '../Store'

let vue = new Vue()

import { LoginDetector,OnlineDetector } from "@/utils/starter";
Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

let routes = [];
PCMenu.forEach(item => {
  routes.push({
    name: item.name,
    path: `${item.link}`,
    meta: {
      loading: null,
      loadScene: !item.hidescene
    },
    component: () => import(`../views/${item.name}/index.vue`)
  });
});


LoginDetector.register(
  detector => new Promise(resolve => detector.resolve(resolve))
);

OnlineDetector.register(
  detector => new Promise(resolve => detector.resolve(resolve))
);

//检验是不是该用户作品


checkWork().then(res=>{
  if (res.data) {
    checkLogin().then(response => {
      if (response.code == 3005) {
        store.commit('UpdateIsShowState', false)
        vue.$bus.emit('canLoad',false) 
        return vue.$alert({content: '不是该用户作品'});
      } else{
        vue.$bus.emit('canLoad',true) 
        LoginDetector.valid();
      }
    });
  } 
  else{
    return vue.$alert({content: '该作品已被删除'});
  }
  
})

getPanoInfo().then(() => {
  store.commit('UpdateIsShowState', true)
  // if(response&&response.status == 1){
  // }
  OnlineDetector.valid();
});


const router = new Router({
  routes: routes
})


router.beforeEach(async (to, from, next) => {
  
    await LoginDetector.listener();
    await OnlineDetector.listener();
    if (from.name == 'hotspot') {
      if (store.getters.isEditing) {
        vue.$confirm({
            content: "热点内容未编辑完，确定要关闭吗",
            ok: () => {
              vue.$bus.emit('delhotspot') 
             return next()
            }
        });
        return
      }
    }


    if (to.path == '/') {
        return next({path: "/base" })
    }
    
    next()
})


export default router;
