# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-11-22 02:36
from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType

from django.db import migrations


def migrate_result_descriptions(apps, *args):
    Result = apps.get_model('aims', 'result')
    ResultDescription = apps.get_model('aims', 'ResultDescription')
    ResultTitle = apps.get_model('aims', 'ResultTitle')
    Narrative = apps.get_model('aims', 'Narrative')

    for result in Result.objects.all():

        result.result_type = result.type
        result.save()

        if result.description is not None and result.description != '':
            Narrative.objects.create(
                related_content_type_id=ContentType.objects.get_or_create(app_label='aims', model='resultdescription')[0].id,
                related_object_id=ResultDescription.objects.create(
                    result=result
                ).id,
                language_id='en',
                activity_id=result.activity_id,
                content=result.description
            )

        if result.title is not None and result.title != '':

            Narrative.objects.create(
                related_content_type_id=ContentType.objects.get_or_create(app_label='aims', model='resulttitle')[0].id,
                related_object_id=ResultTitle.objects.create(
                    result=result
                ).id,
                language_id='en',
                activity_id=result.activity_id,
                content=result.title
            )


def migrate_resultindicator(apps, *args):
    ResultIndicator = apps.get_model('aims', 'ResultIndicator')
    ResultIndicatorDescription = apps.get_model('aims', 'ResultIndicatorDescription')
    ResultIndicatorTitle = apps.get_model('aims', 'ResultIndicatorTitle')
    Narrative = apps.get_model('aims', 'Narrative')
    ResultIndicatorBaselineComment = apps.get_model('aims', 'ResultIndicatorBaselineComment')

    for resultindicator in ResultIndicator.objects.all():

        if resultindicator.description is not None and resultindicator.description != '':

            Narrative.objects.create(
                related_content_type_id=ContentType.objects.get_or_create(app_label='aims', model='resultindicatordescription')[0].id,
                related_object_id=ResultIndicatorDescription.objects.create(
                    resultindicator=resultindicator
                ).id,
                language_id='en',
                activity_id=resultindicator.result.activity_id,
                content=resultindicator.description
            )

        if resultindicator.title is not None and resultindicator.title != '':

            Narrative.objects.create(
                related_content_type_id=ContentType.objects.get_or_create(app_label='aims', model='resultindicatortitle')[0].id,
                related_object_id=ResultIndicatorTitle.objects.create(
                    resultindicator=resultindicator
                ).id,
                language_id='en',
                activity_id=resultindicator.result.activity_id,
                content=resultindicator.title
            )

        if resultindicator.comment is not None and resultindicator.comment != '':

            Narrative.objects.create(
                related_content_type_id=ContentType.objects.get_or_create(app_label='aims', model='resultindicatorbaselinecomment')[0].id,
                related_object_id=ResultIndicatorBaselineComment.objects.create(
                    resultindicator=resultindicator
                ).id,
                language_id='en',
                activity_id=resultindicator.result.activity_id,
                content=resultindicator.comment
            )


def migrate_period(apps, *args):
    ResultIndicatorPeriod = apps.get_model('aims', 'ResultIndicatorPeriod')

    for ri_period in ResultIndicatorPeriod.objects.all():
        ri_period.period_start = ri_period.planned_disbursement_period_start
        ri_period.period_end = ri_period.planned_disbursement_period_end
        ri_period.save()


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0048_results_ala_zimmerman'),
    ]

    operations = [

        migrations.RunPython(migrate_result_descriptions),
        migrations.RunPython(migrate_resultindicator),
        migrations.RunPython(migrate_period),

        migrations.RemoveField(
            model_name='result',
            name='description',
        ),
        migrations.RemoveField(
            model_name='result',
            name='result_type',
        ),
        migrations.RemoveField(
            model_name='result',
            name='title',
        ),
        migrations.RemoveField(
            model_name='resultindicator',
            name='comment',
        ),
        migrations.RemoveField(
            model_name='resultindicator',
            name='description',
        ),
        migrations.RemoveField(
            model_name='resultindicator',
            name='title',
        ),
        migrations.RemoveField(
            model_name='resultindicatorperiod',
            name='planned_disbursement_period_end',
        ),
        migrations.RemoveField(
            model_name='resultindicatorperiod',
            name='planned_disbursement_period_start',
        ),
        migrations.DeleteModel(
            name='ResultIndicatorMeasure',
        ),
        migrations.DeleteModel(
            name='ResultType',
        ),

    ]
