import { useMemo } from "react";
import { Canvas } from "@react-three/fiber";
import { Environment, KeyboardControls, Stars } from "@react-three/drei";

import World from "./world/world";
import Controls from "./controls/controls";
import TransformControl from "./controls/transformControls";
import PostProcessing from "./postProcessing/postProcessing";
import Sun from "./environment/sky";
import CamModelsGroup from "../collaboration/collabCams";
import Shadows from "./environment/shadow";
import MqttEvents from "../../services/factoryBuilder/mqtt/mqttEvents";

import background from "../../assets/textures/hdr/mudroadpuresky2k.hdr";
import SelectionControls from "./controls/selection/selectionControls";
import MeasurementTool from "./tools/measurementTool";
import Simulation from "../simulation/simulation";

// import Simulation from "./simulationtemp/simulation";
import ZoneCentreTarget from "../../components/ui/componets/zoneCameraTarget";
import Dropped3dWidgets from "../../components/ui/componets/Dropped3dWidget";
import ZoneAssets from "../../components/ui/componets/zoneAssets";

export default function Scene() {
  const map = useMemo(
    () => [
      { name: "forward", keys: ["ArrowUp", "w", "W"] },
      { name: "backward", keys: ["ArrowDown", "s", "S"] },
      { name: "left", keys: ["ArrowLeft", "a", "A"] },
      { name: "right", keys: ["ArrowRight", "d", "D"] },
    ],
    []
  );
  const savedTheme: string | null = localStorage.getItem("theme");

  return (
    <KeyboardControls map={map}>
      <Canvas
        eventPrefix="client"
        gl={{ powerPreference: "high-performance", antialias: true }}
        onContextMenu={(e) => {
          e.preventDefault();
        }}
      >
        <Dropped3dWidgets />
        <Controls />
        <TransformControl />
        <SelectionControls />
        <MeasurementTool />
        <World />
        <ZoneCentreTarget />
        <ZoneAssets />
        <Simulation />
        <PostProcessing />
        {savedTheme !== "dark" ? <Sun /> : <></>}
        <Shadows />
        <CamModelsGroup />
        <MqttEvents />
        <Environment files={background} environmentIntensity={1.5} />
      </Canvas>
    </KeyboardControls>
  );
}
