"""This module holds the views related to the ``QuestionWall`` model"""

from django.shortcuts import get_object_or_404
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
from django.views import generic

from mtp.apps.common.views_utils import (
    AjaxResponseMixin,
    SuccessMessageMixin,
)
from mtp.apps.trainings.models import Training
from mtp.apps.trainings.views import TrainingBaseView

from ..models import QuestionWall


class QuestionWallBase(TrainingBaseView):
    """Base view to use on all views related to the ``QuestionWall`` model.

    Define the queryset to use for the current user.

    """

    def get_queryset(self):
        """Get all ``QuestionWall`` instances the current user is allowed to see.

        The user can only see questionwall instance he own.
        Also excludes deleted questionwall from the result.

        Returns
        -------
        Queryset of ``mtp.apps.ideas.models.QuestionWall``
            A lazy queryset with the wanted questionwalls.

        """

        return QuestionWall.objects.non_deleted().filter(
            training__in=self.get_training_queryset_for_current_user()
        )


class QuestionWallEditBase(AjaxResponseMixin, SuccessMessageMixin, QuestionWallBase):
    """Base view to create or edit a given instance of ``QuestionWall``"""

    model = QuestionWall
    fields = ['name', 'description', 'is_anonymous']
    pk_url_kwarg = 'questionwall_uuid'

    def get_success_url(self):
        """Redirect the user to the list of activity of the tied ``Training``.

        Returns
        -------
        str

        """

        return self.object.training.get_activity_list_url()


class QuestionWallCreateView(QuestionWallEditBase, generic.CreateView):
    """View to create a new ``QuestionWall`` instance"""

    success_message = _('Your Questionwall was successfully created.')

    @cached_property
    def training(self):
        """Returns the current training based on the ``training_uuid`` argument in the url.

        Raises
        ------
        ``django.http.Http404``
            If the training doesn't exists.

        """

        return get_object_or_404(
            Training.objects.non_deleted().filter(owner=self.request.user),
            pk=self.kwargs['training_uuid']
        )

    def get_context_data(self, **kwargs):
        """Add ``training`` to the context.

        For the parameters, see ``django.forms.generic.base.ContextMixin``

        """

        return super().get_context_data(training=self.training, **kwargs)

    def form_valid(self, form):
        """Set the ``training`` and save the questionwall

        Parameters
        ----------
        For the parameters, see ``django.forms.generic.CreateView``

        """

        self.object = form.save(commit=False)
        self.object.training = self.training
        self.object.save()

        return super().form_valid(form)


class QuestionWallUpdateView(QuestionWallEditBase, generic.UpdateView):
    """View to update an existing ``QuestionWall`` instance"""

    success_message = _('Your Questionwall was successfully updated.')
