import { useEffect, useRef, useState } from 'react'
import { useMachineStore } from '../../../../../store/simulation/useMachineStore';
import { usePlayButtonStore } from '../../../../../store/usePlayButtonStore';
import MachineAnimator from '../animator/machineAnimator';
import { useProductStore } from '../../../../../store/simulation/useProductStore';
import { useSelectedProduct } from '../../../../../store/simulation/useSimulationStore';
import { useTriggerHandler } from '../../../triggers/triggerHandler/useTriggerHandler';

function MachineInstance({ machineDetail }: { machineDetail: MachineStatus }) {
    const [currentPhase, setCurrentPhase] = useState<string>('idle');
    let isIncrememtable = useRef<boolean>(true);
    const { isPlaying } = usePlayButtonStore();
    const { machines, setMachineState, setMachineActive } = useMachineStore();
    const { selectedProduct } = useSelectedProduct();
    const { getActionByUuid } = useProductStore();
    const { triggerPointActions } = useTriggerHandler();

    const reset = () => {
        setCurrentPhase("idle");
        setMachineState(machineDetail.modelUuid, 'idle');
        setMachineActive(machineDetail.modelUuid, false);
        isIncrememtable.current = true;
    }

    function machineStatus(modelId: string, status: string) {
        // console.log(`${modelId} , ${status}`);
    }

    useEffect(() => {
        if (isPlaying) {
            if (!machineDetail.isActive && machineDetail.state === "idle" && currentPhase == "idle" && !machineDetail.currentAction) {
                machineStatus(machineDetail.modelUuid, 'Machine is idle and waiting for next instruction.')
            } else if (!machineDetail.isActive && machineDetail.state === "idle" && currentPhase == "idle" && machineDetail.currentAction) {
                setCurrentPhase("processing");
                setMachineState(machineDetail.modelUuid, 'running');
                setMachineActive(machineDetail.modelUuid, true);
                machineStatus(machineDetail.modelUuid, "Machine started processing")
            }
        }
    }, [currentPhase, isPlaying, machines])

    function handleCallBack() {
        if (currentPhase == "processing") {
            setMachineState(machineDetail.modelUuid, 'idle');
            setMachineActive(machineDetail.modelUuid, false);
            setCurrentPhase("idle")
            isIncrememtable.current = true;
            machineStatus(machineDetail.modelUuid, "Machine has completed the processing")

            if (machineDetail.currentAction) {
                const action = getActionByUuid(selectedProduct.productId, machineDetail.currentAction.actionUuid);
                if (action && machineDetail.currentAction.materialId) {
                    triggerPointActions(action, machineDetail.currentAction.materialId)
                }
            }
        }
    }

    return (
        <>
            <MachineAnimator processingTime={machineDetail.point.action.processTime} handleCallBack={handleCallBack} currentPhase={currentPhase} machineUuid={machineDetail.modelUuid} machineStatus={machineStatus} reset={reset} />

        </>
    )
}

export default MachineInstance