import { useCallback } from "react";
import { useSceneContext } from "../../../../scene/sceneContext";
import { useProductContext } from "../../../products/productContext";

export function usePickAndDropHandler() {
    const { materialStore, craneStore, productStore } = useSceneContext();
    const { getMaterialById } = materialStore();
    const { getModelUuidByActionUuid } = productStore();
    const { selectedProductStore } = useProductContext();
    const { selectedProduct } = selectedProductStore();
    const { incrementCraneLoad, addCurrentMaterial, addCurrentAction } = craneStore();

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

    const handlePickAndDrop = useCallback((action: CraneAction, materialId?: string) => {
        if (!action || action.actionType !== 'pickAndDrop' || !materialId) return;

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

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

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

        pickAndDropLogStatus(material.materialName, `performing pickAndDrop action`);

    }, [getMaterialById]);

    return {
        handlePickAndDrop,
    };
}