"""Remote control support for Android TV Remote."""
from __future__ import annotations

import asyncio
from collections.abc import Iterable
from typing import Any

from androidtvremote2 import AndroidTVRemote

from homeassistant.components.remote import (
    ATTR_ACTIVITY,
    ATTR_DELAY_SECS,
    ATTR_HOLD_SECS,
    ATTR_NUM_REPEATS,
    DEFAULT_DELAY_SECS,
    DEFAULT_HOLD_SECS,
    DEFAULT_NUM_REPEATS,
    RemoteEntity,
    RemoteEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .entity import AndroidTVRemoteBaseEntity

PARALLEL_UPDATES = 0


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    async_add_entities: AddEntitiesCallback,
) -> None:
    """Set up the Android TV remote entity based on a config entry."""
    api: AndroidTVRemote = hass.data[DOMAIN][config_entry.entry_id]
    async_add_entities([AndroidTVRemoteEntity(api, config_entry)])


class AndroidTVRemoteEntity(AndroidTVRemoteBaseEntity, RemoteEntity):
    """Android TV Remote Entity."""

    _attr_supported_features = RemoteEntityFeature.ACTIVITY

    @callback
    def _current_app_updated(self, current_app: str) -> None:
        """Update the state when the current app changes."""
        self._attr_current_activity = current_app
        self.async_write_ha_state()

    async def async_added_to_hass(self) -> None:
        """Register callbacks."""
        await super().async_added_to_hass()

        self._attr_current_activity = self._api.current_app
        self._api.add_current_app_updated_callback(self._current_app_updated)

    async def async_will_remove_from_hass(self) -> None:
        """Remove callbacks."""
        await super().async_will_remove_from_hass()

        self._api.remove_current_app_updated_callback(self._current_app_updated)

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the Android TV on."""
        if not self.is_on:
            self._send_key_command("POWER")
        activity = kwargs.get(ATTR_ACTIVITY, "")
        if activity:
            self._send_launch_app_command(activity)

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn the Android TV off."""
        if self.is_on:
            self._send_key_command("POWER")

    async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
        """Send commands to one device."""
        num_repeats = kwargs.get(ATTR_NUM_REPEATS, DEFAULT_NUM_REPEATS)
        delay_secs = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
        hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)

        for _ in range(num_repeats):
            for single_command in command:
                if hold_secs:
                    self._send_key_command(single_command, "START_LONG")
                    await asyncio.sleep(hold_secs)
                    self._send_key_command(single_command, "END_LONG")
                else:
                    self._send_key_command(single_command, "SHORT")
                await asyncio.sleep(delay_secs)
