#!/usr/bin/env python

from typeclasses.objects import Object
from commands.sittables import CmdSetSit
from utils.scoring import Scores
from utils.word_list import routput


class Sittable(Object):
    multiple = False

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

    def sit_msg(self):
        adjective = self.db.adjective or "on"
        article = self.db.article or "the"
        extra = self.db.extra or ""

        if self.multiple:
            return routput(f"$You() $conj(sit) {adjective} one of {article} {self.key}. {extra}")
        else:
            return routput(f"$You() $conj(sit) {adjective} {article} {self.key}. {extra}")

    def stand_msg(self):
        return f"$You() $conj(stand) up from {self.key}."

    def do_sit(self, sitter):
        """
        Called when trying to sit on/in this object.

        Args:
            sitter (Object): The one trying to sit down.

        """
        adjective = self.db.adjective or "on"
        article = self.db.article or "the"
        current = self.db.sitter

        if sitter.db.is_sitting:
            sitter.msg(f"You are already sitting {adjective} {article} {self.key}.")
            return

        # Clean up an issue that sometimes someone logs off or leaves
        # a room without standing first.
        if self.db.sitter:
            if self.db.sitter not in self.location.contents:
                self.db.sitter = None

        if self.db.sitter and not self.multiple:
            sitter.msg(f"You can't sit {adjective} {article} {self.key} "
                       f"- {current.get_display_name(sitter)} is already sitting there!")
            return

        self.db.sitter = sitter
        sitter.db.is_sitting = self
        sitter.score(Scores.moss_sit)
        if self.db.response:
            sitter.announce_action(self.db.response)
        else:
            sitter.announce_action(self.sit_msg())

        sitter.location.other_sit(sitter)
        for puppet in sitter.puppets_here("other_sit"):
            puppet.other_sit(sitter)

    def do_stand(self, stander):
        """
        Called when trying to stand from this object.

        Args:
            stander (Object): The one trying to stand up.

        """
        adjective = self.db.adjective or "on"
        current = self.db.sitter
        if not stander == current:
            stander.msg(f"You are not sitting {adjective} {self.key}.")
        else:
            self.db.sitter = None
            del stander.db.is_sitting
            stander.announce_action(self.stand_msg())

            stander.location.other_stand(stander)
            for puppet in stander.puppets_here():
                puppet.other_stand(stander)

class Sittables(Sittable):
    multiple = True

    def sit_msg(self):
        aliases = self.aliases.all()

        adjective = self.db.adjective or "on"
        article = self.db.article or "the"
        name = aliases[-1]  # Last alias is singular
        default = f"{article} {name}"
        singular = self.db.singular or default
        extra = self.db.extra or ""

        return routput(f"$You() $conj(sit) {adjective} {singular}. {extra}")

    def stand_msg(self):
        article = self.db.article or "the"
        default = f"{article} {self.db.name}"
        singular = self.db.singular or default
        return f"$You() $conj(stand) up from {singular}."
