#!/usr/bin/env python

from re import split, match, sub, IGNORECASE

from evennia import CmdSet
from evennia.utils import logger

from commands.command import Command
from typeclasses.characters import Character
from typeclasses.objects import Listener, Recorder
from utils.word_list import routput


class Puppet(Character, Listener):
    """
    Special character that if not puppetable, stays put.
    Perhaps responding or logging information to the GM.
    """
    def at_post_puppet(self, **kwargs):
        """
        Called just after puppeting has been completed and all
        Account<->Object links have been established.

        Args:
            **kwargs (dict): Arbitrary, optional arguments for users
                overriding the call (unused by default).
        Notes:

            You can use `self.account` and `self.sessions.get()` to get
            account and sessions at this point; the last entry in the
            list from `self.sessions.get()` is the latest Session
            puppeting this Object.
        """
        self.msg(f"\nYou are puppeting |c{self.key}|n.")
        self.execute_cmd("pose reset")
        self.msg((self.at_look(self.location), {"type": "look"}), options=None)

    def at_pre_unpuppet(self, **kwargs):
        """
        Set our 'sleeping' pose.
        """
        super().at_pre_unpuppet()
        sleep_pose = self.attributes.get("pose_sleep")
        if sleep_pose:
            self.execute_cmd(f"pose {sleep_pose}")

    def at_post_unpuppet(self, account=None, session=None, **kwargs):
        """
        Make the character object remain in the room.

        Args:
            account (DefaultAccount): The account object that just disconnected
                from this object.
            session (Session): Session controlling the connection that
                just disconnected.
        Keyword Args:
            reason (str): If given, adds a reason for the unpuppet. This
                is set when the user is auto-unpuppeted due to being link-dead.
            **kwargs: Arbitrary, optional arguments for users
                overriding the call (unused by default).

        """
        self.msg(f"\nNo longer puppeting |c{self.key}|n.\n")

    def get_display_desc(self, _, **kwargs):
        """
        Return one of two appearances based on if it is being puppeted or not.

        Set either or both:

        @set me/desc_puppeted = "A clown bouncing up and down from a box."
        @set me/desc_uppuppeted = "A colorful box with a closed lid."

        If either is not specified, it uses the standard description.
        """
        if self.tags.get(key='puppeted', category='account'):
            return self.db.desc_puppeted if self.db.desc_puppeted else self.db.desc
        else:
            return self.db.desc_unpuppeted if self.db.desc_unpuppeted else self.db.desc


class CmdShrubSay(Command):
    """Erase and write on the shrub's chalkboard.
    """
    key = "say"
    aliases = ["write"]

    def func(self):
        self.obj.write(self.args.strip())


class CmdSetShrubSay(CmdSet):
    """
    The shrub's version of the 'say' command.
    """
    def at_cmdset_creation(self):
        super().at_cmdset_creation()
        self.add(CmdShrubSay)


class Shrub(Puppet, Recorder):
    """
    The 'Shrub' has its own way of communicating.
    """
    def at_object_creation(self):
        "Called when a character is first created."
        self.cmdset.add(CmdSetShrubSay, persistent=True)

    def intro(self):
        self.record_msg("After tuning their instruments, a quartet of pixies start their first number. \"Quiet evening, eh Mushy?\" asks the elf bartender to a stout myconid. It makes a vaguely <i>shrugging</i> motion with a pair of pseudopods it uses to manipulate its environment. It knew the question was rhetorical, as everyone knew the portal was opening, and that always brought characters.")
        self.execute_cmd("look")
        self.record_msg("The bartender says, \"Fetch that bowl of candies for bar, please, Mushy.\"")
        self.execute_cmd("recog elf as bartender")
        self.note("Not sure how I landed in this bar, but here I am. Don't get me wrong, I enjoy it. The bartender, a fairly haughty elf originally from the <i>Mud World</i>, always supplies me with a nice glass of water.")
        self.execute_cmd("look elf")
        self.record_msg("The mushroom man returns, balancing the bowl on its cap. \"One more thing,\" the bartender says, \"I had a box of oddly shaped, orange crackers. I have a feeling they may come in useful.\"")
        self.note("Helping to keep the place respectable, <i>Mushy</i> also gathers empty cocktail glasses. One of the <i>fungus ones</i>, or mushroom people, it too has always been kind to me.")
        # self.execute_cmd("look mushroom man")
        self.record_msg("\"Any requests?\" asks Lemon of the bartender.")
        self.note("Lemon, Cart, Hairy and Ring, this pixie quartet can't decided on a name for their group; currently calling themselves, the <i>Pixie Stones</i>, provide the entertainment.")
        self.execute_cmd("look pixies")
        self.record_msg("The bartender replies, \"Nah, just remember to play that weird <i>juzz</i> music when the boss arrives later.\"")
        self.note("And here I am, an <i>Awakened Shrub</i>, joting down the events that traspire in this wyld bar somewhere in the <i>Domain of Moss</i>, out of reach from both the Seelie and the Unseelie courts. Of course, that may change.")
        self.note("Now then, let's see what characters may arrive...")
        self.record_msg("<hr/>")

    def note(self, text):
        """
        Record in the transcript log, a special note.
        """
        self.record_msg((text, {'type': 'note'}))

    def write(self, new_text):
        """
        Change the readable message on the chalkboard.

        This will also record the new message in the log.
        """
        here = self.location
        msg = routput("The shrub uses a branch to << brush ^ wipe >> << off ^ >> << the chalk from ^ >> its << small ^ >> chalkboard, and with another branch, << slowly ^ carefully ^ deliberately ^ >> starts to write << something ^ a message ^ >>.")
        here.msg_contents(msg)
        self.db.inside = f"The chalkboard reads: |w{new_text}|n"
        self.record_msg(self.db.inside)

    def msg(self, text=None, from_obj=None, session=None, **kwargs):
        """
        Record everything that happens in the room for a transcript.

        Some key events may trigger more actions to get more information.
        """
        self.record_msg(text, from_obj)
        super().msg(text, from_obj, session)
