import {
    usePlayButtonStore,
    useResetButtonStore,
} from "../../../store/usePlayButtonStore";
import { useConveyorActions } from "./conveyor/useConveyorActions";
import { useMachineActions } from "./machine/useMachineActions";
import { useRoboticArmActions } from "./roboticArm/useRoboticArmActions";
import { useStorageActions } from "./storageUnit/useStorageUnitActions";
import { useVehicleActions } from "./vehicle/useVehicleActions";
import { useHumanActions } from "./human/useHumanActions";
import { useCraneActions } from "./crane/useCraneActions";
import { useCallback, useEffect } from "react";

export function useActionHandler() {
    const { isReset } = useResetButtonStore();
    const { isPlaying } = usePlayButtonStore();
    const { handleConveyorAction, cleanup: cleanupConveyor } = useConveyorActions();
    const { handleVehicleAction, cleanup: cleanupVehicle } = useVehicleActions();
    const { handleRoboticArmAction, cleanup: cleanupRoboticArm } = useRoboticArmActions();
    const { handleMachineAction, cleanup: cleanupMachine } = useMachineActions();
    const { handleStorageAction, cleanup: cleanupStorage } = useStorageActions();
    const { handleHumanAction, cleanup: cleanupHuman } = useHumanActions();
    const { handleCraneAction, cleanup: cleanupCrane } = useCraneActions();

    const handleAction = useCallback((action: Action, materialId?: string) => {
        if (!action) return;
        try {
            switch (action.actionType) {
                case 'default': case 'spawn': case 'swap': case 'delay': case 'despawn':
                    handleConveyorAction(action as ConveyorAction, materialId as string);
                    break;
                case 'travel':
                    handleVehicleAction(action as VehicleAction, materialId as string);
                    break;
                case 'pickAndPlace':
                    handleRoboticArmAction(action as RoboticArmAction, materialId as string);
                    break;
                case 'process':
                    handleMachineAction(action as MachineAction, materialId as string);
                    break;
                case 'store': case 'retrieve':
                    handleStorageAction(action as StorageAction, materialId as string);
                    break;
                case 'worker': case 'manufacturer':
                    handleHumanAction(action as HumanAction, materialId as string);
                    break;
                case 'pickAndDrop':
                    handleCraneAction(action as CraneAction, materialId as string);
                    break;
                default:
                    console.warn(`Unknown action type: ${(action as Action).actionType}`);
            }
        } catch (error) {
            echo.error("Failed to handle action");
            console.error("Error handling action:", error);
        }
    }, [handleConveyorAction, handleVehicleAction, handleRoboticArmAction, handleMachineAction, handleStorageAction, handleHumanAction, handleCraneAction]);

    const cleanup = useCallback(() => {
        cleanupConveyor();
        cleanupVehicle();
        cleanupRoboticArm();
        cleanupMachine();
        cleanupStorage();
        cleanupHuman();
        cleanupCrane();
    }, [cleanupConveyor, cleanupVehicle, cleanupRoboticArm, cleanupMachine, cleanupStorage, cleanupHuman, cleanupCrane]);

    useEffect(() => {
        return () => {
            cleanup();
        };
    }, [cleanup, isReset, isPlaying]);

    return {
        handleAction,
        cleanup,
    };
}
