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) => {
            if (item.modelfileID === "672a090f80d91ac979f4d0bd") {
                const point1Position = new THREE.Vector3(0, 0.85, 2.2);
                const middlePointPosition = new THREE.Vector3(0, 0.85, 0);
                const point2Position = new THREE.Vector3(0, 0.85, -2.2);

                const point1UUID = THREE.MathUtils.generateUUID();
                const middlePointUUID = THREE.MathUtils.generateUUID();
                const point2UUID = THREE.MathUtils.generateUUID();

                const newPath: Types.ConveyorEventsSchema = {
                    modeluuid: item.modeluuid,
                    modelName: item.modelname,
                    type: 'Conveyor',
                    points: [
                        {
                            uuid: point1UUID,
                            position: [point1Position.x, point1Position.y, point1Position.z],
                            rotation: [0, 0, 0],
                            actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: false }],
                            triggers: [],
                            connections: { source: { pathUUID: item.modeluuid, pointUUID: point1UUID }, targets: [] },
                        },
                        {
                            uuid: middlePointUUID,
                            position: [middlePointPosition.x, middlePointPosition.y, middlePointPosition.z],
                            rotation: [0, 0, 0],
                            actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: false }],
                            triggers: [],
                            connections: { source: { pathUUID: item.modeluuid, pointUUID: middlePointUUID }, targets: [] },
                        },
                        {
                            uuid: point2UUID,
                            position: [point2Position.x, point2Position.y, point2Position.z],
                            rotation: [0, 0, 0],
                            actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: false }],
                            triggers: [],
                            connections: { source: { pathUUID: item.modeluuid, pointUUID: point2UUID }, targets: [] },
                        },
                    ],
                    assetPosition: [...item.position],
                    assetRotation: [item.rotation.x, item.rotation.y, item.rotation.z],
                    speed: 'Inherit',
                };

                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,
                    },
                    assetPosition: [...item.position],
                };

                newPaths.push(newVehiclePath);
            }
        });

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

    return null;
}

export default Behaviour;