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

from django.core.exceptions import (
    ObjectDoesNotExist,
    MultipleObjectsReturned,
)

from mtp.discrete_apps.versionable.managers import (
    DependencyManager,
    DependencyQuerySet,
    PolymorphicManagerForDependency,
    PolymorphicQuerySetForDependency,
)

from mtp.apps.sync.managers import SyncableQuerySet


class DependencyMediaQuerySet(SyncableQuerySet, PolymorphicQuerySetForDependency):
    """QuerySet for the ``mtp.apps.media.models.MediaContent`` model"""

    pass


class DependencyMediaManager(PolymorphicManagerForDependency):
    """Manager for the ``mtp.apps.media.models.Media`` model"""

    pass


class DependencyVideoMediaQuerySet(SyncableQuerySet, PolymorphicQuerySetForDependency):
    """QuerySet for the ``mtp.apps.media.models.VideoMedia`` model"""

    pass


class DependencyVideoMediaManager(PolymorphicManagerForDependency):
    """Manager for the ``mtp.apps.media.models.VideoMedia`` model"""

    pass


class DependencyPdfMediaQuerySet(SyncableQuerySet, PolymorphicQuerySetForDependency):
    """QuerySet for the ``mtp.apps.media.models.PdfMedia`` model"""

    pass


class DependencyPdfMediaManager(PolymorphicManagerForDependency):
    """Manager for the ``mtp.apps.media.models.PdfMedia`` model"""

    pass


class DependencyPresentationMediaQuerySet(DependencyQuerySet, SyncableQuerySet):
    """QuerySet for the ``mtp.apps.media.models.PresentationMedia`` model"""

    def get_media_for_presentation(self, presentation, is_restricted=False):
        """Get the media linked to the provided ``Presentation``

        Parameters
        ----------
        presentation: Presentation
            The presentation for which we want to get the media
        is_restricted: bool
            If True, we will get the restricted media,
            If False, we will get the non-restricted media

        Returns
        -------
        ``Media`` or None

        """

        try:
            return self.get(presentation=presentation, is_restricted=is_restricted).media
        except (ObjectDoesNotExist, MultipleObjectsReturned):
            return None


class DependencyPresentationMediaManager(DependencyManager):
    """Manager for the ``mtp.apps.media.models.PresentationMedia`` model"""

    pass


class NoteQueryset(SyncableQuerySet):
    """Queryset for the ``mtp.apps.media.models.Note`` model"""

    def without_presentation(self):
        """Filter only notes not linked to a ``Presentation``

        Returns
        -------
        NoteQueryset
            A clone of the original queryset (``self``), filtered.

        """

        return self.filter(presentation__isnull=True)

    def for_presentation(self, presentation):
        """Filter only notes linked to the given ``Presentation``

        Parameters
        ----------
        presentation: Presentation
            The presentation we want to want the notes linked to

        Returns
        -------
        NoteQueryset
            A clone of the original queryset (``self``), filtered.

        """

        return self.filter(presentation=presentation)
