import main
from yaml import dump


class EmailCommand:
    def __init__(self, *args):
        self.email(*args)

    def email(
        self, addr, authed, data, obj, spl, success, failure, info, incUsage, length
    ):
        if authed:
            if length == 4:
                if spl[1] == "add":
                    if spl[2].isdigit():
                        num = int(spl[2])
                        if num not in main.alias.keys():
                            failure("No such alias: %i" % num)
                            return
                        if not spl[3] in main.alias[num]["emails"]:
                            main.alias[num]["emails"].append(spl[3])
                            main.saveConf("alias")
                            success(
                                "Successfully added email %s to alias %i"
                                % (spl[3], num)
                            )
                            return
                        else:
                            failure(
                                "Email already exists in alias %i: %s" % (num, spl[3])
                            )
                            return
                    else:
                        failure("Must be a number, not %s" % spl[2])
                        if spl[2] == "domain":
                            domain = spl[3]
                            if "@" in domain:
                                failure("Not a domain: %s" % domain)
                                return
                            if domain not in main.irc["_"]["domains"]:
                                main.irc["_"]["domains"].append(domain)
                                success(
                                    "Successfully added domain %s to default config"
                                    % domain
                                )
                                main.saveConf("irc")
                            else:
                                failure(
                                    "Domain already exists in default config: %s"
                                    % domain
                                )
                                return

                elif spl[1] == "del":
                    if not spl[2].isdigit():
                        # failure("Must be a number, not %s" % spl[2])
                        if spl[2] == "domain":
                            domain = spl[3]

                            if domain in main.irc["_"]["domains"]:
                                main.irc["_"]["domains"].remove(domain)
                                success(
                                    "Successfully removed domain %s to default config"
                                    % domain
                                )
                                main.saveConf("irc")
                            else:
                                failure(
                                    "Domain does not exist in default config: %s"
                                    % domain
                                )
                                return

                    elif spl[1] == "del":
                        if num not in main.alias.keys():
                            failure("No such alias: %i" % num)
                            return
                        if spl[3] in main.alias[num]["emails"]:
                            main.alias[num]["emails"].remove(spl[3])
                            main.saveConf("alias")
                            success(
                                "Successfully removed email %s from alias %i"
                                % (spl[3], num)
                            )
                            return
                        else:
                            failure(
                                "Email does not exist in alias %i: %s" % (spl[3], num)
                            )
                            return
            elif length == 2:
                if spl[1] == "list":
                    info(dump(main.alias))
                    return
                else:
                    incUsage("email")
                    return
            elif length == 3:
                if spl[1] == "list":
                    if spl[2] == "domain":
                        filtered = {
                            f"{k}:{k2}": v2
                            for k, v in main.irc.items()
                            for k2, v2 in v.items()
                            if k2 == "domains"
                        }
                        info(dump(filtered))
                        return
                    else:
                        incUsage("email")
                        return
                else:
                    incUsage("email")
                    return
            else:
                incUsage("email")
                return
        else:
            incUsage(None)
