import { useRoutes, Navigate } from 'react-router-dom';
import { lazyLoad } from './lazyLoadAndDelay'; // 添加一个固定的延迟时间，以便你可以看到加载状态

import Layout from '@/layout/index';
import UserModule from '@/pages/user-module';
import UserLogin from '@/pages/user-module/login';
import AuthGroups from '@/pages/setting';

import UserInfo from '@/pages/user-module/userInfo';

import BookModule from '@/pages/books'
import BookClassify from '@/pages/books/classify'
import BookAudit from '@/pages/books/audit/index.jsx'
import AuditTable from '@/pages/books/audit/table.jsx'
import AuditDetail from '@/pages/books/audit/detail.jsx';
import BookLabel from '@/pages/books/label'
// import BookAuditDetail from '@/pages/books/auditDetail'
import BookManagement from '@/pages/books/management/index.jsx'
import BookManagementList from '@/pages/books/management/list'
import BookAddEdit from '@/pages/books/management/addedit';
import BookSale from '@/pages/books/sale';
import SaleDetail from '@/pages/books/sale/edit.jsx';
import SaleTable from '@/pages/books/sale/table.jsx';
import DiscussList from '@/pages/books/sale/discussList';
import DiscussDetail from '@/pages/books/sale/discussDetail';

import BookSection from '@/pages/books/section'
import OrderManagement from '@/pages/books/order-management'
import OrderTable from '@/pages/books/order-management/tab/table.jsx'
import OrderSalesStatistics from '@/pages/books/order-management/tab/salesStatistics';
import User from '@/pages/member'
import UserHome from '@/pages/member/list/list.jsx'
import UserList from '@/pages/member/list'
import Userlevel from '@/pages/member/userlevel';
import UserDetail from '@/pages/member/detail'

import CouponRecord from '@/pages/member/detail/CouponRecord';
import IntegralRecord from '@/pages/member/detail/IntegralRecord';
import OrderRecord from '@/pages/member/detail/orderRecord';

import AdvertisingModule from '@/pages/advertisement'
import Advert from '@/pages/advertisement/Advert'
import Adsense from '@/pages/advertisement/adsense';

import Jurisdiction from '@/pages/jurisdiction';
import Administrator from '@/pages/jurisdiction/admin';
import RoleList from '@/pages/jurisdiction/role';

import QuestionBank from '@/pages/books/question-bank'
import Applied from '@/pages/setting/applied'
import Personalized from '@/pages/setting/personalized-setting'
import Point from '@/pages/setting/point-management';
import AppVersion from '@/pages/setting/app-version';
import Coupon from '@/pages/setting/coupon';
import Sensitive from '@/pages/setting/sensitive';
import FeedBack from '@/pages/setting/feedback'
import Help from '@/pages/setting/help';
import HelpAddEdit from '@/pages/setting/help/addedit/index'
import HelpTable from '@/pages/setting/help/table.jsx';

import Discuss from '@/pages/books/discussion'

import TeacherModule from '@/pages/teacher';

import NotFound from '@/common/notFound'; //公共
import { getMenuList } from '@/common/request';

const authComponent = (Com) => {
  const token = localStorage.getItem('kiwi.token') || true;
  return token ? lazyLoad(Com) : <Navigate to='/user' />;
};

const GetRoutes = () => {
  const [dynamicRoutes, setDynamicRoutes] = useState(null);

  useEffect(() => {
    const fetchData = async () => {
      // 获取菜单列表
      const menuData = await getMenuList();

      // 处理菜单数据，生成动态路由配置
      const dynamicRoutes = generateDynamicRoutes(menuData);

      // 设置动态路由
      setDynamicRoutes(dynamicRoutes);
    };

    fetchData();
  }, []);

  // 如果动态路由尚未加载完成，可以返回一个加载状态或其他内容
  if (!dynamicRoutes) {
    return <div>Loading...</div>;
  }

  // 使用动态路由
  const routes = useRoutes(dynamicRoutes);

  return routes;
};

// 生成动态路由的函数，根据菜单数据生成路由配置
const generateDynamicRoutes = (menuData) => {
  // 这里根据你的菜单数据结构进行处理，生成动态路由配置
  // 返回一个类似于 rootRouter 的结构，但基于动态数据

  // 示例：假设菜单数据的格式为 [{ path: '/dynamic', title: '动态菜单', children: [...] }]
  const dynamicRoutes = menuData.map((menuItem) => {
    return {
      path: menuItem.path,
      key: menuItem.key, // 确保每个路由有唯一的 key
      element: lazyLoad(getComponentByType(menuItem.type)), // 根据类型获取组件，你可能需要自己实现这个函数
      meta: {
        title: menuItem.title,
      },
      children: generateDynamicRoutes(menuItem.children), // 递归处理子菜单
    };
  });

  return dynamicRoutes;
};

// 根据菜单项的类型返回对应的组件，你可能需要根据实际情况自行实现这个函数
const getComponentByType = (type) => {
  // 根据类型返回对应的组件
  // 例如：假设你的菜单项有 type 字段，根据不同的 type 返回不同的组件
  switch (type) {
    case 'book':
      return BookModule;
    case 'user':
      return User;
    // 其他类型的组件处理...
    default:
      return NotFound; // 默认返回 NotFound 组件
  }
};

export default GetRoutes;
