
import smart_imports

smart_imports.all()


class Writer(object):
    __slots__ = ('type', 'message', 'substitution', 'hero')

    def __init__(self, type, message, substitution, hero):
        self.type = type
        self.message = message
        self.substitution = substitution
        self.hero = hero

    def actor_id(self, actor): return 'quest_%s_actor_%s' % (self.type, actor)

    def name_id(self): return 'quest_%s_name' % (self.type, )

    def action_id(self): return 'quest_%s_action_%s' % (self.type, self.message)

    def journal_id(self): return 'quest_%s_journal_%s' % (self.type, self.message)

    def diary_id(self): return 'quest_%s_diary_%s' % (self.type, self.message)

    def choice_variant_id(self, variant): return 'quest_%s_choice_variant_%s' % (self.type, variant)

    def current_choice_id(self, answer): return 'quest_%s_choice_current_%s' % (self.type, answer)

    def actor(self, actor):
        return self.get_message(self.actor_id(actor))

    def name(self):
        return self.get_message(self.name_id())

    def action(self, ):
        return self.get_message(self.action_id())

    def journal(self, **kwargs):
        return self.get_message_surrogate(self.journal_id(), externals=kwargs)

    def diary(self, **kwargs):
        return self.get_message_surrogate(self.diary_id(), externals=kwargs)

    def choice_variant(self, variant):
        return self.get_message(self.choice_variant_id(variant))

    def current_choice(self, answer):
        return self.get_message(self.current_choice_id(answer))

    def get_message_surrogate(self, type, externals):
        externals.update(self.substitution)

        lexicon_key, externals, restrictions = linguistics_logic.prepair_get_text(type, externals, quiet=True)

        if lexicon_key is None:
            return None

        return heroes_messages.MessageSurrogate.create(key=lexicon_key,
                                                       externals=externals,
                                                       restrictions=restrictions,
                                                       position=self.hero.position.get_description())

    def get_message(self, type_, **kwargs):
        externals = kwargs
        externals.update(self.substitution)

        return linguistics_logic.get_text(type_, externals, quiet=True)


def get_writer(**kwargs):
    return Writer(**kwargs)
