"""Plugwise Select component for Home Assistant."""

from __future__ import annotations

from dataclasses import dataclass

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import STATE_ON, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import (
    SELECT_DHW_MODE,
    SELECT_GATEWAY_MODE,
    SELECT_REGULATION_MODE,
    SELECT_SCHEDULE,
    SELECT_ZONE_PROFILE,
    SelectOptionsType,
    SelectType,
)
from .coordinator import PlugwiseConfigEntry, PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity
from .util import plugwise_command

PARALLEL_UPDATES = 0


@dataclass(frozen=True, kw_only=True)
class PlugwiseSelectEntityDescription(SelectEntityDescription):
    """Class describing Plugwise Select entities."""

    key: SelectType
    options_key: SelectOptionsType


SELECT_TYPES = (
    PlugwiseSelectEntityDescription(
        key=SELECT_SCHEDULE,
        translation_key=SELECT_SCHEDULE,
        options_key="available_schedules",
    ),
    PlugwiseSelectEntityDescription(
        key=SELECT_REGULATION_MODE,
        translation_key=SELECT_REGULATION_MODE,
        entity_category=EntityCategory.CONFIG,
        options_key="regulation_modes",
    ),
    PlugwiseSelectEntityDescription(
        key=SELECT_DHW_MODE,
        translation_key=SELECT_DHW_MODE,
        entity_category=EntityCategory.CONFIG,
        options_key="dhw_modes",
    ),
    PlugwiseSelectEntityDescription(
        key=SELECT_GATEWAY_MODE,
        translation_key=SELECT_GATEWAY_MODE,
        entity_category=EntityCategory.CONFIG,
        options_key="gateway_modes",
    ),
    PlugwiseSelectEntityDescription(
        key=SELECT_ZONE_PROFILE,
        translation_key=SELECT_ZONE_PROFILE,
        entity_category=EntityCategory.CONFIG,
        options_key="zone_profiles",
    ),
)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: PlugwiseConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the Smile selector from a config entry."""
    coordinator = entry.runtime_data

    @callback
    def _add_entities() -> None:
        """Add Entities."""
        if not coordinator.new_devices:
            return

        async_add_entities(
            PlugwiseSelectEntity(coordinator, device_id, description)
            for device_id in coordinator.new_devices
            for description in SELECT_TYPES
            if coordinator.data[device_id].get(description.options_key)
        )

    _add_entities()
    entry.async_on_unload(coordinator.async_add_listener(_add_entities))


class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity):
    """Represent Smile selector."""

    entity_description: PlugwiseSelectEntityDescription

    def __init__(
        self,
        coordinator: PlugwiseDataUpdateCoordinator,
        device_id: str,
        entity_description: PlugwiseSelectEntityDescription,
    ) -> None:
        """Initialise the selector."""
        super().__init__(coordinator, device_id)
        self._attr_unique_id = f"{device_id}-{entity_description.key}"
        self.entity_description = entity_description

        self._location = device_id
        if (location := self.device.get("location")) is not None:
            self._location = location

    @property
    def current_option(self) -> str | None:
        """Return the selected entity option to represent the entity state."""
        return self.device[self.entity_description.key]

    @property
    def options(self) -> list[str]:
        """Return the available select-options."""
        return self.device[self.entity_description.options_key]

    @plugwise_command
    async def async_select_option(self, option: str) -> None:
        """Change to the selected entity option.

        self._location and STATE_ON are required for the thermostat-schedule select.
        """
        await self.coordinator.api.set_select(
            self.entity_description.key, self._location, option, STATE_ON
        )
