"""Support for Lagute LW-12 WiFi LED Controller."""

import logging
from typing import Any, override

import lw12
import voluptuous as vol

from homeassistant.components.light import (
    ATTR_BRIGHTNESS,
    ATTR_EFFECT,
    ATTR_HS_COLOR,
    ATTR_TRANSITION,
    PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
    ColorMode,
    LightEntity,
    LightEntityFeature,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import color as color_util

_LOGGER = logging.getLogger(__name__)


DEFAULT_NAME = "LW-12 FC"
DEFAULT_PORT = 5000

PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_HOST): cv.string,
        vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
    }
)


def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up LW-12 WiFi LED Controller platform."""
    # Assign configuration variables.
    name = config.get(CONF_NAME)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    # Add devices
    lw12_light = lw12.LW12Controller(host, port)
    add_entities([LW12WiFi(name, lw12_light)])


class LW12WiFi(LightEntity):
    """LW-12 WiFi LED Controller."""

    _attr_assumed_state = True
    _attr_color_mode = ColorMode.HS
    _attr_should_poll = False
    _attr_supported_color_modes = {ColorMode.HS}
    _attr_supported_features = LightEntityFeature.EFFECT | LightEntityFeature.TRANSITION

    def __init__(self, name, lw12_light):
        """Initialise LW-12 WiFi LED Controller.

        :param name: Friendly name for this platform to use.
        :param lw12_light: Instance of the LW12 controller.
        """
        self._light = lw12_light
        self._attr_name = name
        self._effect = None
        self._rgb_color = [255, 255, 255]
        self._attr_brightness = 255

    @property
    @override
    def hs_color(self) -> tuple[float, float]:
        """Read back the hue-saturation of the light."""
        return color_util.color_RGB_to_hs(*self._rgb_color)

    @property
    @override
    def effect(self) -> str | None:
        """Return current light effect."""
        if self._effect is None:
            return None
        return self._effect.replace("_", " ").title()

    @property
    @override
    def effect_list(self) -> list[str]:
        """Return a list of available effects.

        Use the Enum element name for display.
        """
        return [effect.name.replace("_", " ").title() for effect in lw12.LW12_EFFECT]

    @override
    def turn_on(self, **kwargs: Any) -> None:
        """Instruct the light to turn on."""
        self._light.light_on()
        if ATTR_HS_COLOR in kwargs:
            self._rgb_color = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
            self._light.set_color(*self._rgb_color)
            self._effect = None
        if ATTR_BRIGHTNESS in kwargs:
            self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
            brightness = int(self._attr_brightness / 255 * 100)
            self._light.set_light_option(lw12.LW12_LIGHT.BRIGHTNESS, brightness)
        if ATTR_EFFECT in kwargs:
            self._effect = kwargs[ATTR_EFFECT].replace(" ", "_").upper()
            # Check if a known and supported effect was selected.
            if self._effect in [eff.name for eff in lw12.LW12_EFFECT]:
                # Selected effect is supported and will be applied.
                self._light.set_effect(lw12.LW12_EFFECT[self._effect])
            else:
                # Unknown effect was set, recover by disabling the effect
                # mode and log an error.
                _LOGGER.error("Unknown effect selected: %s", self._effect)
                self._effect = None
        if ATTR_TRANSITION in kwargs:
            transition_speed = int(kwargs[ATTR_TRANSITION])
            self._light.set_light_option(lw12.LW12_LIGHT.FLASH, transition_speed)
        self._attr_is_on = True

    @override
    def turn_off(self, **kwargs: Any) -> None:
        """Instruct the light to turn off."""
        self._light.light_off()
        self._attr_is_on = False
