from graphql_relay import to_global_id
from wagtail.core.fields import StreamField
from wagtail.tests.utils.form_data import streamfield, nested_form_data, rich_text

from books.factories import ContentBlockFactory, ModuleFactory, ChapterFactory
from books.models import ContentBlock
from core.tests.base_test import SkillboxTestCase

DUPLICATE_CONTENT_BLOCK_MUTATION = """
mutation DuplicateContentBlockMutation($input: DuplicateContentBlockInput!) {
  duplicateContentBlock(input: $input) {
    contentBlock {
      id
    } 
  }
}
"""

CONTENT_BLOCK_QUERY = """
query ContentBlockQuery($slug: String!) {
  module(slug: $slug) {
    chapters {
      id
      contentBlocks {
        id
        title
        type
      }
    }
  }
}
"""


class DuplicateContentBlockTestCase(SkillboxTestCase):
    def setUp(self) -> None:
        self.createDefault()
        #
        self.slug = 'module'
        self.module = ModuleFactory(slug=self.slug)
        self.chapter = ChapterFactory(parent=self.module)
        self.content_block = ContentBlock(
            type=ContentBlock.NORMAL,
            title='Title',
        )
        self.chapter.add_child(instance=self.content_block)

    def test_duplicate_content_block(self):
        result = self.get_client().execute(CONTENT_BLOCK_QUERY, variables={
            'slug': self.slug
        })
        self.assertIsNone(result.errors)
        module = result.data.get('module')
        chapter = module.get('chapters')[0]
        content_blocks = chapter.get('contentBlocks')
        self.assertEqual(len(content_blocks), 1)
        self.assertEqual(ContentBlock.objects.count(), 1)

        result = self.get_client().execute(DUPLICATE_CONTENT_BLOCK_MUTATION, variables={
            'input': {
                "id": to_global_id('ContentBlockNode', self.content_block.id)
            }
        })
        self.assertIsNone(result.errors)
        duplicate_content_block = result.data.get('duplicateContentBlock')
        content_block = duplicate_content_block.get('contentBlock')
        # self.assertEqual(content_block['title'], 'Title')
        self.assertIsNotNone(content_block['id'])

        result = self.get_client().execute(CONTENT_BLOCK_QUERY, variables={
            'slug': self.slug
        })
        self.assertIsNone(result.errors)
        module = result.data.get('module')
        chapter = module.get('chapters')[0]
        content_blocks = chapter.get('contentBlocks')
        self.assertEqual(ContentBlock.objects.count(), 2)
        self.assertEqual(len(content_blocks), 2)
        self.assertTrue('Kopie' in content_blocks[0].get('title'))
        self.assertTrue('Kopie' not in content_blocks[1].get('title'))

    # def test_duplicate_non_editable_contents(self):
    #     # todo: find out how to create contents for tests, then re-enable
    #     # contents__0__text_block__text
    #     nested_form_data({
    #         'content': streamfield([
    #             nested_form_data({
    #                 'text_block': [
    #                     ('text', rich_text('Asdf'))
    #                 ]
    #             })
    #         ])
    #     })
    #
    #     contents = [
    #             nested_form_data({
    #                 'text_block': streamfield([
    #                     ('text', 'Asdf')
    #                 ])
    #             }),
    #         # {
    #         #     "type": "text_block",
    #         #     "value": {
    #         #         "text": "Asdf"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "assignment",
    #         #     "value": {
    #         #         "title": "Ein Auftragstitel",
    #         #         "assignment": "Ein Auftrag",
    #         #     },
    #         # },
    #         # {
    #         #     "type": "image_block",
    #         #     "value": {
    #         #         "path": "/media/original_images/dummy_pZUH02q.jpg"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "image_url_block",
    #         #     "value": {
    #         #         "title": "Asdf",
    #         #         "url": "http://localhost:8000/media/images/dummy_pZUH02q.max-165x165.jpg"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "link_block",
    #         #     "value": {
    #         #         "text": "Asdf",
    #         #         "url": "https://iterativ.ch"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "solution",
    #         #     "value": {
    #         #         "text": "Asdf",
    #         #     },
    #         # },
    #         # {
    #         #     "type": "video_block",
    #         #     "value": {
    #         #         "url": "https://www.youtube.com/watch?v=QxQBWR7sntI"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "document_block",
    #         #     "value": {
    #         #         "url": "http://localhost:8000/media/images/dummy_pZUH02q.max-165x165.jpg"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "infogram_block",
    #         #     "value": {
    #         #         "id": "4405271e-dbfb-407e-ac19-0a238cde393f",
    #         #         "title": "Gerät Internetnutzung Jungen"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "thinglink_block",
    #         #     "value": {
    #         #         "id": "1314204266449076227"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "subtitle",
    #         #     "value": {
    #         #         "text": "Subtitle"
    #         #     },
    #         # },
    #         # {
    #         #     "type": "instruction",
    #         #     "value": {
    #         #         "url": "http://localhost:8000/media/images/dummy_pZUH02q.max-165x165.jpg",
    #         #         "text": "Instruction",
    #         #     },
    #         # },
    #         # {
    #         #     "type": "module_room_slug",
    #         #     "value": {
    #         #         "title": "Raum",
    #         #     },
    #         # },
    #     ]
    #     self.content_block.contents = contents
    #     self.assertTrue(False)
