"""This module holds the api-related views of the ``themes`` app"""

from django.http import Http404

from rest_framework.response import Response
from rest_framework.views import APIView

from mtp.apps.trainings.models import Training

from ..models import Theme

from .serializers import ThemeSerializer


class ThemeDetail(APIView):

    def get(self, request, theme_uuid=None, training_uuid=None):  # pylint: disable=unused-argument
        """
        Retrieve a theme instance.

        This view can be used by providing either a ``theme_id`` or a
        ``training_id``. The relevant theme is retrieved and serialized by
        the ``ThemeSerializer`` defined in ``serializers.py``.

        It raises a 404 if no theme is found.

        """
        if theme_uuid is not None:
            try:
                theme = Theme.objects.get(pk=theme_uuid)
            except Theme.DoesNotExist:
                raise Http404

        elif training_uuid is not None:
            try:
                training = Training.objects.get(pk=training_uuid)
                theme = training.get_theme()
            except Training.DoesNotExist:
                raise Http404

        else:
            raise Http404

        if theme is None:
            raise Http404

        serializer = ThemeSerializer(theme)
        return Response(serializer.data)
