# Generated by Django 3.2.7 on 2021-09-14 03:53
import random

import lorem
from django.conf import settings
from django.core.files import File
from django.db import migrations


def generate_data(apps, schema_editor):
    Page = apps.get_model("main", "Page")
    PageBlock = apps.get_model("main", "PageBlock")
    VideoContent = apps.get_model("main", "VideoContent")
    AudioContent = apps.get_model("main", "AudioContent")
    TextContent = apps.get_model("main", "TextContent")

    contents = [VideoContent, AudioContent, TextContent]
    files_path = settings.BASE_DIR
    video_file = open(files_path / "main/fixtures/test_video.mp4", "rb")
    subtitle_file = open(files_path / "main/fixtures/test_subtitle.srt", "rb")
    audio_file = open(files_path / "main/fixtures/test_audio.mp3", "rb")
    for i in range(50):
        page = Page.objects.create(title=lorem.sentence())

        for j in range(random.randint(1, 10)):
            content_model = random.choice(contents)
            if content_model == VideoContent:
                content = content_model(
                    title=lorem.sentence(),
                )
                content.video.save("test_video.mp3", File(video_file))
                content.subtitle.save("test_subtitle.mp3", File(subtitle_file))
                content.save()
                PageBlock.objects.create(page=page, content_video=content)
            elif content_model == AudioContent:
                content = content_model(
                    title=lorem.sentence(), bitrate=random.randint(100, 1000)
                )
                content.audio.save("test_audio.mp3", File(audio_file))
                content.save()
                PageBlock.objects.create(page=page, content_audio=content)
            elif content_model == TextContent:
                content = content_model.objects.create(
                    title=lorem.sentence(), text=lorem.text()
                )
                PageBlock.objects.create(page=page, content_text=content)
    audio_file.close()
    video_file.close()
    subtitle_file.close()


class Migration(migrations.Migration):
    dependencies = [
        ("main", "0001_initial"),
    ]

    operations = [migrations.RunPython(generate_data)]
