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

export function useTravelHandler() {
    const { materialStore, vehicleStore, productStore } = useSceneContext();
    const { selectedProductStore } = useProductContext();
    const { getMaterialById } = materialStore();
    const { getModelUuidByActionUuid } = productStore();
    const { selectedProduct } = selectedProductStore();
    const { incrementVehicleLoad, addCurrentMaterial } = vehicleStore();

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

    const handleTravel = useCallback((action: VehicleAction, materialId?: string) => {
        if (!action || action.actionType !== 'travel' || !materialId) return;

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

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

        if (!modelUuid) return;

        incrementVehicleLoad(modelUuid, 1);
        addCurrentMaterial(modelUuid, material.materialType, material.materialId);

        travelLogStatus(material.materialName, `is triggering travel from ${modelUuid}`);

    }, [addCurrentMaterial, getMaterialById, getModelUuidByActionUuid, incrementVehicleLoad, selectedProduct.productUuid]);

    return {
        handleTravel,
    };
}