import React, { useRef, useMemo } from "react";
import { InfoIcon } from "../../../icons/ExportCommonIcons";
import InputWithDropDown from "../../../ui/inputs/InputWithDropDown";
import { useEditingPoint, useEyeDropMode, usePreviewPosition, useSelectedActionSphere, useSimulationStates, useSocketStore } from "../../../../store/store";
import * as Types from '../../../../types/world/worldTypes';
import PositionInput from "../customInput/PositionInputs";
import { setEventApi } from "../../../../services/factoryBuilder/assest/floorAsset/setEventsApt";

const ArmBotMechanics: React.FC = () => {
    const { selectedActionSphere } = useSelectedActionSphere();
    const { simulationStates, setSimulationStates } = useSimulationStates();
    const { socket } = useSocketStore();

    const propertiesContainerRef = useRef<HTMLDivElement>(null);

    const { selectedPoint, connectedPointUuids } = useMemo(() => {
        if (!selectedActionSphere?.points?.uuid) return { selectedPoint: null, connectedPointUuids: [] };

        const vehiclePaths = simulationStates.filter(
            (path): path is Types.ArmBotEventsSchema => path.type === "ArmBot"
        );

        const points = vehiclePaths.find(
            (path) => path.points.uuid === selectedActionSphere.points.uuid
        )?.points;

        if (!points) return { selectedPoint: null, connectedPointUuids: [] };

        const connectedUuids: string[] = [];
        if (points.connections?.targets) {
            points.connections.targets.forEach(target => {
                connectedUuids.push(target.pointUUID);
            });
        }

        return {
            selectedPoint: points,
            connectedPointUuids: connectedUuids
        };
    }, [selectedActionSphere, simulationStates]);

    const updateBackend = async (updatedPath: Types.ArmBotEventsSchema | undefined) => {
        if (!updatedPath) return;
        const email = localStorage.getItem("email");
        const organization = email ? email.split("@")[1].split(".")[0] : "";

        // await setEventApi(
        //     organization,
        //     updatedPath.modeluuid,
        //     { type: "ArmBot", points: updatedPath.points }
        // );

        const data = {
            organization: organization,
            modeluuid: updatedPath.modeluuid,
            eventData: { type: "ArmBot", points: updatedPath.points }
        }

        socket.emit('v2:model-asset:updateEventData', data);

    }


    return (
        <div className="machine-mechanics-container" key={selectedPoint?.uuid}>
            <div className="machine-mechanics-header">
                {selectedActionSphere?.path?.modelName || "ArmBot point not found"}
            </div>

            <div className="machine-mechanics-content-container">
                <div className="selected-properties-container" ref={propertiesContainerRef}>
                    <div className="properties-header">ArmBot Properties</div>

                    {selectedPoint && (
                        <>

                        </>
                    )}
                </div>

                <div className="footer">
                    <InfoIcon />
                    Configure armbot properties.
                </div>
            </div>
        </div>
    );
};

export default React.memo(ArmBotMechanics);