# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-06-18 12:13
from __future__ import unicode_literals

from django.db import migrations
from django.db.transaction import atomic
from signbank.settings.base import DEFAULT_KEYWORDS_LANGUAGE


def move_lemma_idglosses(apps, schema_editor):
    """This function copies the idgloss from Gloss instances
    to the LemmaIdglossTranslation model. """

    Gloss = apps.get_model('dictionary', 'Gloss')
    Dataset = apps.get_model('dictionary', 'Dataset')
    Language = apps.get_model('dictionary', 'Language')
    LemmaIdgloss = apps.get_model('dictionary', 'LemmaIdgloss')
    LemmaIdglossTranslation = apps.get_model('dictionary', 'LemmaIdglossTranslation')

    def move_per_gloss(gl, dataset, language):
        number_copied = 0
        idgloss = gl.idgloss
        lemma_idgloss_translations = LemmaIdglossTranslation.objects.filter(lemma__dataset=dataset,
                                                                            language=language,
                                                                            text=idgloss)
        with atomic():
            if lemma_idgloss_translations is None or len(lemma_idgloss_translations) == 0:
                # try:
                lemma = LemmaIdgloss(dataset=gl.dataset)
                lemma.save()
                LemmaIdglossTranslation(lemma=lemma, language=language, text=idgloss).save()
                gl.lemma = lemma
                gl.save()
                number_copied += 1
                # except:
                #     pass
            elif len(lemma_idgloss_translations) == 1:
                gl.lemma = lemma_idgloss_translations[0].lemma
                gl.save()

        return number_copied

    def move_per_language(language_code_2char):
        number_copied = 0
        for dataset in Dataset.objects.all():
            if dataset.name == 'NGT':
                language = Language.objects.get(language_code_2char='nl')
            else:
                language = Language.objects.get(language_code_2char=language_code_2char)
            if language is not None:
                for gl in dataset.gloss_set.all():
                    number_copied += move_per_gloss(gl, dataset, language)

        # Don't forget glosses without a dataset
        dataset = None
        language = Language.objects.get(language_code_2char=language_code_2char)
        if language is not None:
            for gl in Gloss.objects.filter(dataset=dataset):
                number_copied += move_per_gloss(gl, dataset, language)

        return number_copied

    number_copied = move_per_language(DEFAULT_KEYWORDS_LANGUAGE['language_code_2char'])

    print("Number of copied annotation_idglosses: " + str(number_copied))


def remove_lemma_idglosses(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('dictionary', '0023_auto_20180618_1225'),
    ]

    operations = [
        migrations.RunPython(move_lemma_idglosses, remove_lemma_idglosses),
    ]
