#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
"""Provides handling of TCC gateways."""
from __future__ import annotations

from typing import TYPE_CHECKING, Final

from .controlsystem import ControlSystem
from .exceptions import InvalidSchema
from .schema.const import (
    SZ_GATEWAY,
    SZ_GATEWAY_ID,
    SZ_GATEWAY_INFO,
    SZ_IS_WI_FI,
    SZ_MAC,
    SZ_TEMPERATURE_CONTROL_SYSTEMS,
)
from .schema.status import SCH_GATEWAY

if TYPE_CHECKING:
    import logging

    from . import Broker, Location
    from .schema import _EvoDictT, _GatewayIdT


class Gateway:
    """Instance of a location's gateway."""

    STATUS_SCHEMA: Final = SCH_GATEWAY
    TYPE: Final[str] = SZ_GATEWAY

    def __init__(self, location: Location, config: _EvoDictT) -> None:
        self.location = location  # parent

        self._broker: Broker = location._broker
        self._logger: logging.Logger = location._logger

        self._status: _EvoDictT = {}
        self._config: Final[_EvoDictT] = config[SZ_GATEWAY_INFO]

        try:
            assert self.gatewayId, "Invalid config dict"
        except AssertionError as exc:
            raise InvalidSchema(str(exc))
        self._id = self.gatewayId

        self._control_systems: list[ControlSystem] = []
        self.control_systems: dict[str, ControlSystem] = {}  # tcs by id

        tcs_config: _EvoDictT

        for tcs_config in config[SZ_TEMPERATURE_CONTROL_SYSTEMS]:
            tcs = ControlSystem(self, tcs_config)

            self._control_systems.append(tcs)
            self.control_systems[tcs.systemId] = tcs

    def __str__(self) -> str:
        return f"{self._id} ({self.TYPE})"

    @property
    def gatewayId(self) -> _GatewayIdT:
        return self._config[SZ_GATEWAY_ID]

    @property
    def mac(self) -> str:
        return self._config[SZ_MAC]

    @property
    def isWiFi(self) -> bool:
        return self._config[SZ_IS_WI_FI]

    def _update_status(self, status: _EvoDictT) -> None:
        self._status = status
