import Vue from 'vue';
import topic from '@/pages/topic';
import article from '@/pages/article';
import instrument from '@/pages/instrument';
import instrumentOverview from '@/pages/instrumentOverview';
import p404 from '@/pages/p404';
import start from '@/pages/start';
import submission from '@/pages/studentSubmission';
import Router from 'vue-router';
import surveyPage from '@/pages/survey';
import styleGuidePage from '@/pages/styleguide';
import joinClass from '@/pages/joinClass';
import news from '@/pages/news';

import moduleRoutes from './module.routes';
import portfolioRoutes from './portfolio.routes';
import onboardingRoutes from './onboarding.routes';
import meRoutes from './me.routes';
import authRoutes from './auth.routes';
import roomRoutes from './room.routes';

import store from '@/store/index';
import {LAYOUT_SIMPLE} from '@/router/core.constants';
import {EMAIL_NOT_VERIFIED_STATE, NO_VALID_LICENSE_STATE, SUCCESS_STATE} from './oauth.names';

const postLoginRedirectUrlKey = 'postLoginRedirectionUrl';

const routes = [
  {
    path: '/',
    name: 'home',
    component: start
  },
  ...moduleRoutes,
  ...authRoutes,
  ...roomRoutes,
  ...onboardingRoutes,
  ...portfolioRoutes,
  ...meRoutes,
  {path: '/article/:slug', name: 'article', component: article, meta: {layout: LAYOUT_SIMPLE}},
  {
    path: '/instruments/',
    name: 'instrument-overview',
    component: instrumentOverview,
  },
  {path: '/instrument/:slug', name: 'instrument', component: instrument, meta: {layout: LAYOUT_SIMPLE}},
  {path: '/submission/:id', name: 'submission', component: submission, meta: {layout: LAYOUT_SIMPLE}},
  {path: '/topic/:topicSlug', name: 'topic', component: topic, alias: '/book/topic/:topicSlug'},
  {path: '/join-class', name: 'join-class', component: joinClass, meta: {layout: LAYOUT_SIMPLE}},
  {
    path: '/survey/:id',
    component: surveyPage,
    name: 'survey',
    props: true,
    meta: {layout: LAYOUT_SIMPLE},
  },
  {
    path: '/news',
    component: news,
    name: 'news',
  },
  {
    path: '/oauth-redirect',
    redirect: to => {
      switch (to.query.state) {
        case EMAIL_NOT_VERIFIED_STATE:
          return '/verify-email';
        case NO_VALID_LICENSE_STATE:
          return '/license-activation';
        case SUCCESS_STATE:
          if (window.localStorage && localStorage.getItem(postLoginRedirectUrlKey)) {
            const redirectUrl = localStorage.getItem(postLoginRedirectUrlKey);
            localStorage.removeItem(postLoginRedirectUrlKey);
            return redirectUrl;
          }
          return '/';
        default:
          return '/unknown-auth-error';
      }
    }
  },
  {path: '/styleguide', component: styleGuidePage},
  {
    path: '*',
    component: p404,
    meta: {
      layout: 'blank',
    },
  },
];

Vue.use(Router);

const router = new Router({
  routes,
  mode: 'history',
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    }
    return {x: 0, y: 0};
  },
});

router.afterEach((to, from) => {
  store.commit('setEditModule', false);
  store.dispatch('showMobileNavigation', false);
});

export {router, postLoginRedirectUrlKey};
