import main
import modules.chankeep


class JoinCommand:
    def __init__(self, *args):
        self.join(*args)

    def join(
        self, addr, authed, data, obj, spl, success, failure, info, incUsage, length
    ):
        if authed:
            if length == 3:
                if not spl[1] in main.network.keys():
                    failure("Network does not exist: %s" % spl[1])
                    return
                modules.chankeep.joinSingle(spl[1], spl[2])
                success("Joined %s" % spl[2])

            elif length == 4:
                if not spl[1] in main.network.keys():
                    failure("Network does not exist: %s" % spl[1])
                    return
                if not int(spl[2]) in main.network[spl[1]].relays.keys():
                    failure("Relay %s does not exist on network %s" % (spl[2], spl[1]))
                    return
                if not spl[1] + spl[2] in main.IRCPool.keys():
                    failure("Name has no instance: %s" % spl[1])
                    return
                main.IRCPool[spl[1] + spl[2]].join(spl[3])
                success("Joined %s" % spl[3])
                return
            elif length == 5:
                if not spl[1] in main.network.keys():
                    failure("Network does not exist: %s" % spl[1])
                    return
                if not int(spl[2]) in main.network[spl[1]].relays.keys():
                    failure("Relay % does not exist on network %", (spl[2], spl[1]))
                    return
                if not spl[1] + spl[2] in main.IRCPool.keys():
                    failure("Name has no instance: %s" % spl[1])
                    return
                main.IRCPool[spl[1] + spl[2]].join(spl[3], spl[4])
                success("Joined %s with key %s" % (spl[3], spl[4]))
                return
            else:
                incUsage("join")
                return
        else:
            incUsage(None)
