import React, { useEffect, useContext, useState } from "react";
import { Routes, Route } from "react-router-dom";
// import { AuthContext } from "Context/Auth";
// import { GlobalContext } from "Context/Global";

import PrivateRoute from "./PrivateRoutes";
import PublicRoute from "./PublicRoutes";
import { PublicWrapper } from "Components/PublicWrapper";
import { NotFoundPage } from "Pages/404";
import { SnackBar } from "Components/SnackBar";
import { SessionExpiredModal } from "Components/SessionExpiredModal";

// generatePagesRoutes
import { AdminWrapper } from "Components/AdminWrapper";

import {
 
  AddAdminSettingTablePage,
  AddAdminSowTablePage,
 
  AddAdminWireframeTablePage,
  AdminForgotPage,
  AdminLoginPage,
  AdminProfilePage,
  AdminResetPage,
 
  EditAdminProjectTablePage,
  EditAdminSettingTablePage,
  EditAdminSowTablePage,
 
  AdminEmailListPage,
 
  AdminUserListPage,
  AskGptAdminSowTablePage,
 
  ListAdminDeploymentTablePage,
  AdminBaasPage,
  ListAdminProjectTablePage,
  ListAdminSettingTablePage,
  ListAdminSowTablePage,
  ListAdminWireframeTablePage,
 
  AdminDashboardPage,
 
  LandingPage,
  Pay,
  PreWireframeFlow,
  ViewAdminDeploymentTablePage,
  ViewAdminProjectTablePage,
  ViewAdminSettingTablePage,
  ViewAdminSowTablePage,
  ViewAdminWireframeTablePage,
  MagicLoginVerifyPage,
  UserMagicLoginPage,
  TestGPT,
 
  Postman,
 
  GenerateUI,
  GenerateQuery
} from "./LazyLoad";
import { GlobalContext } from "Context/Global";
import { AuthContext } from "Context/Auth";
import { ModalSidebar } from "Components/ModalSidebar";
 
import EditWireframePage from "Pages/Admin/Edit/EditWireframePage";
 

export const DynamicWrapper = ({ isAuthenticated, role, children }) => {
  if (!isAuthenticated) {
    return <PublicWrapper>{children}</PublicWrapper>;
  }
  if (isAuthenticated) {
    if (role === "admin") {
      return <AdminWrapper>{children}</AdminWrapper>;
    }
  }
};

export const NotFound = ({ isAuthenticated, role }) => {
  if (!isAuthenticated) {
    return (
      <PublicWrapper>
        <NotFoundPage />
      </PublicWrapper>
    );
  }
  if (isAuthenticated) {
    if (role === "admin") {
      return (
        <AdminWrapper>
          <NotFoundPage />
        </AdminWrapper>
      );
    }
  }
};

export default () => {
  const { state } = useContext(AuthContext);
  const {
    state: { isOpen },
    dispatch,
  } = useContext(GlobalContext);
  const [screenSize, setScreenSize] = useState(window.innerWidth);

  function setDimension(e) {
    if (e.currentTarget.innerWidth >= 1024) {
      toggleSideBar(true);
    } else toggleSideBar(false);
    setScreenSize(e.currentTarget.innerWidth);
  }

  // const toTop = () => {
  //   containerRef.current.scrollTo(0, 0);
  // };

  const toggleSideBar = (open) => {
    if (isOpen && screenSize < 1024) {
      dispatch({
        type: "OPEN_SIDEBAR",
        payload: { isOpen: open },
      });
    } else if (!isOpen && screenSize >= 1024) {
      dispatch({
        type: "OPEN_SIDEBAR",
        payload: { isOpen: open },
      });
    }
  };

  useEffect(() => {
    window.addEventListener("resize", setDimension);

    return () => {
      window.removeEventListener("resize", setDimension);
    };
  }, [screenSize]);

  return (
    <div
      onClick={() => {
        isOpen ? toggleSideBar(false) : null;
      }}
      className={`h-screen overflow-x-hidden bg-white`}
    >
      <Routes>
        <Route
          exact
          path="/admin/profile"
          element={
            <PrivateRoute
              access={"admin"}
              path={"/admin/profile"}
              element={
                <AdminWrapper>
                  <AdminProfilePage />
                </AdminWrapper>
              }
            />
          }
        />
        
        
        <Route
          path="/admin/email"
          element={
            <PrivateRoute
              access={"admin"}
              path={"/admin/email"}
              element={
                <AdminWrapper>
                  <AdminEmailListPage />
                </AdminWrapper>
              }
            />
          }
        />
 

        <Route
          exact
          path="/admin/add-sow"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/add-sow"}
              element={
                <AdminWrapper>
                  <AddAdminSowTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/edit-sow/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/edit-sow/:id"}
              element={
                <AdminWrapper>
                  <EditAdminSowTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/view-sow/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/view-sow/:id"}
              element={
                <AdminWrapper>
                  <ViewAdminSowTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/ask-gpt/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/ask-gpt/:id"}
              element={
                <AdminWrapper>
                  <AskGptAdminSowTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/requirements"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/requirements"}
              element={
                <AdminWrapper>
                  <ListAdminSowTablePage />
                </AdminWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/admin/generate-ui/:component"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/generate-ui/:component"}
              element={
                <AdminWrapper>
                  <GenerateUI />
                </AdminWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/admin/generate-query/:query"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/generate-query/:query"}
              element={
                <AdminWrapper>
                  <GenerateQuery />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/requirement/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/requirement/:id"}
              element={
                <AdminWrapper>
                  <PreWireframeFlow />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/add-setting"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/add-setting"}
              element={
                <AdminWrapper>
                  <AddAdminSettingTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/edit-setting/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/edit-setting/:id"}
              element={
                <AdminWrapper>
                  <EditAdminSettingTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/view-setting/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/view-setting/:id"}
              element={
                <AdminWrapper>
                  <ViewAdminSettingTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/setting"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/setting"}
              element={
                <AdminWrapper>
                  <ListAdminSettingTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/add-wireframe"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/add-wireframe"}
              element={
                <AdminWrapper>
                  <AddAdminWireframeTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/view-wireframe/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/view-wireframe/:id"}
              element={
                <AdminWrapper>
                  <ViewAdminWireframeTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/build"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/build"}
              element={
                <AdminWrapper>
                  <ListAdminWireframeTablePage />
                </AdminWrapper>
              }
            />
          }
        />
 
        <Route
          exact
          path="/admin/edit-project/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/edit-project/:id"}
              element={
                <AdminWrapper>
                  <EditAdminProjectTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/view-project/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/view-project/:id"}
              element={
                <AdminWrapper>
                  <ViewAdminProjectTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/baas"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/baas"}
              element={
                <AdminWrapper>
                  <AdminBaasPage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/project"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/project"}
              element={
                <AdminWrapper>
                  <ListAdminProjectTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/users"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/users"}
              element={
                <AdminWrapper>
                  <AdminUserListPage />
                </AdminWrapper>
              }
            />
          }
        />
 

        <Route
          exact
          path="/admin/deployment"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/deployment"}
              element={
                <AdminWrapper>
                  <ListAdminDeploymentTablePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/view-deployment/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/view-deployment/:id"}
              element={
                <AdminWrapper>
                  <ViewAdminDeploymentTablePage />
                </AdminWrapper>
              }
            />
          }
        />
 

        <Route
          exact
          path="/admin/edit-wireframe/:id"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/edit-wireframe/:id"}
              element={
                <AdminWrapper>
                  <EditWireframePage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/admin/dashboard"
          element={
            <PrivateRoute
              access="admin"
              path={"/admin/dashboard"}
              element={
                <AdminWrapper>
                  <AdminDashboardPage />
                </AdminWrapper>
              }
            />
          }
        />

        <Route
          exact
          path="/"
          element={
            <PublicRoute
              path={"/"}
              element={
                <PublicWrapper>
                  <LandingPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/admin/login"
          element={
            <PublicRoute
              path={"/admin/login"}
              element={
                <PublicWrapper>
                  <AdminLoginPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/admin/forgot"
          element={
            <PublicRoute
              path={"/admin/forgot"}
              element={
                <PublicWrapper>
                  <AdminForgotPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/admin/reset"
          element={
            <PublicRoute
              path={"/admin/reset"}
              element={
                <PublicWrapper>
                  <AdminResetPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/magic-login"
          element={
            <PublicRoute
              path={"/magic-login"}
              element={
                <PublicWrapper>
                  <UserMagicLoginPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/magic-login/verify"
          element={
            <PublicRoute
              path={"/magic-login/verify"}
              element={
                <PublicWrapper>
                  <MagicLoginVerifyPage />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/pay"
          element={
            <PublicRoute
              path={"/pay"}
              element={
                <PublicWrapper>
                  <Pay />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/gpt"
          element={
            <PublicRoute
              path={"/gpt"}
              element={
                <PublicWrapper>
                  <TestGPT />
                </PublicWrapper>
              }
            />
          }
        />
        <Route
          exact
          path="/tools/postman"
          element={
            <PublicRoute
              path={"/tools/postman"}
              element={
                <PublicWrapper>
                  <Postman />
                </PublicWrapper>
              }
            />
          }
        />

        {/* Custom Routes */}

        <Route
          path={"*"}
          element={<PublicRoute path={"*"} element={<NotFound {...state} />} />}
        />
      </Routes>
      <SessionExpiredModal />
      <SnackBar />
      {/* <ModalPromptForm/> */}
      <ModalSidebar />
    </div>
  );
};
