"""This module provides the ``TrainingTask`` model for the ``trainings`` app"""

from django.db import models
from django.utils.translation import ugettext_lazy as _

from extended_choices import Choices

from mtp.apps.tasks.models import DjangoTask

from ..managers import TrainingTaskManager


TASK_TYPES = Choices(
    ('DUPLICATE_TO_MEMBER', 1, _('Duplicate to member')),
    ('CREATE_DRAFT', 2, _('Create draft')),
)
TASK_TYPES.add_subset('TRAINING_CREATION', ['DUPLICATE_TO_MEMBER', 'CREATE_DRAFT'])


class TrainingTask(DjangoTask):
    """Django model representing Training related task

    This task inherits from ``DjangoTask`` and adds a relation to a
    ``Training``, and the type of task which will be used for filtering/grouping

    Attributes
    ----------
    training : django ``ForeignKey`` to mtp.apps.trainings.models.Training
        Foreign key to the ``Training`` being manipulated.
    task_type: django ``PositiveSmallIntegerField``
        The type of task, from ``TASK_TYPES``.

    """

    TASK_TYPES = TASK_TYPES

    objects = TrainingTaskManager()

    training = models.ForeignKey(
        'trainings.Training',
        verbose_name=_('Training'),
        related_name='tasks',
        on_delete=models.CASCADE
    )
    task_type = models.PositiveSmallIntegerField(
        verbose_name=_('Task type'),
        choices=TASK_TYPES
    )

    created_training = models.OneToOneField(
        'trainings.Training',
        verbose_name=_('Created training'),
        related_name='created_by_task',
        blank=True,
        null=True,
        on_delete=models.CASCADE
    )

    def manage_result(self, result):
        """Save the created training if the task is about creating a training

        Parameters
        ----------
        result: str
            The result returned by the celery task. A string representing the new training id

        """
        super().manage_result(result)

        if self.task_type not in TASK_TYPES.TRAINING_CREATION:
            return

        # The attribute is defined because it's the pk of the ``created_training`` o2o field
        # pylint: disable=attribute-defined-outside-init
        self.created_training_id = result

    def get_finished_notice_for_user(self, user):
        """Return the notice to display in the trainings list for a finished task

        Parameters
        ----------
        user : MTPUser
            The user for which we want the notice

        Returns
        -------
        str
            The computed notice

        """

        notice = None
        if self.user_id == user.pk:
            if self.task_type == self.TASK_TYPES.CREATE_DRAFT:
                notice = _('New version you recently created')
            else:
                # Should not happen, but just in case, because of the "normally" word in
                # Clement, 2016-07-27: "Normally sharing is for others
                notice = _('Recently created by you')
        else:
            notice = _('Recently created by %(user)s') % {
                'user': self.user.get_full_name()
            }

        return notice
