import main


class SwhoCommand:
    def __init__(self, *args):
        self.swho(*args)

    def swho(
        self, addr, authed, data, obj, spl, success, failure, info, incUsage, length
    ):
        if authed:
            if length == 2:
                if not spl[1] in main.network.keys():
                    failure("Network does not exist: %s" % spl[1])
                    return
                for i in main.IRCPool.keys():
                    if spl[1] == main.IRCPool[i].net:
                        for x in main.IRCPool[i].channels:
                            main.IRCPool[i].who(x)
                success("Sent WHO to all channels on all networks for %s" % spl[1])
                return
            elif length == 3:
                if not spl[1] in main.network.keys():
                    failure("Network does not exist: %s" % spl[1])
                    return
                matches = []

                # This loop gets all networks where the core network matches spl[1]
                #  where there is also a currently joined channel matching spl[2]
                for i in main.IRCPool.keys():
                    if spl[1] == main.IRCPool[i].net:
                        for x in main.IRCPool[i].channels:
                            if x == spl[2]:
                                main.IRCPool[i].who(x)
                                matches.append(i)
                if matches == []:
                    failure("No matches found for channel %s" % spl[2])
                    return
                success("Sent WHO to %s on: %s" % (spl[2], ", ".join(matches)))
                return

            else:
                incUsage("swho")
                return
        else:
            incUsage(None)
