"""Flip postprocessing filter."""

from typing import List, Optional

import numpy as np

from wled_controller.core.filters.base import FilterOptionDef, PostprocessingFilter
from wled_controller.core.filters.image_pool import ImagePool
from wled_controller.core.filters.registry import FilterRegistry


@FilterRegistry.register
class FlipFilter(PostprocessingFilter):
    """Flips the image horizontally and/or vertically."""

    filter_id = "flip"
    filter_name = "Flip"
    supports_strip = False

    @classmethod
    def get_options_schema(cls) -> List[FilterOptionDef]:
        return [
            FilterOptionDef(
                key="horizontal",
                label="Horizontal",
                option_type="bool",
                default=False,
                min_value=None,
                max_value=None,
                step=None,
            ),
            FilterOptionDef(
                key="vertical",
                label="Vertical",
                option_type="bool",
                default=False,
                min_value=None,
                max_value=None,
                step=None,
            ),
        ]

    def process_image(self, image: np.ndarray, image_pool: ImagePool) -> Optional[np.ndarray]:
        h = self.options.get("horizontal", False)
        v = self.options.get("vertical", False)
        if not h and not v:
            return None
        height, width, c = image.shape
        result = image_pool.acquire(height, width, c)
        if h and v:
            np.copyto(result, image[::-1, ::-1])
        elif h:
            np.copyto(result, image[:, ::-1])
        else:
            np.copyto(result, image[::-1])
        return result
