import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import UserLogin from "@/features/UserLogin/UserLogin.vue";
import MainLayout from "@/components/MainLayout/MainLayout.vue";
import OnlineExam from "@/features/OnlineExam/OnlineExamHome.vue";
import ExamingHome from "@/features/OnlineExam/Examing/ExamingHome.vue";
import SubmitPaper from "@/features/OnlineExam/ExamEnd/SubmitPaper.vue";
import ExamEnd from "@/features/OnlineExam/ExamEnd/ExamEnd.vue";
import OnlineExamOverview from "@/features/OnlineExam/OnlineExamOverview/OnlineExamOverview.vue";
import WelcomePage from "@/features/WelcomePage/WelcomePage.vue";
import ChangePassword from "@/features/ChangePassword/ChangePassword.vue";
import SiteMessage from "@/features/SiteMessage/SiteMessage.vue";
import SiteMessageDetail from "@/features/SiteMessage/SiteMessageDetail.vue";
import OfflineExam from "@/features/OfflineExam/OfflineExam.vue";
import OnlinePractice from "@/features/OnlinePractice/OnlinePractice.vue";
import OnlinePracticeRecord from "@/features/OnlinePractice/OnlinePracticeRecord.vue";
import OnlinePracticeRecordDetail from "@/features/OnlinePractice/OnlinePracticeRecordDetail.vue";
import { store } from "@/store/store";

const routes: RouteRecordRaw[] = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: UserLogin, name: "UserLogin" },
  {
    path: "/",
    component: MainLayout,
    children: [
      {
        path: "welcome",
        component: WelcomePage,
        name: "WelcomePage",
      },
      {
        path: "online-exam",
        component: OnlineExam,
        name: "OnlineExam",
      },
      {
        path: "online-homework",
        component: OnlineExam,
        name: "OnlineHomework",
        props: { examType: "ONLINE_HOMEWORK" },
      },
      {
        path: "online-practice",
        component: OnlinePractice,
        name: "OnlinePractice",
      },
      {
        path: "online-practice/:examId/record",
        component: OnlinePracticeRecord,
        name: "OnlinePracticeRecord",
      },
      {
        path: "online-practice/:examId/record/:examRecordDataId",
        component: OnlinePracticeRecordDetail,
        name: "OnlinePracticeRecordDetail",
      },
      {
        path: "offline-exam",
        component: OfflineExam,
        name: "OfflineExam",
      },
      {
        path: "site-message",
        component: SiteMessage,
        name: "SiteMessage",
      },
      {
        path: "site-message/:noticeId",
        component: SiteMessageDetail,
        name: "SiteMessageDetail",
      },
      {
        path: "password",
        component: ChangePassword,
        name: "ChangePassword",
      },
    ],
  },
  {
    path: "/online-exam/exam/:examId/overview",
    name: "OnlineExamOverview",
    component: OnlineExamOverview,
  },
  {
    path: "/online-exam/exam/:examId/examRecordData/:examRecordDataId/order/:order",
    name: "OnlineExamingHome",
    component: ExamingHome,
  },
  {
    path: "/online-exam/exam/:examId/examRecordData/:examRecordDataId/submit",
    name: "SubmitPaper",
    component: SubmitPaper,
  },
  {
    path: "/online-exam/exam/:examId/examRecordData/:examRecordDataId/end",
    name: "ExamEnd",
    component: ExamEnd,
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@/components/PageError404.vue"),
  },
];

// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = createRouter({
  // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  history: createWebHistory("oe-web"),
  routes, // short for `routes: routes`
});

router.beforeEach((to, from, next) => {
  const loginPath = "/login/";

  if (to.path) {
    _hmt.push(["_trackPageview", "/oe-web" + to.fullPath]);
  }

  if (to.name === "UserLogin") {
    next();
  } else {
    if (!store.user.token) {
      next({ path: loginPath });
    } else {
      next();
    }
  }
});

export default router;
