import numpy as np
import PIL.Image
import PIL.ImageDraw
from numpy.typing import NDArray

from .. import _utils
from ._ink import Ink
from ._ink import get_pil_ink
from ._ink import require_fill_or_outline


def ellipse(
    image: NDArray[np.uint8],
    yx1: tuple[float, float],
    yx2: tuple[float, float],
    fill: Ink | None = None,
    outline: Ink | None = None,
    width: int = 0,
) -> NDArray[np.uint8]:
    """Draw ellipse on numpy array with Pillow.

    Args:
        image: Input image.
        yx1: Minimum vertex (y_min, x_min) of the bounding box.
        yx2: Maximum vertex (y_max, x_max) of the bounding box.
        fill: RGB color to fill the mark. None for no fill.
        outline: RGB color to draw the outline.
        width: Line width.

    Returns:
        Output image.
    """
    dst = _utils.numpy_to_pillow(image)
    ellipse_(image=dst, yx1=yx1, yx2=yx2, fill=fill, outline=outline, width=width)
    return _utils.pillow_to_numpy(dst)


def ellipse_(
    image: PIL.Image.Image,
    yx1: tuple[float, float],
    yx2: tuple[float, float],
    fill: Ink | None = None,
    outline: Ink | None = None,
    width: int = 0,
) -> None:
    """Draw ellipse on PIL image in-place.

    Args:
        image: PIL image to draw on (modified in-place).
        yx1: Minimum vertex (y_min, x_min) of the bounding box.
        yx2: Maximum vertex (y_max, x_max) of the bounding box.
        fill: RGB color to fill the mark. None for no fill.
        outline: RGB color to draw the outline.
        width: Line width.
    """
    require_fill_or_outline(fill, outline)

    draw = PIL.ImageDraw.Draw(image)

    y1, x1 = yx1
    y2, x2 = yx2

    draw.ellipse(
        [x1, y1, x2, y2],
        fill=get_pil_ink(fill),
        outline=get_pil_ink(outline),
        width=width,
    )
