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

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

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

from ..forms import BrainstormingForm
from ..models import Brainstorming


class BrainstormingBase(TrainingActivityDetailBase):
    """Base view to use on all views related to the ``Brainstorming`` model.

    Define the queryset to use for the current user.

    """

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

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

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

        """

        return super().get_queryset().instance_of(Brainstorming)

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

        return self.object.get_detail_url()


class BrainstormingEditBase(AjaxResponseMixin, BrainstormingBase):
    """Base view to create or edit a given instance of ``Brainstorming``"""

    model = Brainstorming
    form_class = BrainstormingForm
    pk_url_kwarg = 'brainstorming_uuid'


class BrainstormingDetailView(BrainstormingEditBase, generic.DetailView):
    """Detail View for a ``Brainstorming`` instance"""

    template_name = 'ideas/idea_activity_detail.html'


class BrainstormingCreateView(BrainstormingEditBase, TrainingActivityCreateBase):
    """View to create a new ``Brainstorming`` instance"""

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


class BrainstormingUpdateView(BrainstormingEditBase, TrainingActivityEditBase):
    """View to update an existing ``Brainstorming`` instance"""

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