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

interface Path {
    modeluuid: string;
    modelName: string;
    points: {
        uuid: string;
        position: [number, number, number];
        rotation: [number, number, number];
        actions: { uuid: string; name: string; type: string; material: string; delay: number | string; spawnInterval: number | string; isUsed: boolean }[] | [];
        triggers: { uuid: string; name: string; type: string; isUsed: boolean }[] | [];
        connections: { source: { pathUUID: string; pointUUID: string }; targets: { pathUUID: string; pointUUID: string }[] };
    }[];
    pathPosition: [number, number, number];
    pathRotation: [number, number, number];
    speed: number;
}

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

    useEffect(() => {
        const newPaths: Path[] = [];

        floorItems.forEach((item: Types.FloorItemType) => {
            if (item.modelfileID === "6633215057b31fe671145959") {
                const point1Position = new THREE.Vector3(0, 1.25, 3.3);
                const middlePointPosition = new THREE.Vector3(0, 1.25, 0);
                const point2Position = new THREE.Vector3(0, 1.25, -3.3);

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

                const newPath: Path = {
                    modeluuid: item.modeluuid,
                    modelName: item.modelname,
                    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: [] },
                        },
                    ],
                    pathPosition: [...item.position],
                    pathRotation: [item.rotation.x, item.rotation.y, item.rotation.z],
                    speed: 1,
                };

                newPaths.push(newPath);
            }
        });

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

    return null;
}

export default Behaviour;