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

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

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

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

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

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

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

        workerLogStatus(material.materialName, `performing worker action`);

    }, [getMaterialById]);

    return {
        handleWorker,
    };
}