import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Login from "@/features/login/Login.vue";
import Layout from "@/components/Layout.vue";
import PageError404 from "@/components/PageError404.vue";
import { getToken } from "@/auth/auth";
import { isNil } from "lodash-es";
import { useMainStore } from "@/store";

// components
import RoutOrg from "@/features/rootOrg/RootOrg.vue";
import AuthManagement from "@/features/authManagement/AuthManagement.vue";
import RootOrgEdit from "@/features/rootOrg/RootOrgEdit.vue";
import SubOrg from "@/features/subOrg/SubOrg.vue";
import RoleManagement from "@/features/roleManagement/RoleManagement.vue";
import UserManagement from "@/features/userManagement/UserManagement.vue";
import UserPrivilege from "@/features/userManagement/UserPrivilege.vue";
import CourseManagement from "@/features/courseManagement/CourseManagement.vue";
// project
import ProjectManagement from "@/features/projectManagement/ProjectManagement.vue";
import ProjectDataManagement from "@/features/projectDataManagement/ProjectDataManagement.vue";
import ProjectParamsManagement from "@/features/projectParamsManagement/ProjectParamsManagement.vue";
import ProjectPapersManagement from "@/features/projectPapersManagement/ProjectPapersManagement.vue";
import PaperAnalysis from "@/features/paperAnalysis/PaperAnalysis.vue";
import AllAnalysis2 from "@/features/allAnalysis/AllAnalysis2.vue";
import ProjectCompareManagement from "@/features/projectCompareManagement/ProjectCompareManagement.vue";
import ProjectCompareDetail2 from "@/features/projectCompareDetail/ProjectCompareDetail2.vue";
import TaskManagement from "@/features/taskManagement/TaskManagement.vue";
// report
import ReportMain from "@/features/report/ReportMain.vue";

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login, name: "Login" },
  {
    path: "/basic",
    children: [
      {
        path: "rootOrg",
        component: RoutOrg,
      },
      {
        path: "auth",
        component: AuthManagement,
      },
      {
        path: "rootOrg/edit/:orgId",
        component: RootOrgEdit,
      },
      {
        path: "subOrg",
        component: SubOrg,
      },
      {
        path: "role",
        component: RoleManagement,
      },
      {
        path: "user",
        component: UserManagement,
      },
      {
        path: "user/privilege/:userId",
        component: UserPrivilege,
      },
      {
        path: "course",
        component: CourseManagement,
      },
    ],
    component: Layout,
  },
  {
    path: "/project",
    children: [
      {
        path: "projectManagement",
        component: ProjectManagement,
      },
      {
        path: "datasource/:projectId",
        component: ProjectDataManagement,
      },
      {
        path: "params/:projectId",
        component: ProjectParamsManagement,
      },
      {
        path: "papers/:projectId",
        component: ProjectPapersManagement,
      },
      {
        path: ":projectId/paperAnalysis/:paperId",
        component: PaperAnalysis,
      },
      {
        path: "allAnalysis/:projectId",
        component: AllAnalysis2,
      },
      {
        path: "projectCompareManagement",
        component: ProjectCompareManagement,
      },
      {
        path: "projectCompareDetail",
        component: ProjectCompareDetail2,
      },
      {
        path: "taskManagement",
        component: TaskManagement,
      },
    ],
    component: Layout,
  },
  {
    path: "/report/:viewType/:projectId/:paperId/:compareProjectId?",
    name: "PaperReport",
    component: ReportMain,
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: PageError404,
  },
] as RouteRecordRaw[];

// 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("web"),
  routes, // short for `routes: routes`
});

export async function routeLogout(params: {
  cause?: string;
  redirectTo: string;
}) {
  const store = useMainStore();
  return router.push(
    `/login?rootOrgId=${
      store.userInfo.rootOrgId === -1 ? 1 : store.userInfo.rootOrgId
    }&redirectTo=${encodeURI(params.redirectTo)}`
  );
}

router.beforeEach((to, from, next) => {
  const token = getToken();
  if (
    isNil(token) &&
    !to.path.includes("/login") &&
    !to.path.includes("/report")
  ) {
    routeLogout({ cause: "missingToken", redirectTo: to.fullPath })
      .then(() => next(false))
      .catch((e) => console.log(e));
  } else {
    next();
  }
});

export default router;
