import { useCallback } from 'react';
import { useActionHandler } from '../../actions/useActionHandler';
import { useProductStore } from '../../../../store/simulation/useProductStore';
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';

export function useTriggerHandler() {
    const { materialStore, armBotStore, machineStore, conveyorStore, vehicleStore, storageUnitStore } = useSceneContext();
    const { selectedProductStore } = useProductContext();
    const { handleAction } = useActionHandler();
    const { selectedProduct } = selectedProductStore();
    const { getEventByTriggerUuid, getEventByModelUuid, getActionByUuid, getModelUuidByActionUuid } = useProductStore();
    const { getArmBotById } = armBotStore();
    const { getConveyorById } = conveyorStore();
    const { addArmBotToMonitor } = useArmBotEventManager();
    const { addConveyorToMonitor } = useConveyorEventManager();
    const { addVehicleToMonitor } = useVehicleEventManager();
    const { addMachineToMonitor } = useMachineEventManager();
    const { getVehicleById } = vehicleStore();
    const { getMachineById } = 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.productId, trigger.triggerUuid);

        const toEvent = getEventByModelUuid(selectedProduct.productId, 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.productId, 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, () => {
                                            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.productId, 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.productId, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

                                        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);
                                                    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 (fromEvent?.type === 'vehicle') {
            if (toEvent?.type === 'transfer') {
                // Vehicle to Transfer
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productId, 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,
                        });

                        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,
                            });

                            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.productId, 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') {
                // Vehicle to Storage Unit
                if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
                    const material = getMaterialById(materialId);
                    if (material) {
                        const action = getActionByUuid(selectedProduct.productId, 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.point.action.storageCapacity) {

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

                            } else {

                                // Event Manager Needed

                            }
                        }
                    }
                }
            }
        } 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.productId, 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.productId, 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.productId, material.previous?.modelUuid || '');
                                        if (previousModel) {
                                            if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
                                                handleAction(action, materialId)
                                            } else {
                                                addConveyorToMonitor(conveyor.modelUuid,
                                                    () => {
                                                        handleAction(action, materialId)
                                                    }
                                                )
                                            }
                                        } else {
                                            handleAction(action, materialId)
                                        }
                                        // handleAction(action, materialId)
                                    }
                                } else {
                                    handleAction(action, materialId)
                                }

                            } else {

                                // Handle current action using Event Manager

                                addArmBotToMonitor(armBot.modelUuid,
                                    () => {
                                        const model = getEventByModelUuid(selectedProduct.productId, 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.productId, material.previous?.modelUuid || '');
                                                if (previousModel) {
                                                    if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
                                                        handleAction(action, materialId)
                                                    } else {
                                                        addConveyorToMonitor(conveyor.modelUuid,
                                                            () => {
                                                                handleAction(action, materialId)
                                                            }
                                                        )
                                                    }
                                                } else {
                                                    handleAction(action, materialId)
                                                }
                                                // handleAction(action, materialId)
                                            }
                                        } else {
                                            handleAction(action, materialId)
                                        }
                                    }
                                );
                            }
                        }
                    }
                }

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

            }
        } 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.productId, 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.productId, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid);

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

                                handleAction(action, material.materialId);

                            } else if (model?.type === 'vehicle') {
                                const nextAction = getActionByUuid(selectedProduct.productId, 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,
                                    })
                                    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.productId, 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.productId, 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.productId, 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.point.action.storageCapacity) {

                                // Handle current action from vehicle
                                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

            }
        }
    }

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

        const modelUuid = getModelUuidByActionUuid(selectedProduct.productId, action.actionUuid);
        if (!modelUuid) return;
        const finalModel = getEventByModelUuid(selectedProduct.productId, 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
    };
}