import router from './router'
import { resetRouter } from '@/router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken, removeToken } from '@/utils/auth'
import Vue from 'vue'
import { OSSclient } from './utils/oss'
import WHITELIST from './utils/whiteList'
import moment from "moment";
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false })

const whiteList = WHITELIST
Vue.prototype.$openWindow = null
Vue.prototype.$setOpenWindow = (openWindow) => {
  if (Vue.prototype.$openWindow) {
    Vue.prototype.$openWindow.close();
  }
  Vue.prototype.$openWindow = openWindow;
}
// 时间格式化
Vue.prototype.$moment = moment;
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  Vue.prototype.$setOpenWindow();
  // 设置页面标题
  // document.title = getPageTitle(to.meta.title)
  await OSSclient()
  // 确定用户是否已登录
  const hasToken = getToken()
  Vue.prototype.toPath = to.path
  Vue.prototype.$path = []
  if (hasToken) {
    if (to.path === '/login' || to.path === '/recompose' || to.path === '/email-recompose' || to.path === '/error' || to.path === '/ReviewersResearchForm' || to.path === '/ReviewersResearch') {
      if (to.path === '/ReviewersResearch') {
        await OSSclient()
        await store.dispatch('user/logout')
      }
      if (to.path === '/ReviewersResearchForm') {
        await OSSclient()
      }
      next()
      NProgress.done()
    } else if (from.path === '/researchForm') {
      removeToken()
      const lang = to.query.lang || zzSessionStorage.getItem('lang')
      next(`/researchLogin?trialId=${to.query.trialId}&lang=${lang}`)
      NProgress.done()
    } else if (from.path === '/researchDetail_m') {
      removeToken()
      const lang = to.query.lang || zzSessionStorage.getItem('lang')
      next(`/researchLogin_m?trialId=${to.query.trialId}&lang=${lang}`)
      NProgress.done()
    } else {
      await OSSclient()
      const hasGetUserInfo = store.getters.userId
      if (hasGetUserInfo) {
        next()
      } else {
        try {
          // 获取用户信息
          await store.dispatch('user/getInfo')
          await store.dispatch('user/getUserInfo')
          const accessRoutes = await store.dispatch('permission/generateRoutes')
          resetRouter()
          router.addRoutes(accessRoutes)
          next({ ...to, replace: true })
        } catch (error) {
          console.log(error)
          // 删除token并进入登录页面以重新登录
          await store.dispatch('user/resetToken')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      if (to.path === '/researchLogin') {
        const lang = to.query.lang || zzSessionStorage.getItem('lang')
        const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
        if (flag) {
          next(`/researchLogin_m?trialId=${to.query.trialId}&lang=${lang}`)
        } else {
          next()
        }
      } else {
        // 在免登录whiteList中，直接进入
        if (to.path === '/readingDicoms' || to.path === '/noneDicomReading') {
          await OSSclient()
        }
        next()
      }
    } else {
      if (to.path === '/researchForm') {
        const lang = to.query.lang || zzSessionStorage.getItem('lang')
        next(`/researchLogin?trialId=${to.query.trialId}&lang=${lang}`)
        NProgress.done()
      } else if (to.path === '/researchDetail_m') {
        const lang = to.query.lang || zzSessionStorage.getItem('lang')
        next(`/researchLogin_m?trialId=${to.query.trialId}&lang=${lang}`)
        NProgress.done()
      } else if (to.path === '/ReviewersResearchForm') {
        next(`/ReviewersResearch?`)
        NProgress.done()
      } else {
        // 其他无权访问的页面将重定向到登录页面。
        next(`/login?`)
        NProgress.done()
      }
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
