"""This module contains the views for the ``media`` app"""

from django.utils.translation import ugettext_lazy as _
from django.views import generic

from mtp.apps.common.views_utils import (
    AjaxResponseMixin,
    SyncableDeleteView
)
from mtp.apps.trainings.views.activities import (
    TrainingActivityCreateBase,
    TrainingActivityDetailBase,
    TrainingActivityEditBase,
)

from .forms import PresentationForm, VideoForm
from .models import Presentation, Video


class PresentationBase(TrainingActivityDetailBase):
    """Base view to use on all views related to the presentation."""

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

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

        Returns
        -------
        Queryset of ``mtp.apps.media.models.Presentation``
            A lazy queryset with the wanted presentations.

        """

        return super().get_queryset().instance_of(Presentation)

    def get_success_url(self):
        """Get the url where to go back when an action is done on a ``Presentation``"""

        return self.object.get_detail_url()


class PresentationEditBase(AjaxResponseMixin, PresentationBase):
    """Base view to view, create or update a given instance of ``Presentation``"""

    model = Presentation
    form_class = PresentationForm
    pk_url_kwarg = 'presentation_uuid'


class PresentationDetailView(PresentationEditBase, generic.DetailView):
    """View to see a ``Presentation`` instance"""

    pass


class PresentationUpdateView(PresentationEditBase, TrainingActivityEditBase):
    """View to update a ``Presentation`` instance"""

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


class PresentationCreateView(PresentationEditBase, TrainingActivityCreateBase):
    """View to create a ``Presentation`` instance"""

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


class PresentationDeleteView(PresentationEditBase, SyncableDeleteView):
    """View to delete a ``Presentation`` instance."""

    success_message = _('Your presentation was successfully deleted.')


class VideoBase(TrainingActivityDetailBase):
    """Base view to use on all views related to the video."""

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

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

        Returns
        -------
        Queryset of ``mtp.apps.media.models.Video``
            A lazy queryset with the wanted videos.

        """

        return super().get_queryset().instance_of(Video)

    def get_success_url(self):
        """Get the url where to go back when an action is done on a ``Video``"""

        return self.object.get_detail_url()


class VideoEditBase(AjaxResponseMixin, VideoBase):
    """Base view to view, create or update a given instance of ``Video``"""

    model = Video
    form_class = VideoForm
    pk_url_kwarg = 'video_uuid'


class VideoDetailView(VideoEditBase, generic.DetailView):
    """View to see a ``Video`` instance"""

    pass


class VideoUpdateView(TrainingActivityEditBase, VideoEditBase):
    """View to update a ``Video`` instance"""

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

    def form_valid(self, form):
        """Save the video media

        Parameters
        ----------
        form : instance of ``django.forms.models.ModelForm``
            The validated form to save.

        """

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

        return super().form_valid(form)


class VideoCreateView(TrainingActivityCreateBase, VideoEditBase):
    """View to create a ``Video`` instance"""

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

    def form_valid(self, form):
        """Save the video content

        Parameters
        ----------
        form : instance of ``django.forms.models.ModelForm``
            The validated form to save.

        """

        self.object = form.save(commit=False)
        self.object.media.save()
        self.object.media_id = self.object.media.id

        return super().form_valid(form)


class VideoDeleteView(VideoEditBase, SyncableDeleteView):
    """View to delete a ``Video`` instance."""

    success_message = _('Your video was successfully deleted.')
