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

export function useAssemblyHandler() {
    const { materialStore, humanStore, productStore } = useSceneContext();
    const { getMaterialById } = materialStore();
    const { getModelUuidByActionUuid } = productStore();
    const { selectedProductStore } = useProductContext();
    const { selectedProduct } = selectedProductStore();
    const { incrementHumanLoad, addCurrentMaterial, addCurrentAction } = humanStore();

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

    const handleAssembly = useCallback((action: HumanAction, materialId?: string) => {
        if (!action || action.actionType !== 'assembly' || !materialId) return;

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

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

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

        assemblyLogStatus(material.materialName, `performing assembly action`);

    }, [getMaterialById]);

    return {
        handleAssembly,
    };
}