import React from 'react';
import { createBrowserRouter, Navigate, useParams } from 'react-router';
import { ProtectedRoute } from './components/ProtectedRoute';
import { ErrorPage } from './components/ErrorPage';
import { NotFoundPage } from './components/NotFoundPage';
// import { MainLayout } from './layouts/MainLayout';
import { ClassroomPage } from './pages/ClassroomPage';
// import { SubmissionRecordsPage } from './pages/SubmissionRecordsPage';
// import { StockDataPage } from './pages/StockDataPage';
// import { StockXunlianCodesPage } from './pages/StockXunlianCodesPage';
// import { DateNotesPage } from './pages/DateNotesPage';
import AuthPage from './pages/AuthPage';
import WechatCallbackPage from './pages/WechatCallbackPage';
import StockHomePage from './pages/StockHomePage';
import { XunlianPage } from './pages/XunlianPage';
import { StockMain } from './components/stock/stock_main';
import ExamIndex from './components/Exam/ExamIndex';
import ExamAdmin from './components/Exam/ExamAdmin';
import ExamCard from './components/Exam/ExamCard';
import MemberPage from './pages/MemberPage';
import VideoReplayPage from './pages/VideoReplayPage';
import VideoPlayerPage from './pages/VideoPlayerPage';
import { MainLayout } from './layouts/MainLayout';
import { getGlobalConfig } from '../utils/utils';
import { UserType } from '@/server/modules/users/user.enum';
import { useAuth } from './hooks/AuthProvider';

const PublicClassroomPage = () => <ClassroomPage />;
const PrivateClassroomPage = () => <ClassroomPage />;
const RouteClassroomPage = () => {
  let params = useParams()
  if( params.classId == getGlobalConfig('PUBLIC_CHATROOM_ID'))
    return <PublicClassroomPage />
  else
    return <PrivateClassroomPage />
}
const HomeClassroomPage = () => {
  const { user } = useAuth();

  const getUrl = (classId = '') => {
    if (user?.userType === UserType.TEACHER) {
      return `/mobile/classroom/${classId}/${UserType.TEACHER}`;
    } else {
      return `/mobile/classroom/${classId}/${UserType.STUDENT}`;
    }
  };

  return <Navigate to={getUrl(getGlobalConfig('PUBLIC_CHATROOM_ID'))} replace />
};

export const router = createBrowserRouter([
  {
    path: '/',
    element: <Navigate to="/mobile" replace />
  },
  {
    path: '/mobile/login',
    element: <AuthPage />
  },
  {
    path: '/mobile/auth/callback',
    element: <WechatCallbackPage />
  },
  {
    path: '/mobile/register',
    element: <Navigate to="/mobile/login" replace />
  },
  {
    path: '/mobile',
    element: (
      <ProtectedRoute>
        <MainLayout />
      </ProtectedRoute>
    ),
    children: [
      {
        path: '',
        element: <HomeClassroomPage />
      },
      {
        path: 'member',
        element: <MemberPage />
      },
      {
        path: 'classroom/:classId/:role',
        element: <ProtectedRoute><RouteClassroomPage /></ProtectedRoute>,
        errorElement: <ErrorPage />
      },
      {
        path: 'classroom/:role',
        element: <ProtectedRoute><ClassroomPage /></ProtectedRoute>,
        errorElement: <ErrorPage />
      },
      {
        path: 'classroom',
        element: <ProtectedRoute><ClassroomPage /></ProtectedRoute>,
        errorElement: <ErrorPage />
      },
      {
        path: 'stock',
        element: <ProtectedRoute><StockMain /></ProtectedRoute>,
        errorElement: <ErrorPage />
      },
      {
        path: 'xunlian',
        element: (
          <ProtectedRoute>
            <XunlianPage />
          </ProtectedRoute>
        ),
      },
      {
        path: 'video-replay',
        element: (
          <ProtectedRoute>
            <VideoReplayPage />
          </ProtectedRoute>
        ),
        errorElement: <ErrorPage />
      },
      {
        path: 'video/:id',
        element: (
          <ProtectedRoute>
            <VideoPlayerPage />
          </ProtectedRoute>
        ),
        errorElement: <ErrorPage />
      },
      {
        path: '*',
        element: <NotFoundPage />,
        errorElement: <ErrorPage />
      },
    ],
  },
  // {
  //   path: '/mobile/classroom',
  //   element: (
  //     <ProtectedRoute>
  //       <ClassroomPage />
  //     </ProtectedRoute>
  //   ),
  // },
  {
    path: '/mobile/exam/card',
    element: <ProtectedRoute><ExamCard /></ProtectedRoute>,
    errorElement: <ErrorPage />
  },
  {
    path: '/mobile/exam/:id',
    element: <ProtectedRoute><ExamAdmin /></ProtectedRoute>,
    errorElement: <ErrorPage />
  },
  {
    path: '/mobile/exam',
    element: <ProtectedRoute><ExamIndex /></ProtectedRoute>,
    errorElement: <ErrorPage />
  },
  {
    path: '*',
    element: <NotFoundPage />,
    errorElement: <ErrorPage />
  },
]);