import random

import factory
import wagtail_factories
from django.contrib.auth import get_user_model
from factory import CREATE_STRATEGY
from wagtail.core.rich_text import RichText

from books.factories import TextBlockFactory, ImageUrlBlockFactory, LinkBlockFactory
from core.factories import fake, fake_paragraph
from rooms.models import Comment, Room, RoomEntry, ModuleRoomSlug
from users.models import SchoolClass


class RoomFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Room

    title = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(4, 8)))
    school_class = factory.Iterator(SchoolClass.objects.all())
    appearance = factory.LazyAttribute(lambda x: random.choice(['red', 'green', 'brown']))


block_types = ['text_block', 'image_block']


class RoomEntryFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = RoomEntry

    title = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(8, 12)))
    description = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(20, 30)))
    author = factory.Iterator(get_user_model().objects.all())
    room = factory.SubFactory(RoomFactory)

    contents = wagtail_factories.StreamFieldFactory({
        'text_block': TextBlockFactory,
        'image_url_block': ImageUrlBlockFactory,
        'link_block': LinkBlockFactory
    })

    @classmethod
    def stream_field_magic(cls, kwargs, stream_field_name):
        if stream_field_name in kwargs:
            for idx, resource in enumerate(kwargs[stream_field_name]):
                value = resource['value']
                for jdx, field in enumerate(value):
                    block_type = resource['type']

                    if block_type == 'text_block':
                        kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, field)] = RichText(value[field])

                    if block_type == 'image_url_block':
                        kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, field)] = value[field]

                    if block_type == 'link_block':
                        kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, field)] = value[field]

                    # elif block_type == 'image_block':
                    #     kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, 'image__title')] = fake_paragraph()
                    #     if field == 'path':
                    #         field_value = value[field]
                    #         kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, 'image__file')] = field_value
                    #   # kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'image_block', )] = fake_paragraph()

            del kwargs[stream_field_name]
        else:
            for i in range(0, random.randint(3, 7)):
                block_type = random.choice(block_types)
                if block_type == 'text_block':
                    kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'text_block', 'text')] = RichText(fake_paragraph())

                # elif block_type == 'image_block':
                #     kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'image_block', 'image__title')] = fake_paragraph()

    @classmethod
    def create(cls, **kwargs):
        cls.stream_field_magic(kwargs, 'contents')
        return cls._generate(CREATE_STRATEGY, kwargs)


class ModuleRoomSlugFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = ModuleRoomSlug

    slug = factory.Sequence(lambda n: u'slug-{:d}'.format(n))
    title = factory.Sequence(lambda n: u'Title {:d}'.format(n))


class CommentFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Comment

    text = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(10, 30)))
