import numpy as np
from compas_cgal._cgal import slicer
from compas.plugins import plugin

from compas_cgal.types import VerticesFaces
from compas_cgal.types import ListOfPlanes
from compas_cgal.types import ListOfPolylinesNumpy


__all__ = [
    'slice_mesh'
]


@plugin(category='trimesh', pluggable_name='trimesh_slice')
def slice_mesh(mesh: VerticesFaces,
               planes: ListOfPlanes) -> ListOfPolylinesNumpy:
    """Slice a mesh by a list of planes.

    Parameters
    ----------
    mesh : tuple of vertices and faces
        The mesh to slice.
    planes : list of (point, normal) tuples or compas.geometry.Plane
        The slicing planes.

    Returns
    -------
    list of arrays
        The points defining the slice polylines.

    """
    vertices, faces = mesh
    points, normals = zip(*planes)
    V = np.asarray(vertices, dtype=np.float64)
    F = np.asarray(faces, dtype=np.int32)
    P = np.array(points, dtype=np.float64)
    N = np.array(normals, dtype=np.float64)

    pointsets = slicer.slice_mesh(V, F, P, N)
    return pointsets
