"""Gamma correction postprocessing filter."""

from typing import Any, Dict, 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 GammaFilter(PostprocessingFilter):
    """Applies gamma correction."""

    filter_id = "gamma"
    filter_name = "Gamma"

    def __init__(self, options: Dict[str, Any]):
        super().__init__(options)
        value = self.options["value"]
        lut = np.arange(256, dtype=np.float32) / 255.0
        np.power(lut, value, out=lut)
        self._lut = np.clip(lut * 255.0, 0, 255).astype(np.uint8)

    @classmethod
    def get_options_schema(cls) -> List[FilterOptionDef]:
        return [
            FilterOptionDef(
                key="value",
                label="Gamma",
                option_type="float",
                default=2.2,
                min_value=0.1,
                max_value=5.0,
                step=0.1,
            ),
        ]

    def process_image(self, image: np.ndarray, image_pool: ImagePool) -> Optional[np.ndarray]:
        if self.options["value"] == 1.0:
            return None
        image[:] = self._lut[image]
        return None
