"""Support for Verizon FiOS Quantum Gateways."""

from typing import override

from quantum_gateway import QuantumGatewayScanner
from requests.exceptions import RequestException
import voluptuous as vol

from homeassistant.components.device_tracker import (
    DOMAIN as DEVICE_TRACKER_DOMAIN,
    PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
    DeviceScanner,
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_SSL
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType

from .const import DEFAULT_HOST, LOGGER

PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
    {
        vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
        vol.Optional(CONF_SSL, default=True): cv.boolean,
        vol.Required(CONF_PASSWORD): cv.string,
    }
)


def get_scanner(
    hass: HomeAssistant, config: ConfigType
) -> QuantumGatewayDeviceScanner | None:
    """Validate the configuration and return a Quantum Gateway scanner."""
    scanner = QuantumGatewayDeviceScanner(config[DEVICE_TRACKER_DOMAIN])

    return scanner if scanner.success_init else None


class QuantumGatewayDeviceScanner(DeviceScanner):
    """Class which queries a Quantum Gateway."""

    def __init__(self, config) -> None:
        """Initialize the scanner."""

        self.host = config[CONF_HOST]
        self.password = config[CONF_PASSWORD]
        self.use_https = config[CONF_SSL]
        LOGGER.debug("Initializing")

        try:
            self.quantum = QuantumGatewayScanner(
                self.host, self.password, self.use_https
            )
            self.success_init = self.quantum.success_init
        except RequestException:
            self.success_init = False
            LOGGER.error("Unable to connect to gateway. Check host")

        if not self.success_init:
            LOGGER.error("Unable to login to gateway. Check password and host")

    @override
    def scan_devices(self):
        """Scan for new devices and return a list of found MACs."""
        connected_devices = []
        try:
            connected_devices = self.quantum.scan_devices()
        except RequestException:
            LOGGER.error("Unable to scan devices. Check connection to router")
        return connected_devices

    @override
    def get_device_name(self, device):
        """Return the name of the given device or None if we don't know."""
        return self.quantum.get_device_name(device)
