"""This module holds the test for the apiv2 related serializers of the ``media`` app"""

from mtp.apps.media.tests.test_cases import MediaTestCase

from mtp.apps.media.factories import NoteFactory
from mtp.apps.media.models import Note, PresentationMedia

from ..serializers import (
    NoteSerializer,
    PresentationSerializer,
    VideoSerializer,
)


class MediaSerializerTestCase(MediaTestCase):
    """Test case for the serializer of the ``media`` app"""

    def test_presentation_serializer_should_work(self):
        """Test that the ``PresentationSerializer`` should work correctly"""

        serializer = PresentationSerializer(self.presentation)

        self.assertDictEqual(
            serializer.data,
            {
                'id': self.presentation.id,
                'name': self.presentation.name,
                'description': self.presentation.description,
                'activity_type': self.presentation.activity_type,
                'sort_order': self.presentation.sort_order,
                'session': self.presentation.session_id,
                'training': self.presentation.training_id,
                'pdf': self.presentation.pdf.file.url,
                'pdf_images': self.presentation.pdf.images_url_list,
                'restricted_pdf': self.presentation.restricted_pdf.file.url,
                'restricted_pdf_images': self.presentation.restricted_pdf.images_url_list
            }
        )

        # The serializer should also work on a presentation without restricted pdf
        PresentationMedia.objects.get(presentation=self.presentation, is_restricted=True).delete()

        serializer = PresentationSerializer(self.presentation)

        self.assertDictEqual(
            serializer.data,
            {
                'id': self.presentation.id,
                'name': self.presentation.name,
                'description': self.presentation.description,
                'activity_type': self.presentation.activity_type,
                'sort_order': self.presentation.sort_order,
                'session': self.presentation.session_id,
                'training': self.presentation.training_id,
                'pdf': self.presentation.pdf.file.url,
                'pdf_images': self.presentation.pdf.images_url_list,
                'restricted_pdf': None,
                'restricted_pdf_images': []
            }
        )


    def test_video_serializer_should_work(self):
        """Test that the ``VideoSerializer`` should work correctly"""

        serializer = VideoSerializer(self.video)

        self.assertDictEqual(
            serializer.data,
            {
                'id': self.video.id,
                'name': self.video.name,
                'description': self.video.description,
                'activity_type': self.video.activity_type,
                'sort_order': self.video.sort_order,
                'session': self.video.session_id,
                'training': self.video.training_id,
                'file': self.video.media.file.url
            }
        )

    def test_note_serializer_from_instance_should_work(self):
        """Test that the ``NoteSerializer`` from an instance should work"""

        note = NoteFactory(
            user=self.trainee,
            training_session=self.training_session,
            presentation=self.presentation,
            presentation_page=1
        )

        serializer = NoteSerializer(note)

        self.assertDictEqual(
            serializer.data,
            {
                'id': note.id,
                'content': note.content,
                'user': self.trainee.id,
                'training_session': self.training_session.id,
                'presentation': self.presentation.id,
                'presentation_page': 1
            }
        )

    def test_note_serializer_from_data_with_presentation_should_work(self):
        """Test that the ``NoteSerializer`` from data with presentation should work"""

        note_data = {
            'content': 'Note content',
            'user': self.trainee.id,
            'training_session': self.training_session.id,
            'presentation': self.presentation.id,
            'presentation_page': 1
        }

        serializer = NoteSerializer(data=note_data)

        self.assertTrue(serializer.is_valid())

        serializer.save()

        note = Note.objects.get(content='Note content')

        self.assertEqual(note.user, self.trainee)
        self.assertEqual(note.training_session, self.training_session)
        self.assertEqual(note.presentation, self.presentation)
        self.assertEqual(note.presentation_page, 1)

    def test_note_serializer_from_data_should_work(self):
        """Test that the ``NoteSerializer`` from data should work"""

        note_data = {
            'content': 'Note content',
            'user': self.trainee.id,
            'training_session': self.training_session.id,
        }

        serializer = NoteSerializer(data=note_data)

        self.assertTrue(serializer.is_valid())

        serializer.save()

        note = Note.objects.get(content='Note content')

        self.assertEqual(note.user, self.trainee)
        self.assertEqual(note.training_session, self.training_session)

