import { useEffect, useState } from "react";
import { useSelectedEventSphere } from "../../../store/simulation/useSimulationStore";
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
import VehicleInstances from "./instances/vehicleInstances";
import VehicleUI from "../spatialUI/vehicle/vehicleUI";
import { useSceneContext } from "../../scene/sceneContext";

function Vehicles() {
    const { vehicleStore } = useSceneContext();
    const { getVehicleById } = vehicleStore();
    const { selectedEventSphere } = useSelectedEventSphere();
    const { isPlaying } = usePlayButtonStore();
    const [isVehicleSelected, setIsVehicleSelected] = useState(false);

    useEffect(() => {
        if (selectedEventSphere) {
            const selectedVehicle = getVehicleById(selectedEventSphere.userData.modelUuid);
            if (selectedVehicle) {
                setIsVehicleSelected(true);
            } else {
                setIsVehicleSelected(false);
            }
        }
    }, [getVehicleById, selectedEventSphere])

    return (
        <>

            <VehicleInstances />

            {isVehicleSelected && selectedEventSphere && !isPlaying &&
                <VehicleUI />
            }

        </>
    );
}

export default Vehicles;
