import { useFloorItems, useSimulationPaths } from '../../../store/store';
import * as THREE from 'three';
import * as Types from '../../../types/world/worldTypes';
import { useEffect } from 'react';

function Behaviour() {
    const { setSimulationPaths } = useSimulationPaths();
    const { floorItems } = useFloorItems();

    useEffect(() => {
        const newPaths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[] = [];

        floorItems.forEach((item: Types.FloorItemType) => {
            // console.log('item: ', item);
            if (item.eventData && item.eventData.type === 'Conveyor') {
                const newPath: Types.ConveyorEventsSchema = {
                    modeluuid: item.modeluuid,
                    modelName: item.modelname,
                    type: 'Conveyor',
                    points: item.eventData.points,
                    position: [...item.position],
                    rotation: [item.rotation.x, item.rotation.y, item.rotation.z],
                    speed: item.eventData.speed,
                };

                newPaths.push(newPath);
            } else if (item.modelfileID === "67e3da19c2e8f37134526e6a") {
                const pointUUID = THREE.MathUtils.generateUUID();
                const pointPosition = new THREE.Vector3(0, 1.3, 0);

                const newVehiclePath: Types.VehicleEventsSchema = {
                    modeluuid: item.modeluuid,
                    modelName: item.modelname,
                    type: 'Vehicle',
                    point: {
                        uuid: pointUUID,
                        position: [pointPosition.x, pointPosition.y, pointPosition.z],
                        actions: { uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Start', start: {}, hitCount: 1, end: {}, buffer: 0 },
                        connections: { source: { pathUUID: item.modeluuid, pointUUID: pointUUID }, targets: [] },
                        speed: 2,
                    },
                    position: [...item.position],
                };

                newPaths.push(newVehiclePath);
            }
        });

        setSimulationPaths(newPaths);
    }, [floorItems]);
    return null;
}

export default Behaviour;