"""
Characters

Characters are (by default) Objects setup to be puppeted by Accounts.
They are what you "see" in game. The Character class in this module
is setup to be the "default" character type created by the default
creation commands.

"""

from evennia.objects.objects import DefaultCharacter
from evennia.prototypes.spawner import spawn

from utils.word_list import Token, routput
from .objects import Object


class Character(Object, DefaultCharacter):
    """
    The Character just re-implements some of the Object's methods and hooks
    to represent a Character entity in-game.

    See mygame/typeclasses/objects.py for a list of
    properties and methods available on all Object child classes like this.

    """
    def at_object_creation(self):
        "called when a character is first created."
        if self.dbref != "#1":
            self.create_letter()

    def at_post_puppet(self):
        if self.db.visited:
            self.msg(f"""

You hear a whisper on the breeze, “Welcome back, {self.key.capitalize()}.”

            """)
        else:
            self.db.visited = True
            self.msg("""

As the surrounding mists dissipate, you find yourself in an ancient, halcyon forest dripping with moss.


You see an envelope of parchment wedged under a scaly protrusion of bark...inside, a letter in familiar penmanship, personally addressed to you, which you pick up.

(Type 'inventory' or 'inv' or just 'i' to see what you carry).""")

    def create_letter(self):
        "create a welcome letter in a character's inventory"
        letter = spawn({
            "typeclass": "typeclasses.readables.Letter",
            "key": "letter",
            "desc": "A letter of familiar penmanship stuffed in an envelope.",
        })[0]
        letter.db.inside = f"""You read a letter with an oddly familiar penmanship:

  My dearest {self.name.capitalize()},

  If you are reading this, you've found the world I was overly excited in relaying to you over drinks in Marsivan. Most excellent. Enjoy this halcyon world, unspoiled and idyllic.

  I'm here, so join me in a cup of tea and we can reconnect and reminisce of glorious days gone by, and the utter curiosity that surrounds us.

  Your friend,
  Dabbler

(Type 'help me start' for details on playing this game)
        """
        letter.location = self

    def do_take(self, args):
        """
        A character has a _steal_command. What are the limitations?
        """
        args = Token(args)
        if args.empty():
            self.msg("What do you want to take?")
        elif len(args.words) == 1:
            self.msg(f"You want to take {args.words[0]}, but from whom?")
        else:
            to_take = args.words[0]
            from_whom = args.words[-1]
            victim = self.search(from_whom)
            if victim:
                thing = victim.has(to_take)
                if thing:
                    self.msg(f"You take {thing.key} from {victim.key}.")
                    self.location.msg_contents(
                        f"{self.key} takes {thing.key} from {victim.key}!",
                        exclude=self)
                    thing.move_to(self, quiet=True, use_destination=True)
                    return

                self.msg(f"{victim.key} doesn't have a {to_take}.")
            else:
                self.msg(f"You don't see {from_whom}.")

    def at_pre_move(self, destination, **kwargs):
       """
       Called by self.move_to when trying to move somewhere. If this returns
       False, the move is immediately cancelled.
       """
       if self.db.is_sitting:
           self.msg("You stand up first...")
           self.db.is_sitting = False;
       return True
