/**
 * 1. 前端维护的完整路由表, 用于创建"菜单"
 * 2. component 是 string，并不是 组件对象。
 * 3. 生成菜单，关注的 path，name，component，icon。
 * 4. 部分路由组件是菜单不可见的，当访问该路由时，左侧菜单的menu item会失去active状态。需要设置 meta.activeMenu 保持选中状态。
 */
import { AppRouteRecordRaw } from './types';
import { getTreeItem } from '@/utils';
import { cloneDeep } from 'lodash-es';
import { PageEnum } from '@/enums/pageEnum';
import {
  disasterPreventionRoute,
  emergencyManagementRoute,
  platformRoutes,
  largeScreenRoutes,
  trafficRoutes,
  securityConfidentialityRoutes,
  campusRoutes,
  productionSafetyRoutes,
} from './routers';
import { checkCBrainOAuthToken } from '@/utils/checkCBrainOAuth';

export type RouteRecordString = Omit<AppRouteRecordRaw, 'component'> & { component?: string };

export const RootRoute: RouteRecordString = {
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_HOME,
  meta: {
    title: 'Root',
  },
};

export const HOME_PAGE: RouteRecordString = {
  // 模板管理
  path: '/home',
  name: 'HomePage',
  component: '/home/PageHome',
  meta: {
    icon: '',
    title: '首页',
  },
};

/** C大脑 OAuth2.0 单点登录入口（无 code 会重定向到授权页，有 code 会换 token 后跳首页） */
export const CbrainEntryRoute: RouteRecordString = {
  path: '/cbrain-entry',
  name: 'cbrain-entry',
  component: '/entry/Entry',
  beforeEnter: (to, from, next) => {
    checkCBrainOAuthToken(to, from, next);
  },
  meta: {
    title: 'C大脑登录',
  },
};

export const fullRoutes: AppRouteRecordRaw[] = [
  disasterPreventionRoute,
  emergencyManagementRoute,
  platformRoutes,
  largeScreenRoutes,
  trafficRoutes,
  securityConfidentialityRoutes,
  campusRoutes,
  productionSafetyRoutes,
] as const;

/**
 * 类型定义。避免与已有的定义冲突，避免与 vue-router的类型定义冲突，前面加 _ 前缀
 */
export interface _RouteViewItem {
  label: string;
  value: string;
}
export type _RouteView = _RouteViewItem & { children?: _RouteViewItem[] | null };
export type _RouteViewTree = _RouteView[];

/**
 * 获取指定 父路由 下的子路由。
 * 若 父级路由 未指定，则返回一级路由
 */
export function getChildRoutesView(parentRouteName?: string | null): _RouteViewItem[] {
  // 未指定 parentRouteName， 则返回 level1 的路由
  if (parentRouteName == null) {
    return fullRoutes.map((route) => ({
      label: route.meta?.title as string,
      value: route.name,
    }));
  }

  // 获取指定父路由的子路由
  const parentRoute: AppRouteRecordRaw = cloneDeep(getTreeItem(fullRoutes, parentRouteName, 'name'));
  if (parentRoute.children && parentRoute.children.length > 0) {
    return parentRoute.children.map((route) => ({
      label: route.meta?.title as string,
      value: route.name,
    }));
  } else {
    return [];
  }
}

/**
 * 获取指定的 路由信息
 */
export function getRouteByName(routeName: string) {
  return cloneDeep(getTreeItem(fullRoutes, routeName, 'name')) as AppRouteRecordRaw;
}

/**
 * 转换成 el-tree-select 的数据结构形式
 */
export function transformToRouteViewTree(routes?: AppRouteRecordRaw[] | null) {
  const tree: _RouteViewTree = [];
  if (routes == null) return tree;

  for (const route of routes) {
    const treeItem: _RouteView = {
      value: route.name,
      label: route.meta.title as string,
      children: null,
    };

    if (route.children && route.children.length > 0) {
      treeItem.children = transformToRouteViewTree(route.children);
      tree.push(treeItem);
    } else {
      tree.push(treeItem);
    }
  }

  return tree;
}

export const routeViewTree = transformToRouteViewTree(fullRoutes);
