# Generated by Django 2.2.3 on 2019-08-09 15:57
import os
import shutil
from django.db import migrations
from django.conf import settings
from django.core.files import File

def convert_old_models(apps, schema_editor):
    DocumentTemplate = apps.get_model('document', 'DocumentTemplate')
    DocumentStyle = apps.get_model('style', 'DocumentStyle')
    DocumentStyleFile = apps.get_model('style', 'DocumentStyleFile')
    ExportTemplate = apps.get_model('style', 'ExportTemplate')
    if not os.path.exists(
        os.path.join(
            settings.MEDIA_ROOT,
            "style-files"
        )
    ):
        os.makedirs(
            os.path.join(
                settings.MEDIA_ROOT,
                "style-files"
            )
        )
    if not os.path.exists(
        os.path.join(
            settings.MEDIA_ROOT,
            "export-template-files"
        )
    ):
        os.makedirs(
            os.path.join(
                settings.MEDIA_ROOT,
                "export-template-files"
            )
        )
    for dt in DocumentTemplate.objects.all():
        for old_ds in dt.document_styles.all():
            doc_style = DocumentStyle()
            doc_style.title = old_ds.title
            slug = old_ds.filename
            existing_ds = DocumentStyle.objects.filter(
                document_template=dt,
                slug=slug
            ).first()
            ds_count = 0
            while existing_ds:
                slug = str(ds_count)
                ds_count += 1
                existing_ds = DocumentStyle.objects.filter(
                    document_template=dt,
                    slug=slug
                ).first()
            doc_style.slug = slug
            doc_style.contents = old_ds.contents
            doc_style.document_template = dt
            doc_style.save()
            for font in old_ds.fonts.all():
                style_file = DocumentStyleFile()
                filename = font.font_file.path.split('/').pop()
                existing_dsf = DocumentStyleFile.objects.filter(
                    style=doc_style,
                    filename=filename
                ).first()
                dsf_count = 0
                while existing_dsf:
                    filename = str(dsf_count)
                    dsf_count += 1
                    existing_dsf = DocumentStyleFile.objects.filter(
                        style=doc_style,
                        filename=filename
                    ).first()
                new_file = File(open(font.font_file.path, 'rb'))
                style_file.filename = filename
                style_file.style = doc_style
                style_file.file.save(
                    filename,
                    new_file
                )
                style_file.save()
                doc_style.contents = (
                    '@font-face {' +
                    font.fontface_definition.replace(
                        '[URL]',
                        style_file.filename
                    ) +
                    '}\n\n' + doc_style.contents
                )
                doc_style.save()
        for old_et in dt.export_templates.all():
            export_template = ExportTemplate()
            title = old_et.file_name[:128]
            existing_et = ExportTemplate.objects.filter(
                document_template=dt,
                title=title
            ).first()
            et_count = 0
            while existing_et:
                title = str(et_count)
                et_count += 1
                existing_et = ExportTemplate.objects.filter(
                    document_template=dt,
                    title=title
                ).first()
            export_template.title = title
            export_template.file_type = old_et.file_type
            export_template.document_template = dt
            new_file = File(open(old_et.template_file.path, 'rb'))
            export_template.template_file.save(
                title + '.' + old_et.file_type,
                new_file
            )
            # export_template.template_file = old_et.template_file
            export_template.save()


def empty_new_models(apps, schema_editor):
    DocumentStyle = apps.get_model('style', 'DocumentStyle')
    ExportTemplate = apps.get_model('style', 'ExportTemplate')
    DocumentStyle.objects.all().delete()
    ExportTemplate.objects.all().delete()
    if os.path.exists(
        os.path.join(
            settings.MEDIA_ROOT,
            "style-files"
        )
    ):
        shutil.rmtree(
            os.path.join(
                settings.MEDIA_ROOT,
                "style-files"
            )
        )
    if os.path.exists(
        os.path.join(
            settings.MEDIA_ROOT,
            "export-template-files"
        )
    ):
        shutil.rmtree(
            os.path.join(
                settings.MEDIA_ROOT,
                "export-template-files"
            )
        )


class Migration(migrations.Migration):

    dependencies = [
        ('style', '0005_documentstyle_documentstylefile_exporttemplate'),
    ]

    operations = [
        migrations.RunPython(convert_old_models, empty_new_models),
    ]
