from django.test import TestCase, RequestFactory
from graphene.test import Client
from graphql_relay import to_global_id

from api.schema import schema
from api.utils import get_object
from books.models import ContentBlock, Chapter
from books.factories import ModuleFactory
from core.factories import UserFactory


class NewContentBlockMutationTest(TestCase):
    def setUp(self):
        module = ModuleFactory()
        chapter = Chapter(title='Hello')
        module.add_child(instance=chapter)
        user = UserFactory(username='aschi')
        content_block = ContentBlock(title='bla', slug='bla')
        chapter.specific.add_child(instance=content_block)

        request = RequestFactory().get('/')
        request.user = user
        self.client = Client(schema=schema, context_value=request)

        self.sibling_id = to_global_id('ContentBlockNode', content_block.pk)

    def test_add_new_content_block(self):
        self.assertEqual(ContentBlock.objects.count(), 1)

        mutation = """
            mutation AddContentBlock($input: AddContentBlockInput!) {
              addContentBlock(input: $input) {
                newContentBlock {
                  id
                  title
                  slug
                  contents
                  type
                }
                errors
                clientMutationId  
            }
        }
        """

        title = "Hello World"

        result = self.client.execute(mutation, variables={
            'input': {
                "contentBlock": {
                    "title": title,
                    "contents": [
                        {
                            "type": "text_block",
                            "value": {
                                "text": "Hello there"
                            }
                        }
                    ]
                },
                "after": self.sibling_id
            }
        })
        self.assertIsNone(result.get('errors'))
        self.assertEqual(ContentBlock.objects.count(), 2)

        new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
        self.assertEqual(new_content_block.get('title'), title)
        self.assertEqual(len(new_content_block['contents']), 1)

        id = new_content_block.get('id')
        content_block_page = get_object(ContentBlock, id)

        self.assertEqual(content_block_page.title, title)

    def test_addNewContentBlock_withImageUrlBlock(self):
        self.assertEqual(ContentBlock.objects.count(), 1)

        mutation = """
            mutation AddContentBlock($input: AddContentBlockInput!) {
              addContentBlock(input: $input) {
                newContentBlock {
                  id
                  title
                  slug
                  contents
                  type
                }
                errors
                clientMutationId  
            }
        }
        """

        title = "Hello World"

        result = self.client.execute(mutation, variables={
            'input': {
                "contentBlock": {
                    "title": title,
                    "type": "NORMAL",
                    "contents": [
                        {
                            "type": "image_url_block",
                            'value': {
                                "url": "/test.png"
                            }
                        }
                    ]
                },
                "after": self.sibling_id
            }
        })
        self.assertIsNone(result.get('errors'))
        self.assertEqual(ContentBlock.objects.count(), 2)

        new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
        self.assertEqual(new_content_block.get('title'), title)
        self.assertEqual(len(new_content_block['contents']), 1)
        content = new_content_block['contents'][0]
        self.assertEqual(content.get('type'), 'image_url_block')
        self.assertEqual(content.get('value'), {'url': '/test.png'})
