"""This module contains the test cases for the apiv2-related views of the ``documents`` app"""

from mock import patch

from django.core.urlresolvers import reverse

from mtp.apps.api.tests.test_cases import MTPAPITestCase
from mtp.apps.documents.factories import NoteFactory
from mtp.apps.documents.models import Document
from mtp.apps.documents.tests.test_cases import DocumentTestCase


class DocumentViewSetTestCase(DocumentTestCase, MTPAPITestCase):
    """Test case for the ``DocumentViewSet`` ViewSet"""

    def test_document_detail_view_is_working_correctly(self):
        """Test that the document detail view is working correctly"""

        self.login_as_trainer()

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

        self.assertEqual(response.status_code, 200)


class AnnotatedDocumentPdfDownloadViewTestCase(DocumentTestCase, MTPAPITestCase):
    """Test case for the ``AnnotatedDocumentPdfDownloadView`` view"""

    @classmethod
    def setUpTestData(cls):
        """Store the url of the annotated document endpoint"""

        super().setUpTestData()

        cls.url = reverse('apiv2:annotated-document', kwargs={
            'document_uuid': cls.document.id,
            'session_uuid': cls.training_session.id
        })

    def test_view_should_work(self):
        """Test that the view should behave correctly"""

        self.login_as_trainee()

        # The user don't have any notes.
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 404)

        NoteFactory(
            document=self.document,
            session=self.training_session,
            user=self.trainee,
            page=1
        )

        # Still 404, the images from the pdf don't exists.
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 404)

        with patch.object(Document, 'pdf_images_url_list', return_value=['test_1', 'test_2']):
            response = self.client.get(self.url)

            # We got our notes + the images from the document, the view work.
            self.assertEqual(response.status_code, 200)

            # The view context is OK.
            self.assertEqual(response.context['document'], self.document)
            self.assertEqual(response.context['session'], self.training_session)

            notes = self.document.note_set.filter(
                session=self.training_session,
                user=self.trainee
            ).non_deleted()

            images_with_notes = [
                {
                    # Remove the "/media/" from the file path, as the "pdf_images_url_list"
                    # from above return the image paths including the "/media/" part and we
                    # are adding the `MEDIA_ROOT` to the path in the template.
                    'image': '/'.join(image_url.split('/')[2:]),
                    'notes': [
                        note.text
                        for note in notes
                        if note.page == page_index
                        ]
                }
                for page_index, image_url in enumerate(self.document.pdf_images_url_list)
            ]

            self.assertEqual(response.context['images_with_notes'], images_with_notes)
