import * as Types from "../../../../types/world/worldTypes";

import { toast } from 'react-toastify';

import RemoveConnectedLines from "../lines/removeConnectedLines";
// import { deletePointApi } from "../../../../services/factoryBuilder/lines/deletePointApi";
import { Socket } from "socket.io-client";

function deletePoint(
    hoveredDeletablePoint: Types.RefMesh,
    onlyFloorlines: Types.RefOnlyFloorLines,
    floorPlanGroupPoint: Types.RefGroup,
    floorPlanGroupLine: Types.RefGroup,
    lines: Types.RefLines,
    setDeletedLines: any,
    socket: Socket<any>
): void {
    //////////  Deleting a Point and the lines that are connected to it  //////////

    if (!hoveredDeletablePoint.current) {
        return;
    }

    (<any>hoveredDeletablePoint.current.material).dispose();
    (<any>hoveredDeletablePoint.current.geometry).dispose();
    floorPlanGroupPoint.current.remove(hoveredDeletablePoint.current);
    const DeletedPointUUID = hoveredDeletablePoint.current.uuid;

    const email = localStorage.getItem('email')
    const organization = (email!.split("@")[1]).split(".")[0];

    //REST

    // deletePointApi(organization, DeletedPointUUID);

    //SOCKET

    const data = {
        organization: organization,
        uuid: DeletedPointUUID,
        socketId: socket.id
    }

    socket.emit('v1:Line:delete:point', data);

    //////////  Update onlyFloorlines.current to remove references to the deleted point  //////////

    onlyFloorlines.current = onlyFloorlines.current.map(floorline =>
        floorline.filter(line => line[0][1] !== DeletedPointUUID && line[1][1] !== DeletedPointUUID)
    ).filter(floorline => floorline.length > 0);

    RemoveConnectedLines(DeletedPointUUID, floorPlanGroupLine, floorPlanGroupPoint, setDeletedLines, lines);

    toast.success("Point Removed!");
}

export default deletePoint;
