#!/usr/bin/env python

from evennia import Command, CmdSet
from evennia.prototypes.spawner import spawn

from typeclasses.objects import Object
from utils.word_list import routput

from datetime import date
import random


class CmdEat(Command):
    """
    Eat something
    """
    key = "eat"
    aliases = "bite"

    def func(self):
        self.obj.do_eat(self.caller)


class CmdSetEat(CmdSet):
    def at_cmdset_creation(self):
        self.add(CmdEat)


class Consumable(Object):
    """
    Create with a command:

    @create/drop berries;berry : typeclasses.consumables.Consumable
    @desc berries = Bright red, almost violet, berry.
    # How many berries are there?
    @set berries/amount = 10
    # How many berries do you eat at a time:
    @set berries/eat_amount = 3
    @set berries/eat_msg = "Sweet and slightly tart. [Delicious|Tangy|Mmmm]."
    @set berries/finish_msg = "Those were [delicious|great]."
    """
    amount = 4
    eat_amount = 1

    def at_object_creation(self):
        self.cmdset.add_default(CmdSetEat)
        self.db.amount = self.amount
        self.db.eat_amount = self.eat_amount

    def do_eat(self, eater):
        tea_type = self.db.tea_type or "tea"
        tea_details = self.db.tea_details

        self.db.amount = self.db.amount - self.db.eat_amount
        if self.db.eat_msgs:
            msg = random.choice(self.db.eat_msgs)
        elif self.db.eat_msg:
            msg = self.db.eat_msg
        else:
            msg = "You take a bite."
        eater.msg(routput(msg))

        if self.db.amount <= 0:
            if self.db.finish_msgs:
                msg = random.choice(self.db.finish_msgs)
            elif self.db.finish_msg:
                msg = self.db.finish_msg
            else:
                msg = "You finished it."
            eater.msg(routput(msg))
            self.delete()


class CmdMakeConsumable(Command):
    """
    Generate a new thing and give it to the caller.
    """
    key = "pick"
    # aliases = "pick"

    def func(self):
        self.obj.do_make(self.caller, self.args)


class CmdSetMakeConsumable(CmdSet):
    def at_cmdset_creation(self):
        self.add(CmdMakeConsumable)


class Producer(Object):
    """
    An object that produces a Consumable object.
    Use it to create a 'berry bush', for instance:

    @create/drop berry bush;bush : typeclasses.consumables.Producer
    @desc bush = A bush laden with bright red brambleberries.

    # Then describe what to produce:
    @set bush/make_name = "berries"
    @set bush/make_aliases = ["berry"]
    @set bush/make_verb = "pick some"
    # This one is optional as it defaults to Consumable:
    @set bush/make_class = "typeclasses.consumables.Consumable"
    @set bush/make_desc = "Bright red berry with flecks of [purple|violet|orange]."

    # How many berries are there when you pick them?
    @set bush/make_amount = 10
    # How many berries do you eat at a time:
    @set bush/make_eat_amount = 3

    # Either a single msg:
    @set bush/make_eat_msg = "Sweet and slightly tart. [Delicious|Tangy|Mmmm]."
    # Or many:
    @set bush/make_eat_msgs = [ "Sweet and slightly tart.", "[Delicious|Tangy|Mmmm].", "Ooo...that one was sour.", "That was a bit ripe, but still good.", "So sweet with a hint of [orange|maple]." ]
    @set bush/make_finish_msg = "Those were [delicious|great]."
    """
    def at_object_creation(self):
        self.cmdset.add_default(CmdSetMakeConsumable)

    def do_make(self, picker, args):
        consumable = spawn({
            "typeclass": self.db.make_class or "typeclasses.consumables.Consumable",
            "key": self.db.make_name,
            "aliases": self.db.make_aliases,
            "desc": routput(self.db.make_desc),
        })[0]
        consumable.db.amount = self.db.make_amount or 3
        consumable.db.eat_amount = self.db.make_eat_amount or 1

        if self.db.make_eat_msg:
            consumable.db.eat_msg = self.db.make_eat_msg
        if self.db.make_eat_msgs:
            consumable.db.eat_msgs = self.db.make_eat_msgs
        if self.db.make_finish_msg:
            consumable.db.finish_msg = self.db.make_finish_msg
        if self.db.make_finish_msgs:
            consumable.db.finish_msgs = self.db.make_finish_msgs

        consumable.location = picker
        picker.msg(f"You {self.db.make_verb or 'get'} {self.db.make_name}.")


# ----------------------------------------------------------------------

class CmdMakeScone(Command):
    """
    Generate a new thing and give it to the caller.
    """
    key = "get scone"
    # aliases = "take scone"

    def func(self):
        self.obj.do_make(self.caller, self.args)


class CmdSetMakeScone(CmdSet):
    def at_cmdset_creation(self):
        self.add(CmdMakeScone)


class Trolley(Producer):
    """
    A special producer that has a plate of specific dishes for
    that day. I need this special because I'm not sure how to populate
    the database with complex arrays of data.

    """
    scones = [
        {
            "name": "blueberry scone",
            "taste": "blueberries",
        },
        {
            "name": "chocolate chip scone",
            "taste": "chocolate",
        },
        {
            "name": "lemon poppy seed scone",
            "taste": "lemon",
        },
        {
            "name": "cinnamon scone",
            "taste": "spice",
        },
        {
            "name": "cranberry orange scone",
            "tastes": ["tartness of the cranberries", "orange"],
        },
        {
            "name": "pumpkin scone",
            "taste": "spices",
        },
        {
            "name": "cheddar cheese and chive scone",
            "expression": "cheesy"
        },
        {
            "name": "cherry almond scone",
            "expression": "nutty",
        },
        {
            "name": "apple cinnamon scone",
            "tastes": ["apple", "cinnamon"],
        },
    ]

    def at_object_creation(self):
        self.cmdset.add_default(CmdSetMakeScone)

    def do_bake(self):
        """
        Set the scone that we bake today.
        """
        todays_scone = self.scones[date.today().day % len(self.scones)]
        name = todays_scone.get("name")

        self.db.make_name =  name
        self.db.make_aliases = ["scone", "scones"]
        self.db.make_desc = routput(f"A [tasty|yummy|delicious]-looking {name}")
        self.db.make_amount = 3
        self.db.make_eat_amount = 1

        msgs = [
            "[Delicious|Mmmm|So good|Really good].",
        ]
        if todays_scone.get('taste'):
            msgs += [f"You [can|] [really|] [notice|taste] the {todays_scone.get('taste')}."]
        if todays_scone.get('tastes'):
            for taste in todays_scone.get('tastes'):
                msgs += [f"You [can|] [really|] [notice|taste] the {taste}."]
        if todays_scone.get('expression'):
            msgs += [f"[Really|So] {todays_scone.get('expression')}."]

        self.db.make_eat_msgs = msgs
        self.db.make_finish_msg = "That was [delicious|great|really good]."
