import React, { useEffect, useState } from 'react'
import IKInstance from '../ikInstance/ikInstance';
import RoboticArmAnimator from '../animator/roboticArmAnimator';
import { usePlayButtonStore } from '../../../../../store/usePlayButtonStore';
import { useArmBotStore } from '../../../../../store/simulation/useArmBotStore';

function RoboticArmInstance({ armBot }: any) {
    const { isPlaying } = usePlayButtonStore();
    const [currentPhase, setCurrentPhase] = useState<(string)>("init");
    // console.log('currentPhase: ', currentPhase);
    const { armBots, addArmBot, addCurrentAction } = useArmBotStore();

    useEffect(() => {

        // console.log('isPlaying: ', isPlaying);
        if (isPlaying) {
            //Moving armBot from initial point to rest position.

            if (armBot?.isActive && armBot?.state == "idle" && currentPhase == "init") {
                addCurrentAction(armBot.modelUuid, 'action-001');
                setCurrentPhase("moving-to-rest");

            }
            //Waiting for trigger.
            if (!armBot?.isActive && armBot?.state == "idle" && currentPhase == "moving-to-rest") {
                setCurrentPhase("rest");
            }
            // Moving armBot from rest position to pick up point.
            if (!armBot?.isActive && armBot?.state == "idle" && currentPhase == "rest") {

            }
            //Moving arm from start point to end point.
            if (armBot?.isActive && armBot?.state == "running " && currentPhase == "rest-to-start ") {

            }
            //Moving arm from end point to idle. 
            if (armBot?.isActive && armBot?.state == "running" && currentPhase == "end-to-start") {

            }

        }

    }, [currentPhase, armBot, isPlaying])

    const HandleCallback = () => {
        if (armBot.isActive && armBot.state == "idle" && currentPhase == "init") {
            addCurrentAction('armbot-xyz-001', 'action-001');
        }
    }

    return (
        <>

            <IKInstance />
            <RoboticArmAnimator armUuid={armBot?.modelUuid} HandleCallback={HandleCallback} currentPhase={currentPhase} />

        </>
    )
}

export default RoboticArmInstance;