#!/usr/bin/env python

from os.path import exists
from datetime import datetime
from random import randint
from re import split, match
from shutil import copyfile

from evennia import CmdSet
from evennia.utils import logger, delay

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


class StoryCube(Object, Listener):
    """
    An invisible puppet to respond to situations.

    @create/drop cube1:typeclasses.puppets.StoryCube
    @desc cube1 = Description for Admins
    @lock cube1 = view:false()  # to make it invisible
    """
    def return_appearance(self, caller):
        return "What?"


class Changling(StoryCube):
    """
    Object that changes appearance every time someone looks at it.

    Important Attribute:
      desc_prefix (string): The initial part of the description.
          This can be an empty string.

      desc_postfix (string): The final part of the description.
          This can be an empty string.

      descs (list): list of descriptions. One of these is
          picked randomly when this object is looked at and its index
          in the list is used as a key for to solve the puzzle.
    """

    def at_object_creation(self):
        """Called when object is created."""
        super().at_object_creation()
        self.db.desc_prefix = ""
        self.db.desc_postfix = ""
        self.db.descs = ["You don't see anything particular."]

    def return_appearance(self, caller):
        """
        This hook is called by the look command to get the description
        of the object. We overload it with our own version.
        """
        def optional(text, prefix=False):
            if text and text != "":
                return ("" if prefix else " ") + \
                    routput(text) + \
                    (" " if prefix else "")
            else:
                return ""

        # randomly get the index for one of the descriptions
        descs = self.db.descs
        clueindex = randint(0, len(descs) - 1)
        # set this description, with the random extra
        self.db.desc = optional(self.db.desc_prefix, True) + \
            descs[clueindex] + optional(self.db.desc_postfix)

        # Might as well remember the last view we got, as it could be
        # a clue we could refer to.
        caller.attributes.add(key=f"{self.key}_view_index",
                              value=clueindex)

        # call the parent function as normal (this will use
        # the new desc Attribute we just set)
        # return super().return_appearance(caller)
        return self.db.desc

    def at_object_receive(self, obj, giver, **kwargs):
        if obj.key == 'conch':
            return super().at_object_receive(obj, giver)

        obj.move_to(self.location, quiet=True, move_type="drop")
        delay(2, giver.announce_action,
              f"Soon after $you() $conj(place) a {obj.name} in the open cavity of the obelisk, it falls to the floor.")
        return True
