from unittest import mock

from mayan.apps.documents.events import (
    event_document_created, event_document_file_created,
    event_document_file_edited, event_document_version_created,
    event_document_version_page_created
)
from mayan.apps.documents.models.document_file_models import DocumentFile
from mayan.apps.documents.models.document_models import Document
from mayan.apps.documents.tests.base import GenericDocumentTestCase
from mayan.apps.documents.tests.literals import TEST_FILE_SMALL_PATH

from .literals import TEST_SOURCE_BACKEND_PATH_INTERACTIVE_ACTION
from .mixins.interactive_mixins import InteractiveSourceBackendTestMixin


class SourceBackendCallbackTestCase(
    InteractiveSourceBackendTestMixin, GenericDocumentTestCase
):
    _test_source_backend_path = TEST_SOURCE_BACKEND_PATH_INTERACTIVE_ACTION
    auto_upload_test_document = False

    @mock.patch(
        target='mayan.apps.sources.models.Source.callback_post_document_create'
    )
    def test_callback_post_document_create_on_document_upload(
        self, mocked_source_method
    ):
        document_count = Document.objects.count()

        self._clear_events()

        with open(file=TEST_FILE_SMALL_PATH, mode='rb') as file_object:
            self._execute_test_source_action(
                action_name='document_upload',
                extra_data={'file_object': file_object}
            )

        self.assertEqual(
            Document.objects.count(), document_count + 1
        )

        self.assertEqual(mocked_source_method.call_count, 1)

        events = self._get_test_events()
        self.assertEqual(events.count(), 5)

        test_document = Document.objects.first()
        test_document_file = test_document.file_latest
        test_document_version = test_document.version_active
        test_document_version_page = test_document_version.pages.first()

        self.assertEqual(events[0].action_object, self._test_document_type)
        self.assertEqual(events[0].actor, test_document)
        self.assertEqual(events[0].target, test_document)
        self.assertEqual(events[0].verb, event_document_created.id)

        self.assertEqual(events[1].action_object, test_document)
        self.assertEqual(events[1].actor, test_document_file)
        self.assertEqual(events[1].target, test_document_file)
        self.assertEqual(events[1].verb, event_document_file_created.id)

        self.assertEqual(events[2].action_object, test_document)
        self.assertEqual(events[2].actor, test_document_file)
        self.assertEqual(events[2].target, test_document_file)
        self.assertEqual(events[2].verb, event_document_file_edited.id)

        self.assertEqual(events[3].action_object, test_document)
        self.assertEqual(events[3].actor, test_document_version)
        self.assertEqual(events[3].target, test_document_version)
        self.assertEqual(events[3].verb, event_document_version_created.id)

        self.assertEqual(events[4].action_object, test_document_version)
        self.assertEqual(events[4].actor, test_document_version_page)
        self.assertEqual(events[4].target, test_document_version_page)
        self.assertEqual(
            events[4].verb, event_document_version_page_created.id
        )

    @mock.patch(
        target='mayan.apps.sources.models.Source.callback_post_document_file_upload'
    )
    def test_callback_post_document_file_upload_on_document_upload(
        self, mocked_source_method
    ):
        document_count = Document.objects.count()

        self._clear_events()

        with open(file=TEST_FILE_SMALL_PATH, mode='rb') as file_object:
            self._execute_test_source_action(
                action_name='document_upload',
                extra_data={'file_object': file_object}
            )

        self.assertEqual(
            Document.objects.count(), document_count + 1
        )

        self.assertEqual(mocked_source_method.call_count, 1)

        events = self._get_test_events()
        self.assertEqual(events.count(), 5)

        test_document = Document.objects.first()
        test_document_file = test_document.file_latest
        test_document_version = test_document.version_active
        test_document_version_page = test_document_version.pages.first()

        self.assertEqual(events[0].action_object, self._test_document_type)
        self.assertEqual(events[0].actor, test_document)
        self.assertEqual(events[0].target, test_document)
        self.assertEqual(events[0].verb, event_document_created.id)

        self.assertEqual(events[1].action_object, test_document)
        self.assertEqual(events[1].actor, test_document_file)
        self.assertEqual(events[1].target, test_document_file)
        self.assertEqual(events[1].verb, event_document_file_created.id)

        self.assertEqual(events[2].action_object, test_document)
        self.assertEqual(events[2].actor, test_document_file)
        self.assertEqual(events[2].target, test_document_file)
        self.assertEqual(events[2].verb, event_document_file_edited.id)

        self.assertEqual(events[3].action_object, test_document)
        self.assertEqual(events[3].actor, test_document_version)
        self.assertEqual(events[3].target, test_document_version)
        self.assertEqual(events[3].verb, event_document_version_created.id)

        self.assertEqual(events[4].action_object, test_document_version)
        self.assertEqual(events[4].actor, test_document_version_page)
        self.assertEqual(events[4].target, test_document_version_page)
        self.assertEqual(
            events[4].verb, event_document_version_page_created.id
        )

    @mock.patch(
        target='mayan.apps.sources.models.Source.callback_post_document_file_upload'
    )
    def test_callback_post_document_file_upload_on_document_file_upload(
        self, mocked_source_method
    ):
        self._create_test_document_stub()

        document_count = Document.objects.count()
        document_file_count = DocumentFile.objects.count()

        self._clear_events()

        with open(file=TEST_FILE_SMALL_PATH, mode='rb') as file_object:
            self._execute_test_source_action(
                action_name='document_file_upload',
                extra_data={'file_object': file_object}
            )

        self.assertEqual(
            Document.objects.count(), document_count
        )
        self.assertEqual(
            DocumentFile.objects.count(), document_file_count + 1
        )

        self.assertEqual(mocked_source_method.call_count, 1)

        events = self._get_test_events()
        self.assertEqual(events.count(), 4)

        test_document = Document.objects.first()
        test_document_file = test_document.file_latest
        test_document_version = test_document.version_active
        test_document_version_page = test_document_version.pages.first()

        self.assertEqual(events[0].action_object, test_document)
        self.assertEqual(events[0].actor, test_document_file)
        self.assertEqual(events[0].target, test_document_file)
        self.assertEqual(events[0].verb, event_document_file_created.id)

        self.assertEqual(events[1].action_object, test_document)
        self.assertEqual(events[1].actor, test_document_file)
        self.assertEqual(events[1].target, test_document_file)
        self.assertEqual(events[1].verb, event_document_file_edited.id)

        self.assertEqual(events[2].action_object, test_document)
        self.assertEqual(events[2].actor, test_document_version)
        self.assertEqual(events[2].target, test_document_version)
        self.assertEqual(events[2].verb, event_document_version_created.id)

        self.assertEqual(events[3].action_object, test_document_version)
        self.assertEqual(events[3].actor, test_document_version_page)
        self.assertEqual(events[3].target, test_document_version_page)
        self.assertEqual(
            events[3].verb, event_document_version_page_created.id
        )
