import network
from time import sleep

def connect(ssid, password, ip, gateway):
    if ssid is None:
        print("Missing ssid")
        return None
    if password is None:
        password = ''
    try:
        sta_if = network.WLAN(network.STA_IF)
        if ip is not None and gateway is not None:
            sta_if.ifconfig((ip, '255.255.255.0', gateway, '1.1.1.1'))
        if not sta_if.isconnected():
            print('connecting to network...')
            sta_if.active(True)
            sta_if.connect(ssid, password)
            sleep(0.1)
            if sta_if.isconnected():
                return sta_if.ifconfig()
            return None
        return sta_if.ifconfig()
    except Exception as e:
        print(f"Failed to connect to wifi {e}")
        return None


def ap(ssid, password, channel=None):
    ap_if = network.WLAN(network.AP_IF)
    ap_mac = ap_if.config('mac')
    print(ssid)
    ap_if.active(True)
    if channel is not None:
        ap_if.config(essid=ssid, password=password, channel=channel)
    else:
        ap_if.config(essid=ssid, password=password)
    ap_if.active(False)
    ap_if.active(True)
    print(ap_if.ifconfig())

def get_mac():
    ap_if = network.WLAN(network.AP_IF)
    return ap_if.config('mac')

def get_ap_config():
    """Get current AP configuration."""
    try:
        ap_if = network.WLAN(network.AP_IF)
        if ap_if.active():
            config = ap_if.ifconfig()
            return {
                'ssid': ap_if.config('essid'),
                'channel': ap_if.config('channel'),
                'ip': config[0] if config else None,
                'active': True
            }
        return {
            'ssid': None,
            'channel': None,
            'ip': None,
            'active': False
        }
    except Exception as e:
        print(f"Error getting AP config: {e}")
        return None

def get_sta_status():
    """Get current station connection status."""
    try:
        sta_if = network.WLAN(network.STA_IF)
        if sta_if.active():
            if sta_if.isconnected():
                config = sta_if.ifconfig()
                return {
                    'connected': True,
                    'ssid': sta_if.config('essid'),
                    'ip': config[0] if config else None,
                    'gateway': config[2] if len(config) > 2 else None,
                    'netmask': config[1] if len(config) > 1 else None,
                    'dns': config[3] if len(config) > 3 else None
                }
            return {
                'connected': False,
                'ssid': None,
                'ip': None,
                'gateway': None,
                'netmask': None,
                'dns': None
            }
        return {
            'connected': False,
            'ssid': None,
            'ip': None,
            'gateway': None,
            'netmask': None,
            'dns': None
        }
    except Exception as e:
        print(f"Error getting STA status: {e}")
        return None
