import React, { useEffect, useState } from "react";
import ModuleToggle from "../components/ui/ModuleToggle";
import SideBarLeft from "../components/layout/sidebarLeft/SideBarLeft";
import SideBarRight from "../components/layout/sidebarRight/SideBarRight";
import useModuleStore, { useThreeDStore } from "../store/useModuleStore";
import RealTimeVisulization from "../modules/visualization/RealTimeVisulization";
import Tools from "../components/ui/Tools";
// import Scene from "../modules/scene/scene";
import {
  useSocketStore,
  useFloorItems,
  useOrganization,
  useUserName,
  useWallItems,
  useZones,
  useLoadingProgress,
} from "../store/store";
import { useNavigate } from "react-router-dom";
import { usePlayButtonStore } from "../store/usePlayButtonStore";
import MarketPlace from "../modules/market/MarketPlace";
import LoadingPage from "../components/templates/LoadingPage";
import SimulationPlayer from "../components/ui/simulation/simulationPlayer";
import RenderOverlay from "../components/templates/Overlay";
import MenuBar from "../components/ui/menu/menu";
import KeyPressListener from "../utils/shortcutkeys/handleShortcutKeys";

const Project: React.FC = () => {
  let navigate = useNavigate();
  const { activeModule } = useModuleStore();
  const { loadingProgress } = useLoadingProgress();
  const { setUserName } = useUserName();
  const { setOrganization } = useOrganization();
  const { setFloorItems } = useFloorItems();
  const { setWallItems } = useWallItems();
  const { setZones } = useZones();

  useEffect(() => {
    setFloorItems([]);
    setWallItems([]);
    setZones([]);
    const email = localStorage.getItem("email");
    if (email) {
      const Organization = email!.split("@")[1].split(".")[0];
      useSocketStore.getState().initializeSocket(email, Organization);
      const name = localStorage.getItem("userName");
      if (Organization && name) {
        setOrganization(Organization);
        setUserName(name);
      }
    } else {
      navigate("/");
    }
  }, []);
  const { isPlaying } = usePlayButtonStore();
  const { toggleThreeD } = useThreeDStore();

  return (
    <div className="project-main">
      <KeyPressListener />
      {loadingProgress && <LoadingPage progress={loadingProgress} />}
      {!isPlaying && (
        <>
          {toggleThreeD && <ModuleToggle />}
          <SideBarLeft />
          <SideBarRight />
        </>
      )}
      {/* <RenderOverlay>
        <MenuBar setOpenMenu={setOpenMenu} />
      </RenderOverlay> */}
      {activeModule === "market" && <MarketPlace />}
      <RealTimeVisulization />
      {activeModule !== "market" && <Tools />}
      {isPlaying && activeModule === "simulation" && <SimulationPlayer />}
    </div>
  );
};

export default Project;
