import main
from yaml import dump


class BlacklistCommand:
    def __init__(self, *args):
        self.blacklist(*args)

    def blacklist(
        self, addr, authed, data, obj, spl, success, failure, info, incUsage, length
    ):
        if authed:
            if length == 1:
                info(dump(main.blacklist))
                return
            elif length == 4:
                if spl[1] == "add":
                    if spl[2] in main.blacklist.keys():
                        main.blacklist[spl[2]].append(spl[3])
                    else:
                        main.blacklist[spl[2]] = [spl[3]]
                    success("Blacklisted %s on %s" % (spl[3], spl[2]))
                    main.saveConf("blacklist")
                    return
                elif spl[1] == "del":
                    if spl[2] in main.blacklist.keys():
                        if spl[3] in main.blacklist[spl[2]]:
                            main.blacklist[spl[2]].remove(spl[3])
                            if len(main.blacklist[spl[2]]) == 0:
                                del main.blacklist[spl[2]]
                        else:
                            failure("Not in list: %s" % spl[3])
                            return
                    else:
                        failure("No such entry: %s" % spl[2])
                        return
                    success("Removed blacklist %s on %s" % (spl[3], spl[2]))
                    main.saveConf("blacklist")
                    return
            else:
                incUsage("blacklist")
                return
        else:
            incUsage(None)
