from __future__ import annotations

import subprocess
from collections.abc import Iterator
from datetime import datetime as dt

from CoreWLAN import CWInterface, CWNetwork, CWWiFiClient

from vorta.log import logger
from vorta.network_status.abc import NetworkStatusMonitor, SystemWifiInfo


class DarwinNetworkStatus(NetworkStatusMonitor):
    def __init__(self) -> None:
        super().__init__()

    def is_network_metered(self) -> bool:
        interface: CWInterface = self._get_wifi_interface()

        # If there is no Wifi at all.
        if interface is None:
            return False

        network: CWNetwork | None = interface.lastNetworkJoined()

        if network:
            is_ios_hotspot = network.isPersonalHotspot()
        else:
            is_ios_hotspot = False

        return is_ios_hotspot or any(is_network_metered_with_android(d) for d in get_network_devices())

    def is_network_active(self) -> bool:
        # Not yet implemented
        return True

    def get_current_wifi(self) -> str | None:
        """
        Get current SSID or None if Wi-Fi is off.
        """
        interface: CWInterface | None = self._get_wifi_interface()
        if not interface:
            return None

        # If the user has Wi-Fi turned off lastNetworkJoined will return None.
        network: CWNetwork | None = interface.lastNetworkJoined()

        if network:
            network_name = network.ssid()
            return network_name
        else:
            return None

    def get_known_wifis(self) -> list[SystemWifiInfo]:
        """
        Use the program, "networksetup", to get the list of know Wi-Fi networks.
        """

        wifis = []
        interface: CWInterface | None = self._get_wifi_interface()
        if not interface:
            return []

        interface_name = interface.name()
        output = call_networksetup_listpreferredwirelessnetworks(interface_name)

        result = []
        for line in output.strip().splitlines():
            if line.strip().startswith("Preferred networks"):
                continue
            elif not line.strip():
                continue
            else:
                result.append(line.strip())

        for wifi_network_name in result:
            wifis.append(SystemWifiInfo(ssid=wifi_network_name, last_connected=dt.now()))

        return wifis

    def _get_wifi_interface(self) -> CWInterface | None:
        wifi_client: CWWiFiClient = CWWiFiClient.sharedWiFiClient()
        interface: CWInterface | None = wifi_client.interface()
        return interface


def get_network_devices() -> Iterator[str]:
    for line in call_networksetup_listallhardwareports().splitlines():
        if line.startswith(b'Device: '):
            yield line.split()[1].strip().decode('ascii')


def is_network_metered_with_android(bsd_device: str) -> bool:
    return b'ANDROID_METERED' in call_ipconfig_getpacket(bsd_device)


def call_ipconfig_getpacket(bsd_device: str) -> bytes:
    cmd = ['ipconfig', 'getpacket', bsd_device]
    try:
        return subprocess.check_output(cmd)
    except subprocess.CalledProcessError:
        logger.debug("Command %s failed", ' '.join(cmd))
        return b''


def call_networksetup_listallhardwareports() -> bytes:
    cmd = ['/usr/sbin/networksetup', '-listallhardwareports']
    try:
        return subprocess.check_output(cmd)
    except (subprocess.CalledProcessError, FileNotFoundError):
        logger.debug("Command %s failed", ' '.join(cmd))
        return b''


def call_networksetup_listpreferredwirelessnetworks(interface: str) -> str:
    command = ['/usr/sbin/networksetup', '-listpreferredwirelessnetworks', interface]
    try:
        return subprocess.check_output(command).decode(encoding='utf-8')
    except (subprocess.CalledProcessError, FileNotFoundError):
        logger.debug("Command %s failed", " ".join(command))
        return ''
