import React, { useEffect } 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 "../components/ui/componets/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";

const Project: React.FC = () => {
  let navigate = useNavigate();
  const { activeModule } = useModuleStore();
  const { loadingProgress, setLoadingProgress } = 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">
      {loadingProgress && <LoadingPage progress={loadingProgress} />}
      {!isPlaying && (
        <>
          {toggleThreeD && <ModuleToggle />}
          <SideBarLeft />
          <SideBarRight />
        </>
      )}
      {activeModule === "market" && <MarketPlace />}
      <RealTimeVisulization />
      {activeModule !== "market" && <Tools />}
      {isPlaying && activeModule === "simulation" && <SimulationPlayer />}
    </div>
  );
};

export default Project;
