import { createRouter, createWebHistory, isNavigationFailure } from "vue-router";
import Login from "@/views/Login/Login.vue";
import Framework from "@/views/Framework.vue";
import HomeView from "@/views/HomeView.vue";
import Layout from "@/views/KMPlatform/Layout.vue";
import Home from "@/views/KMPlatform/Home/Home.vue";
import KGBuilder from "@/views/KMPlatform/KGBuilder/KGBuilder.vue";
import KnowledgeBase from "@/views/KMPlatform/KnowledgeBase/KnowledgeBase.vue";
import OpenPlatform from "@/views/KMPlatform/OpenPlatform/OpenPlatform.vue";
import TerminologyManagement from "@/views/KMPlatform/KGBuilder/TM/TerminologyManagement.vue";
import EntityTypeManagement from "@/views/KMPlatform/KGBuilder/ETM/EntityTypeManagement.vue";
import KnowledgeBaseManagement from "@/views/KMPlatform/KnowledgeBase/KBM/KnowledgeBaseManagement.vue";
import KnowledgeManagement from "@/views/KMPlatform/KnowledgeBase/KM/KnowledgeManagement.vue";
import AccountManage from "@/views/KMPlatform/Permission/AccountManage.vue";
import PermissionManage from "@/views/KMPlatform/Permission/permission.vue";
import RoleManage from "@/views/KMPlatform/Permission/RoleManage.vue";
import EntityRelationshipTypeManagement from '@/views/KMPlatform/KGBuilder/KRTM/EntityRelationshipTypeManagement.vue'
import { getSessionVar, deleteSessionVar } from "@/utils/session";
import page404 from "@/views/404/404.vue"
import { isNotLogin } from "@/utils/app"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/kmplatform",
      name: "top",
      meta: {
        auth: false,
      }
    },
    {
      path: "/404",
      name: 'page404',
      component: page404,
      meta: {
        auth: false,
      }
    },
    {
      path: "/login",
      name: "login",
      component: Login,
      meta: {
        auth: false,
      }
    },
    {
      path: "/kmplatform",
      name: "kmplatform",
      component: Layout,
      meta: {
        auth: false,
      },
      children: [
        {
          path: "home",
          name: "kmplatform-home",
          meta: {
            title: "主页",
            auth: true,
          },
          component: Home,
          children: [],
        },
        {
          path: "knowledgebase",
          name: "knowledgebase",
          meta: {
            title: "知识库",
            keepAlive: true,
            auth: true,
          },
          component: KnowledgeBase,
          redirect: "/kmplatform/knowledgebase/kbm",
          children: [
            {
              path: "kbm",
              name: "kbm",
              meta: {
                title: "知识库",
                keepAlive: true,
                auth: true,
              },
              component: KnowledgeBaseManagement,
            },
            {
              path: "km/:kbId",
              name: "km",
              meta: {
                title: "知识库",
                keepAlive: false,
                auth: true,
              },
              component: KnowledgeManagement,
            },
          ],
        },
        {
          path: "kgbuilder",
          name: "kgbuilder",
          meta: {
            title: "知识图谱构建",
            auth: true,
          },
          redirect: "/kmplatform/kgbuilder/home",
          component: Framework,
          children: [
            {
              path: "home",
              name: "home",
              component: HomeView,
              meta: {
                auth: true,
              },
            },
            {
              path: "workspace",
              name: "workspace",
              component: () => import("@/views/WorkspaceView.vue"),
              children: [
                {
                  path: "queue/:id",
                  name: "queue",
                  component: () => import("@/views/QueueView.vue"),
                  meta: {
                    auth: true,
                  },
                },
                {
                  path: 'job/:id',
                  name: 'job-view',
                  component: () => import('@/views/JobView.vue'),
                  meta: {
                    auth: true,
                  },
                },
                {
                  path: "worker",
                  name: "worker",
                  component: () => import("@/views/WorkerView.vue"),
                  meta: {
                    auth: true,
                  },
                },
                {
                  path: "graph",
                  name: "graph",
                  component: () => import("@/views/GraphView.vue"),
                  meta: {
                    auth: true,
                  },
                },
                {
                  path: 'graph-std-schemas',
                  name: 'graph-std-schemas',
                  component: () => import('@/views/GraphStdSchemas.vue'),
                  meta: {
                    auth: true,
                  },
                },
                {
                  path: "graph-mgr/:id",
                  name: "graph-mgr",
                  component: () => import("@/views/GraphManagement.vue"),
                  meta: {
                    auth: true,
                  },
                },
              ],
            },
            {
              path: "about",
              name: "about",
              component: () => import("@/views/AboutView.vue"),
              meta: {
                auth: true,
              },
            },
            {
              path: 'config',
              name: 'config',
              component: () => import('@/views/ConfigurationView.vue'),
              meta: {
                auth: true,
              },
            },
          ]
        },
        {
          path: "openplatform",
          name: "openplatform",
          redirect: "/kmplatform/openplatform/queue/0",
          meta: {
            title: "开放平台",
            auth: false,

          },
          component: OpenPlatform,
          children: [
            {
              path: "queue/:id",
              name: "platformText",
              component: () => import("@/views/KMPlatform/OpenPlatform/platformText.vue"),
              meta: {
                auth: false,
              },
            },
          ],
        },
        {
          path: "kgpermission",
          name: "kgpermission",
          meta: {
            title: "系统权限",
            auth: true,
          },
          redirect: "/kmplatform/kgpermission/AccountManage",
          component: PermissionManage,
          children: [
            {
              path: "RoleManage",
              name: "kgb-RoleManage",
              meta: {
                title: "角色管理",
                auth: true,
              },
              component: RoleManage,
            },
            {
              path: "AccountManage",
              name: "kgb-AccountManage",
              meta: {
                title: "账号管理",
                auth: true,
              },
              component: AccountManage,
            },
            {
              path: "Organizational",
              name: "kgb-Organizational",
              meta: {
                title: "账号管理",
                auth: true,
              },
              component: () => import("@/views/KMPlatform/Permission/Organizational.vue"),
            },
          ],
        },
      ],
    },
  ],
});


function hasRoute(name: string) {
  const routes = router.getRoutes()
  // console.log("routes", routes)
  return routes.some((it) => it.name === name)
}

router.beforeEach((to, from, next) => {
  if (hasRoute(to.name as string)) {
    if (isNotLogin()) {
      // console.log("a,", to.meta && to.name !== 'login')
      if (to.meta.auth && to.name !== 'login') {
        next({ name: 'login' });
      } else {
        next()
      }
    } else {
      next()
    }
  } else {
    next('/404')
  }
});

router.afterEach((to, from, failure) => {
  if (isNavigationFailure(failure)) {
    if (to.fullPath === from.fullPath) return;
    console.log('failed navigation', failure)
  } else {
    to.meta.title ? (document.title = to.meta.title as string) : document.title = '知识图谱自动化构建平台'
  }
})

export default router;
