import { useCallback } from 'react';
import { useActionHandler } from '../../actions/useActionHandler';
import { useArmBotEventManager } from '../../roboticArm/eventManager/useArmBotEventManager';
import { useConveyorEventManager } from '../../conveyor/eventManager/useConveyorEventManager';
import { useVehicleEventManager } from '../../vehicle/eventManager/useVehicleEventManager';
import { useMachineEventManager } from '../../machine/eventManager/useMachineEventManager';
import { useSceneContext } from '../../../scene/sceneContext';
import { useProductContext } from '../../products/productContext';
import { useHumanEventManager } from '../../human/eventManager/useHumanEventManager';
import { useCraneEventManager } from '../../crane/eventManager/useCraneEventManager';

export function useTriggerHandler() {
    const { materialStore, armBotStore, machineStore, conveyorStore, vehicleStore, humanStore, craneStore, storageUnitStore, productStore } = useSceneContext();
    const { selectedProductStore } = useProductContext();
    const { handleAction } = useActionHandler();
    const { selectedProduct } = selectedProductStore();
    const { getEventByTriggerUuid, getEventByModelUuid, getActionByUuid, getModelUuidByActionUuid } = productStore();
    const { getArmBotById } = armBotStore();
    const { getConveyorById } = conveyorStore();
    const { addArmBotToMonitor } = useArmBotEventManager();
    const { addConveyorToMonitor } = useConveyorEventManager();
    const { addVehicleToMonitor } = useVehicleEventManager();
    const { addMachineToMonitor } = useMachineEventManager();
    const { addHumanToMonitor } = useHumanEventManager();
    const { addCraneToMonitor } = useCraneEventManager();
    const { getVehicleById } = vehicleStore();
    const { getHumanById, setHumanScheduled } = humanStore();
    const { getCraneById, setCraneScheduled, addCurrentAction } = craneStore();
    const { getMachineById, setMachineActive } = machineStore();
    const { getStorageUnitById } = storageUnitStore();
    const { getMaterialById, setCurrentLocation, setNextLocation, setPreviousLocation, setIsPaused, setIsVisible, setEndTime } = materialStore();

    const handleTrigger = (trigger: TriggerSchema, action: Action, materialId?: string) => {

        const fromEvent = getEventByTriggerUuid(selectedProduct.productUuid, trigger.triggerUuid);

        const toEvent = getEventByModelUuid(selectedProduct.productUuid, trigger.triggeredAsset?.triggeredModel.modelUuid || '');

        if (fromEvent?.type === 'transfer') {
            if (toEvent?.type === 'transfer') {
                // Transfer to Transfer
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        if (material.next) {

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, {
                                modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                                pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            });
                        }
                        handleAction(action, materialId);
                    }
                }
            } else if (toEvent?.type === 'vehicle') {
                // Transfer to Vehicle
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // Handle current action of the material
                        handleAction(action, materialId);

                        if (material.next) {
                            const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                            const vehicle = getVehicleById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, null);

                            if (action) {

                                if (vehicle) {

                                    if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                        setIsVisible(materialId, false);

                                        // Handle current action from vehicle
                                        handleAction(action, materialId);

                                    } else {

                                        // Handle current action using Event Manager
                                        addVehicleToMonitor(vehicle.modelUuid, () => {
                                            setIsVisible(materialId, false);
                                            handleAction(action, materialId);
                                        })
                                    }
                                }
                            }
                        }
                    }
                }

            } else if (toEvent?.type === 'machine') {
                // Transfer to Machine

            } else if (toEvent?.type === 'roboticArm') {
                // Transfer to Robotic Arm
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // Handle current action of the material
                        handleAction(action, materialId);

                        if (material.next) {
                            const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                            const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, null);

                            if (action) {

                                if (armBot) {
                                    if (action && action.triggers.length > 0 &&
                                        action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                                        const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                        if (model?.type === 'vehicle') {
                                            const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (armBot) {
                                                if (armBot.isActive === false && armBot.state === 'idle') {
                                                    const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);
                                                    if (vehicle) {
                                                        if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {
                                                            // Handle current action from vehicle
                                                            setIsPaused(materialId, true);
                                                            handleAction(action, materialId);

                                                        } else {

                                                            // Handle current action using Event Manager
                                                            setIsPaused(materialId, true);

                                                            addVehicleToMonitor(vehicle.modelUuid,
                                                                () => {
                                                                    handleAction(action, materialId);
                                                                }
                                                            )
                                                        }
                                                    }
                                                } else {
                                                    setIsPaused(materialId, true);
                                                    addArmBotToMonitor(armBot.modelUuid, () => {
                                                        const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                        if (vehicle) {
                                                            if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {
                                                                // Handle current action from vehicle
                                                                setIsPaused(materialId, true);
                                                                handleAction(action, materialId);

                                                            } else {

                                                                // Handle current action using Event Manager
                                                                setIsPaused(materialId, true);

                                                                addVehicleToMonitor(vehicle.modelUuid,
                                                                    () => {
                                                                        handleAction(action, materialId);
                                                                    }
                                                                )
                                                            }
                                                        }
                                                    })
                                                }
                                            }
                                        } else if (model?.type === 'machine') {
                                            const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (armBot) {
                                                if (armBot.isActive === false && armBot.state === 'idle') {
                                                    const machine = getMachineById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);
                                                    if (machine) {
                                                        if (machine.isActive === false && machine.state === 'idle' && !machine.currentAction) {
                                                            setIsPaused(materialId, true);
                                                            handleAction(action, materialId);
                                                        } else {

                                                            // Handle current action using Event Manager
                                                            setIsPaused(materialId, true);

                                                            addMachineToMonitor(machine.modelUuid,
                                                                () => {
                                                                    handleAction(action, materialId);
                                                                }
                                                            )
                                                        }
                                                    }
                                                } else {
                                                    setIsPaused(materialId, true);
                                                    addArmBotToMonitor(armBot.modelUuid, () => {
                                                        const machine = getMachineById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                        if (machine) {
                                                            if (machine.isActive === false && machine.state === 'idle' && !machine.currentAction) {
                                                                setIsPaused(materialId, true);
                                                                handleAction(action, materialId);
                                                            } else {

                                                                // Handle current action using Event Manager
                                                                setIsPaused(materialId, true);

                                                                addMachineToMonitor(machine.modelUuid,
                                                                    () => {
                                                                        handleAction(action, materialId);
                                                                    }
                                                                )
                                                            }
                                                        }
                                                    }
                                                    );
                                                }
                                            }
                                        } else {
                                            if (armBot.isActive === false && armBot.state === 'idle') {

                                                // Handle current action from arm bot
                                                setIsPaused(materialId, true);
                                                handleAction(action, materialId);

                                            } else {

                                                // Handle current action using Event Manager
                                                setIsPaused(materialId, true);
                                                addArmBotToMonitor(armBot.modelUuid,
                                                    () => handleAction(action, materialId)
                                                );

                                            }
                                        }
                                    } else {
                                        if (armBot.isActive === false && armBot.state === 'idle') {

                                            // Handle current action from arm bot
                                            setIsPaused(materialId, true);
                                            handleAction(action, materialId);

                                        } else {

                                            // Handle current action using Event Manager
                                            setIsPaused(materialId, true);
                                            addArmBotToMonitor(armBot.modelUuid,
                                                () => handleAction(action, materialId)
                                            );

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'storageUnit') {
                // Transfer to Storage Unit

            } else if (toEvent?.type === 'human') {
                // Transfer to Human
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // Handle current action of the material
                        handleAction(action, materialId);

                        if (material.next) {
                            const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                            const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, null);

                            if (action) {

                                if (human) {
                                    if (action && action.triggers.length > 0 &&
                                        action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                                        const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                        if (model?.type === 'vehicle') {
                                            const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (human) {
                                                setIsPaused(materialId, true);
                                                setHumanScheduled(human.modelUuid, true);
                                                const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                if (vehicle) {
                                                    addVehicleToMonitor(vehicle.modelUuid, () => {
                                                        addHumanToMonitor(human.modelUuid, () => {
                                                            setIsPaused(materialId, true);

                                                            handleAction(action, materialId);
                                                        }, action.actionUuid)
                                                    })
                                                }
                                            }
                                        } else if (model?.type === 'transfer') {
                                            const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (human) {
                                                setIsPaused(materialId, true);
                                                setHumanScheduled(human.modelUuid, true);
                                                const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                if (conveyor) {
                                                    addConveyorToMonitor(conveyor.modelUuid, () => {
                                                        addHumanToMonitor(human.modelUuid, () => {
                                                            setIsPaused(materialId, true);
                                                            handleAction(action, materialId);
                                                        }, action.actionUuid)
                                                    }, [materialId])
                                                }
                                            }
                                        } else if (model?.type === 'machine') {
                                            const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (human) {
                                                setIsPaused(materialId, true);
                                                setHumanScheduled(human.modelUuid, true);
                                                const machine = getMachineById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                if (machine) {
                                                    addMachineToMonitor(machine.modelUuid, () => {
                                                        addHumanToMonitor(human.modelUuid, () => {
                                                            setIsPaused(materialId, true);

                                                            handleAction(action, materialId);
                                                        }, action.actionUuid);
                                                    })
                                                }
                                            }
                                        } else {
                                            setHumanScheduled(human.modelUuid, true);
                                            setIsPaused(materialId, true);
                                            addHumanToMonitor(human.modelUuid, () => {
                                                handleAction(action, materialId)
                                            }, action.actionUuid);
                                        }
                                    } else {
                                        setHumanScheduled(human.modelUuid, true);
                                        setIsPaused(materialId, true);
                                        addHumanToMonitor(human.modelUuid, () => {
                                            handleAction(action, materialId)
                                        }, action.actionUuid);
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'crane') {
                // Transfer to Crane
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // Handle current action of the material
                        handleAction(action, materialId);

                        if (material.next) {
                            const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                            const crane = getCraneById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, null);

                            if (action) {

                                if (crane) {
                                    if (action && action.triggers.length > 0 &&
                                        action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                                        const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                        if (model?.type === 'human') {
                                            const crane = getCraneById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (crane) {
                                                setIsPaused(materialId, true);
                                                setCraneScheduled(crane.modelUuid, true);
                                                const human = getHumanById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                if (human) {
                                                    addHumanToMonitor(human.modelUuid, () => {
                                                        addCurrentAction(crane.modelUuid, action.actionUuid, null, null);
                                                        addCraneToMonitor(crane.modelUuid, () => {
                                                            setIsPaused(materialId, true);

                                                            handleAction(action, materialId);
                                                        }, action.actionUuid)
                                                    }, action.triggers[0]?.triggeredAsset.triggeredAction.actionUuid)
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (fromEvent?.type === 'vehicle') {
            if (toEvent?.type === 'transfer') {
                // Vehicle to Transfer
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const conveyor = getConveyorById(toEvent.modelUuid);
                    const material = getMaterialById(materialId);

                    if (material && conveyor) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setIsPaused(materialId, false);

                        setIsVisible(materialId, true);

                        if (action &&
                            action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid
                        ) {

                            setNextLocation(material.materialId, {
                                modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                            });

                            addConveyorToMonitor(conveyor.modelUuid, () => {

                                setIsPaused(materialId, false);

                                handleAction(action, materialId);
                            })
                        }
                    }
                }
            } else if (toEvent?.type === 'vehicle') {
                // Vehicle to Vehicle

            } else if (toEvent?.type === 'machine') {
                // Vehicle to Machine

            } else if (toEvent?.type === 'roboticArm') {
                // Vehicle to Robotic Arm
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        if (action && armBot) {

                            addArmBotToMonitor(armBot.modelUuid, () => {
                                setIsVisible(materialId, false);

                                handleAction(action, materialId);
                            })
                        }
                    }
                }

            } else if (toEvent?.type === 'storageUnit') {
                // Vehicle to Storage Unit
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const storageUnit = getStorageUnitById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && storageUnit) {

                            if (storageUnit.currentLoad < storageUnit.storageCapacity) {

                                // Handle current action from vehicle
                                handleAction(action, materialId);

                            } else {

                                // Event Manager Needed

                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'human') {
                // Vehicle to Human

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        if (action && human) {
                            setHumanScheduled(human.modelUuid, true);
                            addHumanToMonitor(human.modelUuid, () => {
                                setIsVisible(materialId, false);

                                handleAction(action, materialId);
                            }, action.actionUuid)
                        }
                    }
                }
            } else if (toEvent?.type === 'crane') {
                // Vehicle to Crane
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // Handle current action of the material
                        handleAction(action, materialId);

                        if (material.next) {
                            const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                            const crane = getCraneById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                            setPreviousLocation(material.materialId, {
                                modelUuid: material.current.modelUuid,
                                pointUuid: material.current.pointUuid,
                                actionUuid: material.current.actionUuid,
                            })

                            setCurrentLocation(material.materialId, {
                                modelUuid: material.next.modelUuid,
                                pointUuid: material.next.pointUuid,
                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                            });

                            setNextLocation(material.materialId, null);

                            if (action) {

                                if (crane) {
                                    if (action && action.triggers.length > 0 &&
                                        action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                                        action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                                        const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                        if (model?.type === 'human') {
                                            const crane = getCraneById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                                            if (crane) {
                                                setIsPaused(materialId, true);
                                                setCraneScheduled(crane.modelUuid, true);
                                                const human = getHumanById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                                if (human) {
                                                    addHumanToMonitor(human.modelUuid, () => {
                                                        addCurrentAction(crane.modelUuid, action.actionUuid, null, null);
                                                        addCraneToMonitor(crane.modelUuid, () => {
                                                            setIsPaused(materialId, true);

                                                            handleAction(action, materialId);
                                                        }, action.actionUuid)
                                                    }, action.triggers[0]?.triggeredAsset.triggeredAction.actionUuid)
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (fromEvent?.type === 'machine') {
            if (toEvent?.type === 'transfer') {
                // Machine to Transfer

            } else if (toEvent?.type === 'vehicle') {
                // Machine to Vehicle

            } else if (toEvent?.type === 'machine') {
                // Machine to Machine

            } else if (toEvent?.type === 'roboticArm') {
                // Machine to Robotic Arm
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);

                    setIsPaused(materialId, true);

                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && armBot) {

                            if (armBot.isActive === false && armBot.state === 'idle') {

                                // Handle current action from arm bot
                                const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                if (model?.type === 'transfer') {
                                    const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                    if (conveyor) {
                                        const previousModel = getEventByModelUuid(selectedProduct.productUuid, material.previous?.modelUuid || '');
                                        if (previousModel) {
                                            if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
                                                setMachineActive(trigger.triggerUuid, false);
                                                handleAction(action, materialId)
                                            } else {
                                                addConveyorToMonitor(conveyor.modelUuid,
                                                    () => {
                                                        setMachineActive(trigger.triggerUuid, false);
                                                        handleAction(action, materialId);
                                                    }
                                                )
                                            }
                                        } else {
                                            setMachineActive(trigger.triggerUuid, false);
                                            handleAction(action, materialId);
                                        }
                                    }
                                } else {
                                    setMachineActive(trigger.triggerUuid, false);
                                    handleAction(action, materialId);
                                }

                            } else {

                                // Handle current action using Event Manager

                                addArmBotToMonitor(armBot.modelUuid,
                                    () => {
                                        const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                        if (model?.type === 'transfer') {
                                            const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                            if (conveyor) {
                                                const previousModel = getEventByModelUuid(selectedProduct.productUuid, material.previous?.modelUuid || '');
                                                if (previousModel) {
                                                    if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
                                                        setMachineActive(trigger.triggerUuid, false);
                                                        handleAction(action, materialId)
                                                    } else {
                                                        addConveyorToMonitor(conveyor.modelUuid,
                                                            () => {
                                                                setMachineActive(trigger.triggerUuid, false);
                                                                handleAction(action, materialId);
                                                            }
                                                        )
                                                    }
                                                } else {
                                                    setMachineActive(trigger.triggerUuid, false);
                                                    handleAction(action, materialId);
                                                }
                                            }
                                        } else {
                                            setMachineActive(trigger.triggerUuid, false);
                                            handleAction(action, materialId);
                                        }
                                    }
                                );
                            }
                        }
                    }
                }

            } else if (toEvent?.type === 'storageUnit') {
                // Machine to Storage Unit

            } else if (toEvent?.type === 'human') {
                // Machine to Human
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);

                    setIsPaused(materialId, true);

                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && human) {
                            addHumanToMonitor(human.modelUuid, () => {
                                const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                if (model?.type === 'transfer') {
                                    const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                    if (conveyor) {
                                        const previousModel = getEventByModelUuid(selectedProduct.productUuid, material.previous?.modelUuid || '');
                                        if (previousModel) {
                                            if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
                                                setHumanScheduled(human.modelUuid, true);
                                                setMachineActive(trigger.triggerUuid, false);
                                                handleAction(action, materialId)
                                            } else {
                                                setHumanScheduled(human.modelUuid, true);
                                                addConveyorToMonitor(conveyor.modelUuid, () => {
                                                    setMachineActive(trigger.triggerUuid, false);
                                                    handleAction(action, materialId)
                                                })
                                            }
                                        } else {
                                            setHumanScheduled(human.modelUuid, true);
                                            setMachineActive(trigger.triggerUuid, false);
                                            handleAction(action, materialId)
                                        }
                                    }
                                } else if (model?.type === 'vehicle') {
                                    const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
                                    if (vehicle) {
                                        if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {
                                            // Handle current action from vehicle
                                            setIsPaused(materialId, true);
                                            setHumanScheduled(human.modelUuid, true);
                                            setMachineActive(trigger.triggerUuid, false);
                                            handleAction(action, materialId);

                                        } else {
                                            // Handle current action using Event Manager
                                            setIsPaused(materialId, true);
                                            setHumanScheduled(human.modelUuid, true);

                                            addVehicleToMonitor(vehicle.modelUuid, () => {
                                                setMachineActive(trigger.triggerUuid, false);
                                                handleAction(action, materialId);
                                            })
                                        }
                                    }
                                } else {
                                    setHumanScheduled(human.modelUuid, true);
                                    setMachineActive(trigger.triggerUuid, false);
                                    handleAction(action, materialId)
                                }
                            }, action.actionUuid);
                        }
                    }
                }
            }
        } else if (fromEvent?.type === 'roboticArm') {
            if (toEvent?.type === 'transfer') {
                // Robotic Arm to Transfer
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);

                        if (action && action.triggers.length > 0 &&
                            action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                            const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                            if (model?.type === 'roboticArm') {

                                handleAction(action, material.materialId);

                            } else if (model?.type === 'vehicle') {
                                const nextAction = getActionByUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredAction.actionUuid);

                                if (action) {
                                    handleAction(action, material.materialId);

                                    const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    setNextLocation(material.materialId, null);

                                    if (nextAction) {

                                        if (vehicle) {

                                            if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                                setPreviousLocation(material.materialId, {
                                                    modelUuid: material.current.modelUuid,
                                                    pointUuid: material.current.pointUuid,
                                                    actionUuid: material.current.actionUuid,
                                                })

                                                setCurrentLocation(material.materialId, {
                                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                    actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                });

                                                setNextLocation(material.materialId, null);

                                                setIsVisible(materialId, false);
                                                setIsPaused(materialId, false);

                                                // Handle current action from vehicle
                                                handleAction(nextAction, materialId);

                                            } else {

                                                // Handle current action using Event Manager
                                                setIsPaused(materialId, true);

                                                addVehicleToMonitor(vehicle.modelUuid,
                                                    () => {
                                                        setPreviousLocation(material.materialId, {
                                                            modelUuid: material.current.modelUuid,
                                                            pointUuid: material.current.pointUuid,
                                                            actionUuid: material.current.actionUuid,
                                                        })

                                                        setCurrentLocation(material.materialId, {
                                                            modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                            pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                            actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                        });

                                                        setNextLocation(material.materialId, null);

                                                        setIsPaused(materialId, false);
                                                        setIsVisible(materialId, false);
                                                        handleAction(nextAction, materialId);
                                                    }
                                                )
                                            }
                                        }
                                    }
                                }

                            } else if (model?.type === 'transfer') {
                                const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);
                                if (conveyor) {
                                    setNextLocation(material.materialId, {
                                        modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                    })
                                    setIsPaused(material.materialId, false); // New
                                    handleAction(action, material.materialId);
                                }
                            } else {
                                setNextLocation(material.materialId, {
                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                })

                                handleAction(action, material.materialId);
                            }
                        } else if (action) {
                            setNextLocation(material.materialId, null)

                            handleAction(action, material.materialId);
                        }

                    }
                }

            } else if (toEvent?.type === 'vehicle') {
                // Robotic Arm to Vehicle
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        setIsPaused(material.materialId, false);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const vehicle = getVehicleById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setNextLocation(material.materialId, null);

                        if (action) {

                            if (vehicle) {

                                if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                    setIsVisible(materialId, false);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    // Handle current action from vehicle
                                    handleAction(action, materialId);

                                } else {

                                    // Event Manager Needed

                                }
                            }
                        }

                    }
                }

            } else if (toEvent?.type === 'machine') {
                // Robotic Arm to Machine

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // setIsPaused(material.materialId, false);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const machine = getMachineById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                        setNextLocation(material.materialId, null);

                        if (action) {

                            if (machine) {

                                if (machine.isActive === false && machine.state === 'idle') {

                                    setIsVisible(materialId, false);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    // Handle current action from machine
                                    handleAction(action, materialId);

                                } else {

                                    // Event Manager Needed

                                }
                            }
                        }
                    }
                }

            } else if (toEvent?.type === 'roboticArm') {
                // Robotic Arm to Robotic Arm

            } else if (toEvent?.type === 'storageUnit') {
                // Robotic Arm to Storage Unit
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const storageUnit = getStorageUnitById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && storageUnit) {

                            if (storageUnit.currentLoad < storageUnit.storageCapacity) {

                                // Handle current action from vehicle
                                handleAction(action, materialId);

                            } else {

                                // Event Manager Needed

                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'human') {
                // Robotic Arm to Human
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        setIsPaused(material.materialId, false);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const human = getHumanById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setNextLocation(material.materialId, null);

                        if (action && action.actionType === 'worker') {

                            if (human) {

                                if (human.isActive === false && human.state === 'idle' && !human.isScheduled && human.currentLoad < action.loadCapacity) {

                                    setIsVisible(materialId, false);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    // Handle current action from human
                                    setHumanScheduled(human.modelUuid, true);
                                    handleAction(action, materialId);

                                } else {

                                    // Event Manager Needed

                                }
                            }
                        }

                    }
                }

            }
        } else if (fromEvent?.type === 'storageUnit') {
            if (toEvent?.type === 'transfer') {
                // Storage Unit to Transfer

            } else if (toEvent?.type === 'vehicle') {
                // Storage Unit to Vehicle

            } else if (toEvent?.type === 'machine') {
                // Storage Unit to Machine

            } else if (toEvent?.type === 'roboticArm') {
                // Storage Unit to Robotic Arm

            } else if (toEvent?.type === 'storageUnit') {
                // Storage Unit to Storage Unit

            } else if (toEvent?.type === 'human') {
                // Storage Unit to Human

            }
        } else if (fromEvent?.type === 'human') {
            if (toEvent?.type === 'transfer') {
                // Human to Transfer

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material && action.actionType === 'worker') {

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);

                        if (action && action.triggers.length > 0 &&
                            action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                            const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                            if (model?.type === 'roboticArm') {

                                handleAction(action, material.materialId);

                            } else if (model?.type === 'vehicle') {
                                const nextAction = getActionByUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredAction.actionUuid);

                                if (action) {
                                    handleAction(action, material.materialId);

                                    const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    setNextLocation(material.materialId, null);

                                    if (nextAction) {

                                        if (vehicle) {

                                            if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                                setPreviousLocation(material.materialId, {
                                                    modelUuid: material.current.modelUuid,
                                                    pointUuid: material.current.pointUuid,
                                                    actionUuid: material.current.actionUuid,
                                                })

                                                setCurrentLocation(material.materialId, {
                                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                    actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                });

                                                setNextLocation(material.materialId, null);

                                                setIsVisible(materialId, false);
                                                setIsPaused(materialId, false);

                                                // Handle current action from vehicle
                                                handleAction(nextAction, materialId);

                                            } else {

                                                // Handle current action using Event Manager
                                                setIsPaused(materialId, true);

                                                addVehicleToMonitor(vehicle.modelUuid,
                                                    () => {
                                                        setPreviousLocation(material.materialId, {
                                                            modelUuid: material.current.modelUuid,
                                                            pointUuid: material.current.pointUuid,
                                                            actionUuid: material.current.actionUuid,
                                                        })

                                                        setCurrentLocation(material.materialId, {
                                                            modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                            pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                            actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                        });

                                                        setNextLocation(material.materialId, null);

                                                        setIsPaused(materialId, false);
                                                        setIsVisible(materialId, false);
                                                        handleAction(nextAction, materialId);
                                                    }
                                                )
                                            }
                                        }
                                    }
                                }

                            } else if (model?.type === 'transfer') {
                                const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);
                                if (conveyor) {
                                    setNextLocation(material.materialId, {
                                        modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                    })
                                    setIsPaused(material.materialId, false);
                                    setIsVisible(material.materialId, true);
                                    handleAction(action, material.materialId);
                                }
                            } else {
                                setNextLocation(material.materialId, {
                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                })

                                handleAction(action, material.materialId);
                            }
                        } else if (action) {
                            setNextLocation(material.materialId, null)

                            handleAction(action, material.materialId);
                        }

                    } else if (material && action.actionType === 'manufacturer') {

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setIsPaused(material.materialId, true);
                        setIsVisible(material.materialId, true);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);

                        if (action && action.triggers.length > 0 &&
                            action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                            const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                            if (model?.type === 'roboticArm') {
                                addArmBotToMonitor(model.modelUuid, () => {

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    });

                                    setNextLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                        pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                    })

                                    setIsPaused(material.materialId, false);
                                })
                            } else if (model?.type === 'vehicle') {
                                addVehicleToMonitor(model.modelUuid, () => {

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    });

                                    setNextLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                        pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                    })

                                    setIsPaused(material.materialId, false);
                                })
                            } else if (model?.type === 'transfer') {

                                setCurrentLocation(material.materialId, {
                                    modelUuid: material.current.modelUuid,
                                    pointUuid: material.current.pointUuid,
                                    actionUuid: material.current.actionUuid,
                                });

                                setNextLocation(material.materialId, {
                                    modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                    pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                })

                                setIsPaused(material.materialId, false);
                            } else if (model?.type === 'human') {
                                if (fromEvent.modelUuid === model.modelUuid) {
                                    setIsPaused(material.materialId, false);

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    });

                                    setTimeout(() => {
                                        setIsPaused(material.materialId, false);
                                        setNextLocation(material.materialId, {
                                            modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                            pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                        })
                                    }, 0)

                                } else {
                                    addHumanToMonitor(model.modelUuid, () => {

                                        setCurrentLocation(material.materialId, {
                                            modelUuid: material.current.modelUuid,
                                            pointUuid: material.current.pointUuid,
                                            actionUuid: material.current.actionUuid,
                                        });

                                        setNextLocation(material.materialId, {
                                            modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                            pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                        })

                                        setIsPaused(material.materialId, false);
                                    }, action.actionUuid)
                                }
                            } else {

                                setCurrentLocation(material.materialId, {
                                    modelUuid: material.current.modelUuid,
                                    pointUuid: material.current.pointUuid,
                                    actionUuid: material.current.actionUuid,
                                });

                                setNextLocation(material.materialId, {
                                    modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '',
                                    pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                })

                                setIsPaused(material.materialId, false);
                            }
                        }
                    } else if (material && action.actionType === 'operator') {

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);

                        if (action && action.triggers.length > 0 &&
                            action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid &&
                            action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid) {
                            const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                            if (model?.type === 'roboticArm') {

                                handleAction(action, material.materialId);

                            } else if (model?.type === 'vehicle') {
                                const nextAction = getActionByUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredAction.actionUuid);

                                if (action) {
                                    handleAction(action, material.materialId);

                                    const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    setNextLocation(material.materialId, null);

                                    if (nextAction) {

                                        if (vehicle) {

                                            if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                                setPreviousLocation(material.materialId, {
                                                    modelUuid: material.current.modelUuid,
                                                    pointUuid: material.current.pointUuid,
                                                    actionUuid: material.current.actionUuid,
                                                })

                                                setCurrentLocation(material.materialId, {
                                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                    actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                });

                                                setNextLocation(material.materialId, null);

                                                setIsVisible(materialId, false);
                                                setIsPaused(materialId, false);

                                                // Handle current action from vehicle
                                                handleAction(nextAction, materialId);

                                            } else {

                                                // Handle current action using Event Manager
                                                setIsPaused(materialId, true);

                                                addVehicleToMonitor(vehicle.modelUuid, () => {
                                                    setPreviousLocation(material.materialId, {
                                                        modelUuid: material.current.modelUuid,
                                                        pointUuid: material.current.pointUuid,
                                                        actionUuid: material.current.actionUuid,
                                                    })

                                                    setCurrentLocation(material.materialId, {
                                                        modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                        pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                        actionUuid: action.triggers[0]?.triggeredAsset?.triggeredAction?.actionUuid || '',
                                                    });

                                                    setNextLocation(material.materialId, null);

                                                    setIsPaused(materialId, false);
                                                    setIsVisible(materialId, false);
                                                    handleAction(nextAction, materialId);
                                                })
                                            }
                                        }
                                    }
                                }

                            } else if (model?.type === 'transfer') {
                                const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);
                                if (conveyor) {
                                    setNextLocation(material.materialId, {
                                        modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                        pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                    })
                                    setIsPaused(material.materialId, false);
                                    setIsVisible(material.materialId, true);
                                    handleAction(action, material.materialId);
                                }
                            } else {
                                setNextLocation(material.materialId, {
                                    modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid,
                                    pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid,
                                })

                                handleAction(action, material.materialId);
                            }
                        } else if (action) {
                            setNextLocation(material.materialId, null)

                            handleAction(action, material.materialId);
                        }

                    }
                }

            } else if (toEvent?.type === 'vehicle') {
                // Human to Vehicle

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        setIsPaused(material.materialId, true);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const vehicle = getVehicleById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        if (action) {

                            if (vehicle) {

                                if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {

                                    setIsVisible(materialId, false);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                                        pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    setNextLocation(material.materialId, null);

                                    // Handle current action from vehicle
                                    handleAction(action, materialId);

                                } else {

                                    addVehicleToMonitor(vehicle.modelUuid,
                                        () => {
                                            setIsVisible(materialId, false);

                                            setPreviousLocation(material.materialId, {
                                                modelUuid: material.current.modelUuid,
                                                pointUuid: material.current.pointUuid,
                                                actionUuid: material.current.actionUuid,
                                            })

                                            setCurrentLocation(material.materialId, {
                                                modelUuid: trigger?.triggeredAsset?.triggeredModel.modelUuid || '',
                                                pointUuid: trigger?.triggeredAsset?.triggeredPoint?.pointUuid || '',
                                                actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid || '',
                                            });

                                            setNextLocation(material.materialId, null);

                                            // Handle current action from vehicle
                                            console.log('action: ', action);
                                            handleAction(action, materialId);
                                        }
                                    )
                                }
                            }
                        }

                    }
                }

            } else if (toEvent?.type === 'machine') {
                // Human to Machine

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {

                        // setIsPaused(material.materialId, false);

                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const machine = getMachineById(trigger.triggeredAsset?.triggeredModel.modelUuid);
                        setNextLocation(material.materialId, null);

                        if (action) {

                            if (machine) {

                                if (machine.isActive === false && machine.state === 'idle') {

                                    setIsVisible(materialId, false);

                                    setPreviousLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: material.current.pointUuid,
                                        actionUuid: material.current.actionUuid,
                                    })

                                    setCurrentLocation(material.materialId, {
                                        modelUuid: material.current.modelUuid,
                                        pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                                        actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                                    });

                                    // Handle current action from machine
                                    handleAction(action, materialId);

                                } else {

                                    // Event Manager Needed

                                }
                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'roboticArm') {
                // Human to Robotic Arm

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const armBot = getArmBotById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && armBot) {

                            if (armBot.isActive === false && armBot.state === 'idle') {

                                // Handle current action from arm bot
                                handleAction(action, materialId);

                            } else {

                                // Event Manager Needed

                            }
                        }
                    }
                }

            } else if (toEvent?.type === 'storageUnit') {
                // Human to Storage Unit

                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
                        const storageUnit = getStorageUnitById(trigger.triggeredAsset?.triggeredModel.modelUuid);

                        setPreviousLocation(material.materialId, {
                            modelUuid: material.current.modelUuid,
                            pointUuid: material.current.pointUuid,
                            actionUuid: material.current.actionUuid,
                        })

                        setCurrentLocation(material.materialId, {
                            modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid,
                            pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid,
                            actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
                        });

                        setNextLocation(material.materialId, null);

                        setIsVisible(materialId, false);

                        if (action && storageUnit) {

                            if (storageUnit.currentLoad < storageUnit.storageCapacity) {

                                // Handle current action from vehicle
                                handleAction(action, materialId);

                            } else {

                                // Event Manager Needed

                            }
                        }
                    }
                }
            } else if (toEvent?.type === 'human') {
                // Human to Human

            }
        } else if (fromEvent?.type === 'crane') {
            if (toEvent?.type === 'transfer') {
                // Crane to Transfer

            } else if (toEvent?.type === 'vehicle') {
                // Crane to Vehicle

            } else if (toEvent?.type === 'machine') {
                // Crane to Machine

            } else if (toEvent?.type === 'roboticArm') {
                // Crane to Robotic Arm

            } else if (toEvent?.type === 'storageUnit') {
                // Crane to Storage Unit

            } else if (toEvent?.type === 'human') {
                // Crane to Human

            } else if (toEvent?.type === 'crane') {
                // Crane to Human

            }
        }
    }

    const handleFinalAction = (action: Action, materialId?: string) => {
        if (!action) return;

        const modelUuid = getModelUuidByActionUuid(selectedProduct.productUuid, action.actionUuid);
        if (!modelUuid) return;
        const finalModel = getEventByModelUuid(selectedProduct.productUuid, modelUuid);
        if (!finalModel) return;

        if (finalModel.type === 'transfer') {
            // Storage Unit to Transfer
            if (!materialId) return;
            const material = getMaterialById(materialId);
            if (material) {
                const currentTime = performance.now();

                setPreviousLocation(material.materialId, {
                    modelUuid: material.current.modelUuid,
                    pointUuid: material.current.pointUuid,
                    actionUuid: material.current.actionUuid,
                })

                setCurrentLocation(material.materialId, {
                    modelUuid: material.next?.modelUuid || '',
                    pointUuid: material.next?.pointUuid || '',
                    actionUuid: action.actionUuid,
                });

                setNextLocation(material.materialId, null);

                setEndTime(material.materialId, currentTime);

                handleAction(action, material.materialId);
            }

        } else if (finalModel.type === 'vehicle') {
            // Storage Unit to Vehicle

        } else if (finalModel.type === 'machine') {
            // Storage Unit to Machine

        } else if (finalModel.type === 'roboticArm') {
            // Storage Unit to Robotic Arm

        } else if (finalModel.type === 'storageUnit') {
            // Storage Unit to Storage Unit

        }

    }

    const triggerPointActions = useCallback((action: Action, materialId?: string) => {
        if (!action) return;

        if (action.triggers.length > 0) {

            action.triggers.forEach(trigger => {
                switch (trigger.triggerType) {
                    case 'onStart':
                        break;
                    case 'onComplete':
                        handleTrigger(trigger, action, materialId);
                        break;
                    case 'onStop':
                        break;
                    case 'onError':
                        break;
                    case 'delay':
                        break;
                    default:
                        console.warn(`Unknown trigger type: ${trigger.triggerType}`);
                }
            });
        } else {
            handleFinalAction(action, materialId);
        }
    }, []);

    return {
        triggerPointActions
    };
}