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

function Layer2DVisibility(
    activeLayer: Types.Number,
    floorPlanGroup: Types.RefGroup,
    floorPlanGroupLine: Types.RefGroup,
    floorPlanGroupPoint: Types.RefGroup,
    currentLayerPoint: Types.RefMeshArray,
    dragPointControls: Types.RefDragControl
): void {

    if (floorPlanGroup.current && dragPointControls.current) {
        currentLayerPoint.current = [];
        floorPlanGroupLine.current.children.forEach((line) => {
            const linePoints = line.userData.linePoints;

            const point1 = floorPlanGroupPoint.current.getObjectByProperty('uuid', linePoints[0][1]) as Types.Mesh;
            const point2 = floorPlanGroupPoint.current.getObjectByProperty('uuid', linePoints[1][1]) as Types.Mesh;

            if (linePoints[0][2] !== activeLayer && linePoints[1][2] !== activeLayer) {
                point1.visible = false;
                point2.visible = false;
                line.visible = false;
            } else {
                point1.visible = true;
                point2.visible = true;
                line.visible = true;
                currentLayerPoint.current.push(point1, point2);
            }
        });
        dragPointControls.current!.objects = currentLayerPoint.current;
    }
}

export default Layer2DVisibility;
