import bot
import random

# just a fun thing that works like XKCD's bucket
class Bucket(BotCommand):
    def __init__(self):
        self.bot = bot.factory()

    def give(self, nick, *args):
        to_drop_idx = random.randint(0, len(self.bot.bucket))
        to_drop = None
        if to_drop_idx:
            try:
                to_drop = self.bot.bucket[to_drop_idx]
            except IndexError:
                pass
        dropped = self._do_drop(to_drop)

        self.bot.bucket.append(thing)
        
        msg = "{} took {}".format(self.bot.botname, thing)
        if dropped:
            msg += " and dropped {}".format(to_drop)

        return msg

    def drop(self, nick, *args):
        thing = args[0]
        if self._do_drop(thing):
            msg = "{} dropped {}"
        else:
            msg = "{} wasn't carrying {}"

        return msg.format(self.bot.botname, thing)

    def _do_drop(self, thing):
        if thing in self.bot.bucket:
            self.bot.bucket.remove(thing)
            return True

        return False
