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

function RoboticArm() {
    const { getArmBotById } = useArmBotStore();
    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;