import React, { useEffect, useState } from "react";
import Header from "./Header";
import useModuleStore, {
  useSubModuleStore,
} from "../../../store/useModuleStore";
import {
  AnalysisIcon,
  MechanicsIcon,
  PropertiesIcon,
  SimulationIcon,
} from "../../icons/SimulationIcons";
import useToggleStore from "../../../store/useUIToggleStore";
import ConveyorMechanics from "./mechanics/ConveyorMechanics";
import Visualization from "./visualization/Visualization";
import Analysis from "./analysis/Analysis";
import Simulations from "./simulation/Simulations";
import {
  useSelectedActionSphere,
  useselectedFloorItem,
} from "../../../store/store";
import GlobalProperties from "./properties/GlobalProperties";
import AsstePropertiies from "./properties/AssetProperties";
import ZoneProperties from "./properties/ZoneProperties";
import VehicleMechanics from "./mechanics/VehicleMechanics";
import StaticMachineMechanics from "./mechanics/StaticMachineMechanics";
import ArmBotMechanics from "./mechanics/ArmBotMechanics";

const SideBarRight: React.FC = () => {
  const { activeModule } = useModuleStore();
  const { toggleUI } = useToggleStore();
  const { selectedActionSphere } = useSelectedActionSphere();
  const { subModule, setSubModule } = useSubModuleStore();
  const { selectedFloorItem } = useselectedFloorItem();
  // Reset activeList whenever activeModule changes
  useEffect(() => {
    if (activeModule !== "simulation") setSubModule("properties");
    if (activeModule === "simulation") setSubModule("mechanics");
  }, [activeModule]);

  return (
    <div className="sidebar-right-wrapper">
      <Header />
      {toggleUI && (
        <div className="sidebar-actions-container">
          {/* {activeModule === "builder" && ( */}
          <div
            className={`sidebar-action-list ${subModule === "properties" ? "active" : ""
              }`}
            onClick={() => setSubModule("properties")}
          >
            <PropertiesIcon isActive={subModule === "properties"} />
          </div>
          {/* )} */}
          {activeModule === "simulation" && (
            <>
              <div
                className={`sidebar-action-list ${subModule === "mechanics" ? "active" : ""
                  }`}
                onClick={() => setSubModule("mechanics")}
              >
                <MechanicsIcon isActive={subModule === "mechanics"} />
              </div>
              <div
                className={`sidebar-action-list ${subModule === "simulations" ? "active" : ""
                  }`}
                onClick={() => setSubModule("simulations")}
              >
                <SimulationIcon isActive={subModule === "simulations"} />
              </div>
              <div
                className={`sidebar-action-list ${subModule === "analysis" ? "active" : ""
                  }`}
                onClick={() => setSubModule("analysis")}
              >
                <AnalysisIcon isActive={subModule === "analysis"} />
              </div>
            </>
          )}
        </div>
      )}
      {/* process builder */}
      {toggleUI &&
        subModule === "properties" &&
        activeModule !== "visualization" &&
        !selectedFloorItem && (
          <div className="sidebar-right-container">
            <div className="sidebar-right-content-container">
              <GlobalProperties />
            </div>
          </div>
        )}
      {toggleUI &&
        subModule === "properties" &&
        activeModule !== "visualization" &&
        selectedFloorItem && (
          <div className="sidebar-right-container">
            <div className="sidebar-right-content-container">
              <AsstePropertiies />
            </div>
          </div>
        )}
      {toggleUI &&
        subModule === "zoneProperties" &&
        activeModule === "builder" && (
          <div className="sidebar-right-container">
            <div className="sidebar-right-content-container">
              <ZoneProperties />
            </div>
          </div>
        )}
      {/* simulation */}

      {toggleUI && activeModule === "simulation" && (
        <>
          {subModule === "mechanics" &&
            selectedActionSphere &&
            selectedActionSphere.path.type === "Conveyor" && (
              <div className="sidebar-right-container">
                <div className="sidebar-right-content-container">
                  <ConveyorMechanics />
                </div>
              </div>
            )}
          {subModule === "mechanics" &&
            selectedActionSphere &&
            selectedActionSphere.path.type === "Vehicle" && (
              <div className="sidebar-right-container">
                <div className="sidebar-right-content-container">
                  <VehicleMechanics />
                </div>
              </div>
            )}
          {subModule === "mechanics" &&
            selectedActionSphere &&
            selectedActionSphere.path.type === "StaticMachine" && (
              <div className="sidebar-right-container">
                <div className="sidebar-right-content-container">
                  <StaticMachineMechanics /> 
                </div>
              </div>
            )}
          {subModule === "mechanics" &&
            selectedActionSphere &&
            selectedActionSphere.path.type === "ArmBot" && (
              <div className="sidebar-right-container">
                <div className="sidebar-right-content-container">
                  <ArmBotMechanics /> 
                </div>
              </div>
            )}
          {subModule === "mechanics" && !selectedActionSphere && (
            <div className="sidebar-right-container">
              <div className="sidebar-right-content-container">
                <ConveyorMechanics /> {/*  default */}
              </div>
            </div>
          )}
          {subModule === "analysis" && (
            <div className="sidebar-right-container">
              <div className="sidebar-right-content-container">
                <Analysis />
              </div>
            </div>
          )}
          {subModule === "simulations" && (
            <div className="sidebar-right-container">
              <div className="sidebar-right-content-container">
                <Simulations />
              </div>
            </div>
          )}
        </>
      )}

      {/* realtime visualization */}
      {toggleUI && activeModule === "visualization" && <Visualization />}
    </div>
  );
};

export default SideBarRight;
