import * as THREE from 'three';

import addLineToScene from './addLineToScene';
import addPointToScene from '../points/addPointToScene';

import * as Types from "../../../../types/world/worldTypes";
import arrayLineToObject from '../lines/lineConvertions/arrayLineToObject';
import { Socket } from 'socket.io-client';
// import { deleteLineApi } from '../../../../services/factoryBuilder/lines/deleteLineApi';
// import { setLine } from '../../../../services/factoryBuilder/lines/setLineApi';

function splitLine(
    visibleIntersect: Types.IntersectionEvent,
    intersectionPoint: Types.Vector3,
    currentLayerPoint: Types.RefMeshArray,
    floorPlanGroupPoint: Types.RefGroup,
    dragPointControls: Types.RefDragControl,
    isSnappedUUID: Types.RefString,
    lines: Types.RefLines,
    setDeletedLines: any,
    floorPlanGroupLine: { current: THREE.Group },
    socket: Socket<any>,
    pointColor: Types.String,
    lineColor: Types.String,
    lineType: Types.String,
): [Types.Line, Types.Line] {

    //////////  Removing the clicked line and splitting it with the clicked position adding a new point and two new lines  //////////


    ((visibleIntersect.object as any).material).dispose();
    ((visibleIntersect.object as any).geometry).dispose();
    floorPlanGroupLine.current.remove(visibleIntersect.object);
    setDeletedLines([visibleIntersect.object.userData.linePoints]);

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

    //REST

    // deleteLineApi(
    //     organization,
    //     [
    //         { "uuid": visibleIntersect.object.userData.linePoints[0][1] },
    //         { "uuid": visibleIntersect.object.userData.linePoints[1][1] }
    //     ]
    // )

    //SOCKET


    const data = {
        organization: organization,
        line: [
            { "uuid": visibleIntersect.object.userData.linePoints[0][1] },
            { "uuid": visibleIntersect.object.userData.linePoints[1][1] }
        ],
        socketId: socket.id
    }

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

    const point = addPointToScene(intersectionPoint, pointColor, currentLayerPoint, floorPlanGroupPoint, dragPointControls, isSnappedUUID, lineType);

    const oldLinePoints = visibleIntersect.object.userData.linePoints;
    lines.current = lines.current.filter(item => item !== oldLinePoints);

    const clickedPoint: Types.Point = [
        new THREE.Vector3(intersectionPoint.x, 0.01, intersectionPoint.z),
        point.uuid,
        oldLinePoints[0][2],
        lineType
    ];

    const start = oldLinePoints[0];
    const end = oldLinePoints[1];

    const newLine1: Types.Line = [start, clickedPoint];
    const newLine2: Types.Line = [clickedPoint, end];

    const line1 = arrayLineToObject(newLine1);
    const line2 = arrayLineToObject(newLine2);

    //REST

    // setLine(organization, line1.layer!, line1.line!, line1.type!);

    //SOCKET

    const input1 = {
        organization: organization,
        layer: line1.layer,
        line: line1.line,
        type: line1.type,
        socketId: socket.id
    }

    socket.emit('v1:Line:create', input1);

    //REST

    // setLine(organization, line2.layer!, line2.line!, line2.type!);

    //SOCKET

    const input2 = {
        organization: organization,
        layer: line2.layer,
        line: line2.line,
        type: line2.type,
        socketId: socket.id
    }

    socket.emit('v1:Line:create', input2);

    lines.current.push(newLine1, newLine2);

    addLineToScene(newLine1[0][0], newLine1[1][0], lineColor, newLine1, floorPlanGroupLine);
    addLineToScene(newLine2[0][0], newLine2[1][0], lineColor, newLine2, floorPlanGroupLine);

    return [newLine1, newLine2];
}

export default splitLine;
