"""Support for SLZB buttons."""

from collections.abc import Awaitable, Callable
from dataclasses import dataclass
import logging
from typing import override

from pysmlight.web import CmdWrapper

from homeassistant.components.button import (
    DOMAIN as BUTTON_DOMAIN,
    ButtonDeviceClass,
    ButtonEntity,
    ButtonEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import DOMAIN, ZWAVE_TYPES
from .coordinator import SmConfigEntry, SmDataUpdateCoordinator
from .entity import SmEntity

PARALLEL_UPDATES = 1

_LOGGER = logging.getLogger(__name__)


@dataclass(frozen=True, kw_only=True)
class SmButtonDescription(ButtonEntityDescription):
    """Class to describe a Button entity."""

    press_fn: Callable[[CmdWrapper, int], Awaitable[None]]


CORE_BUTTON = SmButtonDescription(
    key="core_restart",
    translation_key="core_restart",
    device_class=ButtonDeviceClass.RESTART,
    press_fn=lambda cmd, idx: cmd.reboot(),
)

RADIO_BUTTONS: list[SmButtonDescription] = [
    SmButtonDescription(
        key="zigbee_restart",
        translation_key="zigbee_restart",
        device_class=ButtonDeviceClass.RESTART,
        press_fn=lambda cmd, idx: cmd.zb_restart(idx=idx),
    ),
    SmButtonDescription(
        key="zigbee_flash_mode",
        translation_key="zigbee_flash_mode",
        entity_registry_enabled_default=False,
        press_fn=lambda cmd, idx: cmd.zb_bootloader(idx=idx),
    ),
]

ROUTER = SmButtonDescription(
    key="reconnect_zigbee_router",
    translation_key="reconnect_zigbee_router",
    entity_registry_enabled_default=False,
    press_fn=lambda cmd, idx: cmd.zb_router(idx=idx),
)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: SmConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up SMLIGHT buttons based on a config entry."""
    coordinator = entry.runtime_data.data
    radios = coordinator.data.info.radios

    entities = [SmButton(coordinator, CORE_BUTTON)]
    count = len(radios) if coordinator.data.info.u_device else 1

    for idx in range(count):
        entities.extend(SmButton(coordinator, button, idx) for button in RADIO_BUTTONS)

    async_add_entities(entities)
    entity_created = [False] * len(radios)

    @callback
    def _check_router(startup: bool = False) -> None:
        def router_entity(router: SmButtonDescription, idx: int) -> None:
            nonlocal entity_created
            zb_type = coordinator.data.info.radios[idx].zb_type

            if zb_type == 1 and not entity_created[idx]:
                async_add_entities([SmButton(coordinator, router, idx)])
                entity_created[idx] = True
            elif zb_type != 1 and (startup or entity_created[idx]):
                entity_registry = er.async_get(hass)
                button = f"_{idx}" if idx else ""
                if entity_id := entity_registry.async_get_entity_id(
                    BUTTON_DOMAIN,
                    DOMAIN,
                    f"{coordinator.unique_id}-{router.key}{button}",
                ):
                    entity_registry.async_remove(entity_id)

        for idx, _ in enumerate(radios):
            router_entity(ROUTER, idx)

    coordinator.async_add_listener(_check_router)
    _check_router(startup=True)


class SmButton(SmEntity, ButtonEntity):
    """Defines a SLZB button."""

    coordinator: SmDataUpdateCoordinator
    entity_description: SmButtonDescription
    _attr_entity_category = EntityCategory.CONFIG

    def __init__(
        self,
        coordinator: SmDataUpdateCoordinator,
        description: SmButtonDescription,
        idx: int = 0,
    ) -> None:
        """Initialize SLZB button entity."""
        super().__init__(coordinator)

        self.entity_description = description
        self.idx = idx
        button = f"_{idx}" if idx else ""
        self._attr_unique_id = f"{coordinator.unique_id}-{description.key}{button}"

        if (
            idx < len(coordinator.data.info.radios)
            and coordinator.data.info.radios[idx].zb_type in ZWAVE_TYPES
        ):
            if description.key == "zigbee_restart":
                self._attr_translation_key = "z_wave_restart"
            elif description.key == "zigbee_flash_mode":
                self._attr_translation_key = "z_wave_flash_mode"

    @override
    async def async_press(self) -> None:
        """Trigger button press."""
        await self.entity_description.press_fn(self.coordinator.client.cmds, self.idx)
