"""Support for Gogogate2 garage Doors."""

from typing import Any, override

from ismartgate.common import (
    AbstractDoor,
    DoorStatus,
    TransitionDoorStatus,
    get_configured_doors,
)

from homeassistant.components.cover import (
    CoverDeviceClass,
    CoverEntity,
    CoverEntityFeature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .common import cover_unique_id
from .coordinator import DeviceDataUpdateCoordinator, GogoGateConfigEntry
from .entity import GoGoGate2Entity


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: GogoGateConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the config entry."""
    data_update_coordinator = config_entry.runtime_data

    async_add_entities(
        [
            DeviceCover(config_entry, data_update_coordinator, door)
            for door in get_configured_doors(data_update_coordinator.data)
        ]
    )


class DeviceCover(GoGoGate2Entity, CoverEntity):
    """Cover entity for gogogate2."""

    _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

    def __init__(
        self,
        config_entry: GogoGateConfigEntry,
        data_update_coordinator: DeviceDataUpdateCoordinator,
        door: AbstractDoor,
    ) -> None:
        """Initialize the object."""
        unique_id = cover_unique_id(config_entry, door)
        super().__init__(config_entry, data_update_coordinator, door, unique_id)
        self._attr_device_class = (
            CoverDeviceClass.GATE if self.door.gate else CoverDeviceClass.GARAGE
        )

    @property
    @override
    def name(self) -> str | None:
        """Return the name of the door."""
        return self.door.name

    @property
    @override
    def is_closed(self) -> bool | None:
        """Return true if cover is closed, else False."""
        door_status = self.door_status
        if door_status == DoorStatus.OPENED:
            return False
        if door_status == DoorStatus.CLOSED:
            return True
        return None

    @property
    @override
    def is_closing(self) -> bool:
        """Return if the cover is closing or not."""
        return self.door_status == TransitionDoorStatus.CLOSING

    @property
    @override
    def is_opening(self) -> bool:
        """Return if the cover is opening or not."""
        return self.door_status == TransitionDoorStatus.OPENING

    @override
    async def async_open_cover(self, **kwargs: Any) -> None:
        """Open the door."""
        await self._api.async_open_door(self._door_id)
        await self.coordinator.async_refresh()

    @override
    async def async_close_cover(self, **kwargs: Any) -> None:
        """Close the door."""
        await self._api.async_close_door(self._door_id)
        await self.coordinator.async_refresh()
