"""Remote control support for Panasonic Viera TV."""

from collections.abc import Iterable
from typing import Any, override

from homeassistant.components.remote import RemoteEntity
from homeassistant.const import ATTR_MANUFACTURER, CONF_NAME, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import PanasonicVieraConfigEntry, Remote
from .const import (
    ATTR_DEVICE_INFO,
    ATTR_MODEL_NUMBER,
    ATTR_UDN,
    DEFAULT_MANUFACTURER,
    DEFAULT_MODEL_NUMBER,
    DOMAIN,
)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: PanasonicVieraConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Panasonic Viera TV Remote from a config entry."""

    config = config_entry.data

    remote = config_entry.runtime_data
    name = config[CONF_NAME]
    device_info = config[ATTR_DEVICE_INFO]

    async_add_entities([PanasonicVieraRemoteEntity(remote, name, device_info)])


class PanasonicVieraRemoteEntity(RemoteEntity):
    """Representation of a Panasonic Viera TV Remote."""

    def __init__(
        self, remote: Remote, name: str, device_info: dict[str, Any] | None = None
    ) -> None:
        """Initialize the entity."""
        # Save a reference to the imported class
        self._remote = remote
        self._name = name
        self._device_info = device_info

    @property
    @override
    def unique_id(self) -> str | None:
        """Return the unique ID of the device."""
        if self._device_info is None:
            return None
        return self._device_info[ATTR_UDN]

    @property
    @override
    def device_info(self) -> DeviceInfo | None:
        """Return device specific attributes."""
        if self._device_info is None:
            return None
        return DeviceInfo(
            identifiers={(DOMAIN, self._device_info[ATTR_UDN])},
            manufacturer=self._device_info.get(ATTR_MANUFACTURER, DEFAULT_MANUFACTURER),
            model=self._device_info.get(ATTR_MODEL_NUMBER, DEFAULT_MODEL_NUMBER),
            name=self._name,
        )

    @property
    @override
    def name(self) -> str:
        """Return the name of the device."""
        return self._name

    @property
    @override
    def available(self) -> bool:
        """Return True if the device is available."""
        return self._remote.available

    @property
    @override
    def is_on(self) -> bool:
        """Return true if device is on."""
        return self._remote.state == STATE_ON

    @override
    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the device on."""
        await self._remote.async_turn_on(context=self._context)

    @override
    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn the device off."""
        await self._remote.async_turn_off()

    @override
    async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
        """Send a command to one device."""
        for cmd in command:
            await self._remote.async_send_key(cmd)
