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 checkEmail from '@/pages/check-email';
import emailVerification from '@/pages/email-verification';
import licenseActivation from '@/pages/license-activation';
import forgotPassword from '@/pages/forgot-password';
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';

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: '/check-email',
    component: checkEmail,
    name: 'checkEmail',
    meta: {
      public: true,
      layout: 'public',
    },
  },
  {
    path: '/verify-email',
    component: emailVerification,
    name: 'emailVerification',
    meta: {
      public: true,
      layout: 'public',
    },
  },
  {
    path: '/license-activation',
    component: licenseActivation,
    name: 'licenseActivation',
    meta: {
      layout: 'public',
    },
  },
  {
    path: '/forgot-password',
    component: forgotPassword,
    name: 'forgotPassword',
    meta: {
      layout: 'public',
      public: true,
    },
  },
  {
    path: '/news',
    component: news,
    name: 'news',
  },
  {
    path: '/login-success',
    redirect: to => {
      console.log(to)
      switch (to.query.state) {
        case 'email_not_verified':
          return '/verify-email'
      }
    }
  },
  ...onboardingRoutes,
  {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 default router;
