import Vue from 'vue'
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import notification from 'ant-design-vue/es/notification'
import {
  ACCESS_TOKEN,
  INDEX_MAIN_PAGE_PATH,
  UI_CACHE_DB_DICT_DATA,
  USER_INFO,
  USER_NAME
} from '@/store/mutation-types'
import {
  generateIndexRouter,
  welcome
} from '@/utils/util'


NProgress.configure({
  showSpinner: false
}) // NProgress Configuration

const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration', '/FocusWarningPage', '/drillingRisk'] // no redirect whitelist
if (process.env.VUE_APP_IFRAME_CHECK === 'TRUE') {
  whiteList.push('/top')
}

function initRouter() {
  router.beforeEach((to, from, next) => {
    NProgress.start() // start progress bar

    if (Vue.ls.get(ACCESS_TOKEN)) {
      /* has token */
      if (to.path === '/user/login') {
        next({
          path: INDEX_MAIN_PAGE_PATH
        })
        NProgress.done()
      } else {
        if (false && store.getters.permissionList.length === 0) {} else {
          next()
        }
      }
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        // 在免登录白名单，直接进入
        next()
      } else {
        if (process.env.VUE_APP_IFRAME_CHECK === 'TRUE') {
          next({
            path: '/top',
            query: {
              redirect: to.fullPath
            }
          })
        } else {
          next({
            path: '/user/login',
            query: {
              redirect: to.fullPath
            }
          })
        }

        NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
      }
    }
  })

  router.afterEach(() => {
    NProgress.done() // finish progress bar
  })
}

export default initRouter