import { useCallback } from "react";
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
import { useArmBotStore } from "../../../../../store/simulation/useArmBotStore";
import { useProductStore } from "../../../../../store/simulation/useProductStore";
import { useSelectedProduct } from "../../../../../store/simulation/useSimulationStore";

export function usePickAndPlaceHandler() {
    const { getMaterialById } = useMaterialStore();
    const { addCurrentAction } = useArmBotStore();
    const { getModelUuidByActionUuid } = useProductStore();
    const { selectedProduct } = useSelectedProduct();

    const pickAndPlaceLogStatus = (materialUuid: string, status: string) => {
        echo.warn(`${materialUuid}, ${status}`);
    }

    const handlePickAndPlace = useCallback((action: RoboticArmAction, materialId?: string) => {
        if (!action || action.actionType !== 'pickAndPlace' || !materialId) return;

        const material = getMaterialById(materialId);
        if (!material) return;

        const modelUuid = getModelUuidByActionUuid(selectedProduct.productId, action.actionUuid);

        if (!modelUuid) return;

        addCurrentAction(
            modelUuid,
            action.actionUuid,
            material.materialType,
            material.materialId
        );

        pickAndPlaceLogStatus(material.materialName, `is going to be picked by armBot ${modelUuid}`);

    }, [getMaterialById, getModelUuidByActionUuid, selectedProduct.productId, addCurrentAction]);

    return {
        handlePickAndPlace,
    };
}