"""This module holds the base test cases for the ``media`` app"""

from io import BytesIO

from mtp.apps.common.tests.test_cases import MTPTestCase

from ..factories import (
    PdfMediaFactory,
    PresentationFactory,
    PresentationMediaFactory,
    VideoFactory,
)


class BaseMediaTestCase:
    """Base test case to test the ``media`` app.

    It provides some default test data useful in a lot of tests.

    Attributes
    ----------
    presentation: ``Presentation``
    pdf_media: ``PdfMedia``
    restricted_pdf_media: ``PdfMedia``
    video: ``Video``

    """

    @classmethod
    def setUpTestData(cls):
        """Create some data to use in the ``media`` tests."""

        super().setUpTestData()

        with cls.training.override_version_state(cls.training.VERSION_STATES.DRAFT):

            # Create the presentation and the associated pdf media
            cls.presentation = PresentationFactory(training=cls.training)
            cls.pdf_media = PdfMediaFactory()
            cls.restricted_pdf_media = PdfMediaFactory()
            PresentationMediaFactory(
                presentation=cls.presentation,
                media=cls.pdf_media
            )
            PresentationMediaFactory(
                presentation=cls.presentation,
                media=cls.restricted_pdf_media,
                is_restricted=True
            )

            # Create the video
            cls.video = VideoFactory()

    @staticmethod
    def create_fake_file(name, content=None):
        """Create a "fake file" with the provided name and content

        Parameters
        ----------
        name: str
            The name of the file to create
        content: bytes
            The content of the file

        Returns
        -------
        io.BytesIO

        """

        if content:
            file = BytesIO(content)
        else:
            file = BytesIO(b'somebinarydata')

        file.name = name

        return file


class MediaTestCase(BaseMediaTestCase, MTPTestCase):
    """A test case based on ``BaseMediaTestCase`` and ``MTPTestCase``.

    Should be used in most cases.

    """
