""" This module holds the models of the ``translation`` app. """

from django.db import models
from django.utils.translation import ugettext_lazy as _
from extended_choices import OrderedChoices
from mtp.apps.sync.models import Syncable

from . import model_fields as translation_fields
from .managers import TranslatableLanguageManager, TranslatableLanguageQuerySet


class TranslatableLanguage(Syncable):
    """ Collection of available language to translation.
    We use only TranslatableLanguage instance to set interface language and manage
    dashboard translation.
    A language is only displayed for a user if its registered in TranslatableLanguage.
    We can hide a language with ``enabled`` attribute.

    Attributes
    ----------
    language : mtp.apps.translation ``LanguageField``
        The registered language.
    enabled : django ``BooleanField``
        The boolean to activate or desactivate this language.
    last_modified : django ``DateTimeField``
        The date/time when language has been added.
    """

    language = translation_fields.LanguageField(
        verbose_name=_('language'),
        unique=True,
        help_text=_('The registered language.'))
    enabled = models.BooleanField(
        default=True,
        help_text=_('The boolean to activate or desactivate this language.'))
    last_modified = models.DateTimeField(
        auto_now=True,
        verbose_name=_('last modified'),
        help_text=_('The date/time when language has been added.'))

    objects = TranslatableLanguageManager.from_queryset(TranslatableLanguageQuerySet)()

    class Meta:
        ordering = ['language']

    def __str__(self):
        return self.get_language_display()


class Translation(Syncable):
    """ Allows to keep a trace on imported translations.
    So, we can stock translation progression in a domain.
    A domain is a translation category: an area on which the translation takes place.

    Attributes
    ----------
    language : mtp.apps.translation ``LanguageField``
        The translation language.
    domain : django ``CharField``
        The domain where translation takes place.
    progression : django ``PositiveSmallIntegerField``
        The translation progression.
    translated_at : django ``DateTimeField``
        The date/time when translation has been added.
    """

    DOMAIN_CHOICES = OrderedChoices(
        ('INTERFACE', 'int', 'interface'),
        ('ASSESSMENT', 'ass', 'assessment'),
        ('SURVEY', 'sur', 'survey'),
    )

    language = translation_fields.LanguageField(
        verbose_name=_('language'),
        help_text=_('The translation language.'))
    domain = models.CharField(
        max_length=3,
        choices=sorted(DOMAIN_CHOICES, key=lambda x: x[1]),
        verbose_name=_('domain'),
        help_text=_('The domain where translation takes place.'))
    progression = models.PositiveSmallIntegerField(
        default=0,
        blank=True,
        verbose_name=_('progression'),
        help_text=_('The translation progression.'))
    last_modified = models.DateTimeField(
        auto_now=True,
        verbose_name=_('last modified'),
        help_text=_('The date/time when translation has been added.'))

    class Meta:
        unique_together = ('language', 'domain')

    def __str__(self):
        return "%s - %s" % (self.domain, self.get_language_display())
