import { useEffect, useState } from "react";
import { useSelectedEventSphere } from "../../../store/simulation/useSimulationStore";
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
import RoboticArmInstances from "./instances/roboticArmInstances";
import ArmBotUI from "../spatialUI/arm/armBotUI";
import { useSceneContext } from "../../scene/sceneContext";

function RoboticArm() {
    const {armBotStore} = useSceneContext();
    const { getArmBotById } = armBotStore();
    const { selectedEventSphere } = useSelectedEventSphere();
    const { isPlaying } = usePlayButtonStore();
    const [isArmBotSelected, setIsArmBotSelected] = useState(false);

    useEffect(() => {
        if (selectedEventSphere) {
            const selectedArmBot = getArmBotById(selectedEventSphere.userData.modelUuid);
            if (selectedArmBot) {
                setIsArmBotSelected(true);
            } else {
                setIsArmBotSelected(false);
            }
        }
    }, [getArmBotById, selectedEventSphere])

    return (
        <>

            <RoboticArmInstances />

            {isArmBotSelected && !isPlaying &&
                < ArmBotUI />
            }

        </>
    );
}

export default RoboticArm;