import urllib.parse

from core.lib.threshold import threshold_request


def get_irc_stats():
    url = "irc/stats"
    payload = {}
    stats = threshold_request(url, payload)
    if not stats:
        return {}
    return stats


def get_irc_networks():
    url = "irc/networks"
    payload = {}
    networks = threshold_request(url, payload)
    if not networks:
        return {}
    return networks


def get_irc_network(net):
    url = f"irc/network/{net}"
    payload = {}
    network = threshold_request(url, payload)
    if not network:
        return {}
    return network


def edit_irc_network(net, data):
    url = f"irc/network/{net}/edit"
    payload = dict(data)
    result = threshold_request(url, payload)
    return result


def change_network_status(net, num, status):
    url = f"irc/network/{net}/{num}"
    payload = {"status": status}
    result = threshold_request(url, payload)
    return result


def get_irc_relays(net):
    url = f"irc/network/{net}/relays"
    payload = {}
    relays = threshold_request(url, payload)
    if not relays:
        return {}
    return relays


def get_irc_channels(net):
    url = f"irc/network/{net}/channels"
    payload = {}
    channels = threshold_request(url, payload)
    if not channels:
        return {}
    return channels


def part_channel(net, channel):
    channel = urllib.parse.quote(channel, safe="")
    url = f"irc/network/{net}/channel/{channel}"
    payload = {}
    parted = threshold_request(url, payload, method="DELETE")
    if not parted:
        return {}
    return parted


def join_channel(net, channel):
    channel = urllib.parse.quote(channel, safe="")
    url = f"irc/network/{net}/channel/{channel}"
    payload = {}
    joined = threshold_request(url, payload, method="PUT")
    if not joined:
        return {}
    return joined

def get_aliases():
    url = "aliases"
    payload = {}
    aliases = threshold_request(url, payload, method="GET")
    print("Aliases", aliases)
    return aliases
