"""ETI/Domo thermoregulation device."""

import logging
from typing import Optional

from .base import TYPE_THERMOSTAT, CameDevice, DeviceState

_LOGGER = logging.getLogger(__name__)


# Thermoregulation devices states
THERMO_STATE_OFF = 0
THERMO_STATE_ON = 1

# Thermoregulation devices modes
THERMO_MODE_OFF = 0
THERMO_MODE_MANUAL = 1
THERMO_MODE_AUTO = 2
THERMO_MODE_JOLLY = 3

# Thermoregulation devices seasons
THERMO_SEASON_OFF = "plant_off"
THERMO_SEASON_WINTER = "winter"
THERMO_SEASON_SUMMER = "summer"

# Thermoregulation devices dehumidifier states
THERMO_DEHUMIDIFIER_OFF = 0
THERMO_DEHUMIDIFIER_ON = 1

# Thermoregulation devices fan speeds
THERMO_FAN_SPEED_OFF = 0
THERMO_FAN_SPEED_SLOW = 1
THERMO_FAN_SPEED_MEDIUM = 2
THERMO_FAN_SPEED_FAST = 3
THERMO_FAN_SPEED_AUTO = 4


class CameThermo(CameDevice):
    """ETI/Domo thermoregulation device class."""

    def __init__(self, manager, device_info: DeviceState):
        """Init instance."""
        super().__init__(manager, TYPE_THERMOSTAT, device_info)

    @property
    def mode(self) -> Optional[int]:
        """Get current mode."""
        return self._device_info.get("mode")

    @property
    def season(self) -> Optional[str]:
        """Get current season mode."""
        return self._device_info.get("season")

    @property
    def current_temperature(self) -> Optional[float]:
        """Return the current temperature."""
        temp = self._device_info.get("temp", self._device_info.get("temp_dec"))
        return temp / 10 if temp is not None else None

    @property
    def target_temperature(self) -> Optional[float]:
        """Return the temperature we try to reach."""
        temp = self._device_info.get("set_point")
        return temp / 10 if temp is not None else None

    @property
    def support_target_temperature(self) -> bool:
        """Return True if device can change target temperature."""
        return True

    @property
    def dehumidifier_state(self) -> Optional[int]:
        """Return the state of dehumidifier."""
        dehumidifier = self._device_info.get("dehumidifier", {})
        return dehumidifier.get("enabled")

    @property
    def target_humidity(self) -> Optional[int]:
        """Return the humidity we try to reach."""
        dehumidifier = self._device_info.get("dehumidifier", {})
        return dehumidifier.get("setpoint")

    @property
    def support_target_humidity(self) -> bool:
        """Return True if device can change target humidity."""
        return self.target_humidity is not None

    @property
    def fan_speed(self) -> Optional[int]:
        """Get current fan speed."""
        return self._device_info.get("fan_speed")

    @property
    def support_fan_speed(self) -> bool:
        """Return True if device can change wind speed."""
        return self.fan_speed is not None

    def update(self):
        """Update device state."""
        self._force_update("thermo")

    def zone_config(
        self,
        mode: int = None,
        temperature: float = None,
        season: str = None,
        fan_speed: int = None,
    ):
        """Change device's config."""
        if (
            mode is None
            and temperature is None
            and season is None
            and fan_speed is None
        ):
            raise ValueError("At least one parameter is required")

        self._check_act_id()

        cmd = {
            "cmd_name": "thermo_zone_config_req",
            "act_id": self.act_id,
            "mode": mode if mode is not None else self._device_info.get("mode"),
            "set_point": int(temperature * 10)
            if temperature is not None
            else self._device_info.get("set_point"),
            "extended_infos": 0,
        }
        if season is not None:
            cmd["extended_infos"] = 1
            cmd["season"] = season
        if fan_speed is not None:
            cmd["extended_infos"] = 1
            cmd["fan_speed"] = fan_speed

        self._manager.application_request(cmd)

        log = {}
        for k in ["mode", "set_point", "season", "fan_speed"]:
            if k in cmd:
                log[k] = cmd[k]

        if mode is not None:
            log["mode"] = int(cmd["mode"] != THERMO_MODE_OFF)

        _LOGGER.debug('Set new status for thermostat "%s": %s', self.name, log)

    def set_target_temperature(self, temp: float) -> None:
        """Set the temperature we try to reach."""
        self.zone_config(temperature=temp)
