import main
from modules import helpers


class ListCommand:
    def __init__(self, *args):
        self.list(*args)

    def list(
        self, addr, authed, data, obj, spl, success, failure, info, incUsage, length
    ):
        if authed:
            if length == 1:
                for i in main.network.keys():
                    first_relay = helpers.get_first_relay(i)
                    ####
                    if not first_relay:
                        info("Network has no first instance: %s" % i)
                        continue
                    first_relay.list()
                    success(f"Requested list with instance {first_relay.num} of {i}")
                return
            elif length == 2:
                if not spl[1] in main.network.keys():
                    failure("No such network: %s" % spl[1])
                    return
                first_relay = helpers.get_first_relay(spl[1])
                if not first_relay:
                    failure("Could not get first instance")
                    return
                first_relay.list()
                success(f"Requested list with instance {first_relay.num} of {spl[1]}")
                return
            else:
                incUsage("list")
                return
        else:
            incUsage(None)
