# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2022-10-31 13:24
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import signbank.dictionary.models

def copy_semanticfield_names_to_translations(apps, schema_editor):
    """
    Create translations for the name fields of SemanticField
    :param apps:
    :param schema_editor:
    """

    SemanticField = apps.get_model('dictionary', 'SemanticField')
    SemanticFieldTranslation = apps.get_model('dictionary', 'SemanticFieldTranslation')
    Language = apps.get_model('dictionary', 'Language')
    translation_language_eng = Language.objects.get(language_code_3char='eng')
    translation_language_nld = Language.objects.get(language_code_3char='nld')
    translation_language_zho = Language.objects.get(language_code_3char='zho')
    semanticfields = SemanticField.objects.filter(machine_value__gt=1)
    for semfield in semanticfields:
        translations_for_semfield = [sft.language for sft in SemanticFieldTranslation.objects.filter(semField=semfield)]
        if semfield.name_en and translation_language_eng not in translations_for_semfield:
            semanticfieldtranslation_eng, created = SemanticFieldTranslation.objects.get_or_create(
                                                        semField=semfield,
                                                        language=translation_language_eng,
                                                        name=semfield.name_en)
            semanticfieldtranslation_eng.save()
        if semfield.name_nl and translation_language_nld not in translations_for_semfield:
            semanticfieldtranslation_nld, created = SemanticFieldTranslation.objects.get_or_create(
                                                        semField=semfield,
                                                        language=translation_language_nld,
                                                        name=semfield.name_nl)
            semanticfieldtranslation_nld.save()
        if semfield.name_zh_hans and translation_language_zho not in translations_for_semfield:
            semanticfieldtranslation_zho, created = SemanticFieldTranslation.objects.get_or_create(
                                                        semField=semfield,
                                                        language=translation_language_zho,
                                                        name=semfield.name_zh_hans)
            semanticfieldtranslation_zho.save()

def copy_derivationhistory_names_to_translations(apps, schema_editor):
    """
    Create translations for the name fields of DerivationHistory
    :param apps:
    :param schema_editor:
    """

    DerivationHistory = apps.get_model('dictionary', 'DerivationHistory')
    DerivationHistoryTranslation = apps.get_model('dictionary', 'DerivationHistoryTranslation')
    Language = apps.get_model('dictionary', 'Language')
    translation_language_eng = Language.objects.get(language_code_3char='eng')
    translation_language_nld = Language.objects.get(language_code_3char='nld')
    translation_language_zho = Language.objects.get(language_code_3char='zho')
    derivationhistories = DerivationHistory.objects.all()
    for derivhist in derivationhistories:
        translations_for_derivhist = [dht.language for dht in DerivationHistoryTranslation.objects.filter(derivHist=derivhist)]
        if derivhist.name_en and translation_language_eng not in translations_for_derivhist:
            derivationhistorytranslation_eng, created = DerivationHistoryTranslation.objects.get_or_create(
                                                        derivHist=derivhist,
                                                        language=translation_language_eng,
                                                        name=derivhist.name_en)
            derivationhistorytranslation_eng.save()
        if derivhist.name_nl and translation_language_nld not in translations_for_derivhist:
            derivationhistorytranslation_nld, created = DerivationHistoryTranslation.objects.get_or_create(
                                                        derivHist=derivhist,
                                                        language=translation_language_nld,
                                                        name=derivhist.name_nl)
            derivationhistorytranslation_nld.save()
        if derivhist.name_zh_hans and translation_language_zho not in translations_for_derivhist:
            derivationhistorytranslation_zho, created = DerivationHistoryTranslation.objects.get_or_create(
                                                        derivHist=derivhist,
                                                        language=translation_language_zho,
                                                        name=derivhist.name_zh_hans)
            derivationhistorytranslation_zho.save()

class Migration(migrations.Migration):

    dependencies = [
        ('dictionary', '0052_auto_20221024_1633'),
    ]

    operations = [
        migrations.RunPython(copy_semanticfield_names_to_translations),
        migrations.RunPython(copy_derivationhistory_names_to_translations),
    ]
