import { useEffect } from 'react'
import { useResetButtonStore } from '../../../../../store/usePlayButtonStore';
import { useSceneContext } from '../../../../scene/sceneContext';
import { useProductContext } from '../../../products/productContext';
// import { findConveyorSubsequence } from '../../../simulator/functions/getConveyorSequencesInProduct';

function ConveyorInstance({ conveyor }: { readonly conveyor: ConveyorStatus }) {
    const { materialStore, conveyorStore, productStore } = useSceneContext();
    const { getProductById } = productStore();
    const { selectedProductStore } = useProductContext();
    const { selectedProduct } = selectedProductStore();
    const { getMaterialsByCurrentModelUuid, materials } = materialStore();
    const { isReset } = useResetButtonStore();
    const { setConveyorPaused } = conveyorStore();

    useEffect(() => {
        const product = getProductById(selectedProduct.productUuid);
        if (!product) return;

        const conveyorMaterials = getMaterialsByCurrentModelUuid(conveyor.modelUuid);
        if (conveyorMaterials && conveyorMaterials?.length > 0) {

            const hasPausedMaterials = conveyorMaterials.some(material => material.isPaused);

            if (hasPausedMaterials) {
                setConveyorPaused(conveyor.modelUuid, true);
            } else {
                setConveyorPaused(conveyor.modelUuid, false);
            }
        } else {
            setConveyorPaused(conveyor.modelUuid, false);
        }

        // const conveyorSubsequence = findConveyorSubsequence(product, conveyor.modelUuid);

        // if (!conveyorSubsequence || !conveyorSubsequence.currentSubSequence) {
        //     setConveyorPaused(conveyor.modelUuid, false);
        //     return;
        // }

        // const { currentSubSequence } = conveyorSubsequence;

        // const allMaterials = currentSubSequence.flatMap(event =>
        //     getMaterialsByCurrentModelUuid(event.modelUuid)
        // );

        // const hasPausedMaterials = allMaterials.some(mat => mat?.isPaused);

        // currentSubSequence.forEach(event => {
        //     if (event.type === 'transfer') {
        //         setConveyorPaused(event.modelUuid, hasPausedMaterials);
        //     }
        // });

    }, [materials, conveyor.modelUuid, getMaterialsByCurrentModelUuid, setConveyorPaused, isReset, selectedProduct.productUuid, getProductById]);

    useEffect(() => {
        // console.log('conveyor: ', conveyor);
    }, [conveyor])

    return (
        <>
        </>
    )
};

export default ConveyorInstance