"""Support for Bosch Alarm Panel."""

from typing import override

from bosch_alarm_mode2 import Panel

from homeassistant.components.alarm_control_panel import (
    AlarmControlPanelEntity,
    AlarmControlPanelEntityFeature,
    AlarmControlPanelState,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .entity import BoschAlarmAreaEntity
from .types import BoschAlarmConfigEntry


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: BoschAlarmConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up control panels for each area."""
    panel = config_entry.runtime_data

    async_add_entities(
        AreaAlarmControlPanel(
            hass,
            panel,
            area_id,
            config_entry.unique_id or config_entry.entry_id,
            config_entry.entry_id,
        )
        for area_id in panel.areas
    )


PARALLEL_UPDATES = 0


class AreaAlarmControlPanel(BoschAlarmAreaEntity, AlarmControlPanelEntity):
    """An alarm control panel entity for a bosch alarm panel."""

    _attr_has_entity_name = True
    _attr_supported_features = (
        AlarmControlPanelEntityFeature.ARM_HOME
        | AlarmControlPanelEntityFeature.ARM_AWAY
    )
    _attr_code_arm_required = False
    _attr_name = None

    def __init__(
        self,
        hass: HomeAssistant,
        panel: Panel,
        area_id: int,
        unique_id: str,
        config_entry_id: str,
    ) -> None:
        """Initialise a Bosch Alarm control panel entity."""
        super().__init__(
            hass, panel, area_id, unique_id, config_entry_id, True, False, True
        )
        self._attr_unique_id = self._area_unique_id

    @property
    @override
    def alarm_state(self) -> AlarmControlPanelState | None:
        """Return the state of the alarm."""
        if self._area.is_triggered():
            return AlarmControlPanelState.TRIGGERED
        if self._area.is_disarmed():
            return AlarmControlPanelState.DISARMED
        if self._area.is_arming():
            return AlarmControlPanelState.ARMING
        if self._area.is_pending():
            return AlarmControlPanelState.PENDING
        if self._area.is_part_armed():
            return AlarmControlPanelState.ARMED_HOME
        if self._area.is_all_armed():
            return AlarmControlPanelState.ARMED_AWAY
        return None

    @override
    async def async_alarm_disarm(self, code: str | None = None) -> None:
        """Disarm this panel."""
        await self.panel.area_disarm(self._area_id)

    @override
    async def async_alarm_arm_home(self, code: str | None = None) -> None:
        """Send arm home command."""
        await self.panel.area_arm_part(self._area_id)

    @override
    async def async_alarm_arm_away(self, code: str | None = None) -> None:
        """Send arm away command."""
        await self.panel.area_arm_all(self._area_id)
