"""
This model translates default strings into localized strings.
"""

from evennia.utils import logger
from django.apps import apps
from django.conf import settings
from muddery.server.mappings.element_set import ELEMENT
from muddery.worldeditor.dao import general_query_mapper as q


class CommonMapper(object):
    """
    Common data mapper.
    """
    def __init__(self, model_name):
        self.model_name = model_name
        self.model = apps.get_model(settings.WORLD_DATA_APP, model_name)
        self.objects = self.model.objects

    def all(self):
        return self.objects.all()

    def get(self, *args, **kwargs):
        return self.objects.get(*args, **kwargs)

    def filter(self, *args, **kwargs):
        return self.objects.filter(*args, **kwargs)


class ElementsMapper(CommonMapper):
    """
    Object data's mapper.
    """
    def __init__(self, element_type):
        element_class = ELEMENT(element_type)
        super(ElementsMapper, self).__init__(element_class.model_name)

        self.base_model_name = element_class.get_base_model()

    def all_with_base(self):
        """
        Get all records with its base data.
        """
        if self.base_model_name == self.model_name:
            return q.get_all_from_tables([self.model_name])
        else:
            return q.get_all_from_tables([self.base_model_name, self.model_name])

    def get_by_key_with_base(self, key):
        """
        Get a record with its base data.

        Args:
            key: (string) object's key.
        """
        if self.base_model_name == self.model_name:
            return q.get_tables_record_by_key([self.model_name], key)
        else:
            return q.get_tables_record_by_key([self.base_model_name, self.model_name], key)
