"""this module contains the test cases for the apiv2 related views of the ``media`` app"""

from django.core.urlresolvers import reverse

from mtp.apps.api.tests.test_cases import MTPAPITestCase
from mtp.apps.media.factories import NoteFactory
from mtp.apps.media.models import Note
from mtp.apps.media.tests.test_cases import BaseMediaTestCase, MediaTestCase
from mtp.apps.trainings.factories import TrainingSessionFactory


class PresentationViewSetTestCase(MediaTestCase):
    """Test case for the ``PresentationViewSet`` ViewSet"""

    def setUp(self):
        """Login as a trainer"""

        self.login_as_trainer()

    def test_presentation_detail_view_is_working_correctly(self):
        """Test that the presentation detail view is working correctly"""

        response = self.client.get(
            reverse('apiv2:presentation-detail', kwargs={'pk': self.presentation.id})
        )

        self.assertEqual(response.status_code, 200)

    def test_presentation_list_view_is_working_correctly(self):
        """Test that the presentation list view is working correctly"""

        response = self.client.get(reverse('apiv2:presentation-list'))
        self.assertEqual(response.status_code, 200)


class VideoViewSetTestCase(MediaTestCase):
    """Test case for the ``VideoViewSet`` ViewSet"""

    def setUp(self):
        """Login as a trainer"""

        self.login_as_trainer()

    def test_video_detail_view_is_working_correctly(self):
        """Test that the video detail view is working correctly"""

        response = self.client.get(
            reverse('apiv2:video-detail', kwargs={'pk': self.video.id})
        )

        self.assertEqual(response.status_code, 200)

    def test_video_list_view_is_working_correctly(self):
        """Test that the video list view is working correctly"""

        response = self.client.get(reverse('apiv2:video-list'))
        self.assertEqual(response.status_code, 200)


class NoteViewSetTestCase(BaseMediaTestCase, MTPAPITestCase):
    """Test case for the ``NoteViewSet`` ViewSet"""

    @classmethod
    def setUpTestData(cls):
        """Create some notes"""

        super().setUpTestData()

        cls.trainer_note = NoteFactory(
            user=cls.trainer,
            training_session=cls.training_session,
        )

        NoteFactory(
            user=cls.trainee,
            training_session=cls.training_session,
        )

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

        NoteFactory(
            user=cls.trainee,
            training_session=TrainingSessionFactory(),
        )

    def test_freenote_detail_view_is_working_correctly(self):
        """Test that the freenote detail view is working correctly"""

        detail_url = reverse('apiv2:freenote-detail', kwargs={'pk': self.trainer_note.id})

        self.login_as_trainer()

        response = self.client.get(detail_url)

        self.assertEqual(response.status_code, 200)

    def test_freenote_list_view_is_working_correctly(self):
        """Test that the freenote list view is working correctly"""

        list_url = reverse('apiv2:freenote-list')

        self.login_as_trainer()

        response = self.client.get(list_url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)  # The trainer got one free note

        self.login_as_trainee()

        response = self.client.get(list_url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 2)  # The trainee got two notes

        # We should be able to filter the notes by session
        response = self.client.get(
            '%s?training_session=%s' % (
                list_url,
                self.training_session.id
            )
        )

        # The trainee got one note associated with the session
        self.assertEqual(len(response.data), 1)

    def test_freenote_create_view_is_working_correctly(self):
        """Test that the freenote create view is working correctly"""

        create_url = reverse('apiv2:freenote-list')

        self.login_as_trainer()

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

        response = self.client.post(create_url, data=note_data)

        self.assertEqual(response.status_code, 201)

        # Our note was successfully created
        note = Note.objects.get(content='Note content')

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

    def test_freenote_update_view_should_work(self):
        """Test that the freenote update view is working correctly"""

        detail_url = reverse('apiv2:freenote-detail', kwargs={'pk': self.trainer_note.id})

        old_note_content = self.trainer_note.content

        self.login_as_trainer()

        note_data = {
            'content': 'New note content',
            'user': self.trainer.id,
            'training_session': self.training_session.id,
        }

        response = self.client.patch(
            detail_url,
            note_data,
            format='json'
        )

        self.assertEqual(response.status_code, 200)

        self.trainer_note.refresh_from_db()

        self.assertNotEqual(self.trainer_note.content, old_note_content)
        self.assertEqual(self.trainer_note.content, 'New note content')

    def test_presentation_note_list_view_should_filter_the_notes_by_presentation(self):
        """Test that the presentation note list view should filter the note by presentation"""

        list_url = reverse(
            'apiv2:presentationnote-list',
            kwargs={'presentation_uuid': self.presentation.pk}
        )

        self.login_as_trainer()

        # The trainer don't have nay notes linked to the current presentation
        response = self.client.get(list_url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

        self.login_as_trainee()

        # The trainee got one note linked to this presentation
        response = self.client.get(list_url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
