"""This module contains the apiv2 related serializers for the ``media`` app"""

from rest_framework import serializers

from mtp.apps.trainings.apiv2.serializers import TrainingActivitySerializer

from ..models import (
    Note,
    Presentation,
    Video,
)


class PresentationSerializer(TrainingActivitySerializer):
    """Serializer for the ``Presentation`` model"""

    pdf = serializers.SerializerMethodField()
    restricted_pdf = serializers.SerializerMethodField()
    pdf_images = serializers.SerializerMethodField()
    restricted_pdf_images = serializers.SerializerMethodField()

    class Meta:
        model = Presentation
        fields = TrainingActivitySerializer.Meta.fields + (
            'pdf',
            'pdf_images',
            'restricted_pdf',
            'restricted_pdf_images',
        )

    @staticmethod
    def get_pdf(instance):
        """Get the file url of the tied "pdf" instance

        Parameters
        ----------
        For the parameters, see the ``serializers.ModelSerializer`` class.

        Returns
        -------
        str

        """

        return instance.pdf.file.url

    @staticmethod
    def get_pdf_images(instance):
        """Get the images associated with the ``pdf`` file

        Parameters
        ----------
        For the parameters, see the ``serializers.ModelSerializer`` class.

        Returns
        -------
        list(str)

        """

        return instance.pdf.images_url_list

    @staticmethod
    def get_restricted_pdf(instance):
        """Get the file url of the tied "restricted_pdf" instance

        Parameters
        ----------
        For the parameters, see the ``serializers.ModelSerializer`` class.

        Returns
        -------
        str or None

        """

        if instance.restricted_pdf:
            return instance.restricted_pdf.file.url
        else:
            return None

    @staticmethod
    def get_restricted_pdf_images(instance):
        """Get the images associated with the ``restricted_pdf`` file

        Parameters
        ----------
        For the parameters, see the ``serializers.ModelSerializer`` class.

        Returns
        -------
        list(str)

        """

        if instance.restricted_pdf:
            return instance.restricted_pdf.images_url_list
        else:
            return []


class VideoSerializer(TrainingActivitySerializer):
    """Serializer for the ``Video`` model"""

    file = serializers.SerializerMethodField()

    class Meta:
        model = Video
        fields = TrainingActivitySerializer.Meta.fields + (
            'file',
        )

    @staticmethod
    def get_file(instance):
        """Get the file url of the tied ``VideoMedia`` instance

        Parameters
        ----------
        For the parameters, see the ``serializers.ModelSerializer`` class.

        Returns
        -------
        str

        """

        return instance.media.file.url


class NoteSerializer(serializers.ModelSerializer):
    """Serializer for the ``Note`` model"""

    presentation = serializers.PrimaryKeyRelatedField(
        queryset=Presentation.objects.non_deleted(),
        default=None
    )
    presentation_page = serializers.IntegerField(default=None)

    class Meta:
        model = Note
        fields = (
            'id',
            'content',
            'user',
            'training_session',
            'presentation',
            'presentation_page',
        )
