# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2016-11-23 09:04
from __future__ import unicode_literals

from django.db import migrations
from pyxform.xls2json import parse_file_to_json
from botocore.exceptions import ClientError


def populate_additional_fields(apps, schema_editor):
    Project = apps.get_model('organization', 'Project')
    Questionnaire = apps.get_model('questionnaires', 'Questionnaire')
    QuestionGroup = apps.get_model('questionnaires', 'QuestionGroup')
    Question = apps.get_model('questionnaires', 'Question')

    def update_question(child, **kwargs):
        relevant = None
        required = False
        bind = child.get('bind')
        if bind:
            relevant = bind.get('relevant', None)
            required = True if bind.get('required', 'no') == 'yes' else False

        question = Question.objects.get(**kwargs)
        question.default = child.get('default', None)
        question.hint = child.get('hint', None)
        question.relevant = relevant
        question.required = required
        question.save()

    def update_group(child, group=None, **kwargs):
        group = QuestionGroup.objects.get(**kwargs)

        relevant = None
        bind = child.get('bind')
        if bind:
            relevant = bind.get('relevant', None)

        group.relevant = relevant
        group.type = child['type']
        return group.id

    def update_children(children, questionnaire_id, question_group_id=None):
        for child in children:
            if child['type'] in ['group', 'repeat']:
                group_id = update_group(child,
                                        questionnaire_id=questionnaire_id,
                                        question_group_id=question_group_id,
                                        name=child['name'])
                update_children(child.get('children', []),
                                questionnaire_id,
                                question_group_id=group_id)
            else:
                update_question(child,
                                questionnaire_id=questionnaire_id,
                                question_group_id=question_group_id,
                                name=child['name'])

    for project in Project.objects.all():
        if project.current_questionnaire:
            questionnaire = Questionnaire.objects.get(
                id=project.current_questionnaire)

            if questionnaire.xls_form:
                try:
                    q_json = parse_file_to_json(
                        questionnaire.xls_form.file.name)
                    update_children(
                        q_json.get('children', []), questionnaire.id)
                except ClientError:
                    pass


class Migration(migrations.Migration):
    dependencies = [
        ('questionnaires', '0016_populate_question_index_field'),
    ]

    operations = [
        migrations.RunPython(
            populate_additional_fields,
            reverse_code=migrations.RunPython.noop
        )
    ]
