#!/usr/bin/env python
# -*- coding: utf-8 -*-
from children.models import Child
from dictionaries.models import Dictionary
from history.models import ParamHistory, Param


def get_dictionary_item(value, type_name):

    try:
        result = Dictionary.objects.get(type__slug=type_name, name=value)
    except Dictionary.DoesNotExist:
        result = None
    return result


def values_is_in_dictionary(values_list, type_name):

    for value in values_list:
        if get_dictionary_item(value, type_name):
            return True

    return False


def set_children_address(child, args):

    locality = args[5]
    street = args[6]
    house = args[7]
    flat = args[8]

    child.locality = get_dictionary_item('locality', locality)
    child.street = get_dictionary_item('streets', street)
    child.house = house
    child.flat = flat
    child.save()


def get_child(args):

    last_name = args[1]
    first_name = args[2]
    middle_name = args[3]
    birthday = args[4]

    child, created = Child.objects.get_or_create(
        last_name=last_name,
        first_name=first_name,
        middle_name=middle_name,
        birthday=birthday)

    return child


def date_is_free(child, parameter, date):
    return not ParamHistory.objects.filter(child=child, parameter__slug=parameter, first_date=date)


def set_children_education(child, date, args):

    institution = args[9]
    group = args[10]
    grade = args[11]

    try:
        parameter = Param.objects.get(slug='education')

        if date_is_free(child, 'education', date):

            ParamHistory.objects.create(
                first_date=date,
                parameter=parameter,
                child=child,
                institution=get_dictionary_item(institution, 'institutions'),
                group=get_dictionary_item(group, 'groups'),
                grade=get_dictionary_item(grade, 'grades'),
                risk_group=0
            )

    except Param.DoesNotExist:
        pass


def set_children_m2m_param(param_type, child, date, values):

    try:
        parameter = Param.objects.get(slug=param_type)
        values = values.replace(' ', '').split(',')
        if date_is_free(child, param_type, date) and values_is_in_dictionary(values, param_type):

            history = ParamHistory.objects.create(
                first_date=date,
                parameter=parameter,
                child=child,
                risk_group=0
            )

            for value in values:
                dict_value = get_dictionary_item(value, param_type)
                if dict_value:
                    if param_type == 'health':
                        history.health_states.add(dict_value)
                    if param_type == 'parents':
                        history.parents_status.add(dict_value)

    except Param.DoesNotExist:
        pass


def loader(data, on_date):

    for item in data:
        child = get_child(item)
        set_children_address(child, item)
        set_children_education(child, on_date, item)
        set_children_m2m_param('health', child, on_date, item[12])
        set_children_m2m_param('parents', child, on_date, item[13])

@@ -2842,24 +2842,976 @@
      pass%0A%0A%0A
+def set_children_risk(param_type, child, date, value):%0A%0A    try:%0A        parameter = Param.objects.get(slug=param_type)%0A%0A        value = list(filter(lambda val_list: val_list%5B1%5D == value, ParamHistory.BOOL_CHOICES))%0A%0A        if date_is_free(child, param_type, date) and value:%0A            ParamHistory.objects.create(%0A                first_date=date,%0A                parameter=parameter,%0A                child=child,%0A                risk_group=value%5B0%5D%0A            )%0A%0A    except Param.DoesNotExist:%0A        pass%0A%0A%0Adef set_children_note(param_type, child, date, value):%0A    try:%0A        parameter = Param.objects.get(slug=param_type)%0A%0A        if date_is_free(child, param_type, date) and value:%0A            ParamHistory.objects.create(%0A                first_date=date,%0A                parameter=parameter,%0A                child=child,%0A                risk_group=0,%0A                note=value%0A            )%0A%0A    except Param.DoesNotExist:%0A        pass%0A%0A%0A
 def loader(d
@@ -4110,8 +4110,128 @@
 em%5B13%5D)%0A
+        set_children_risk('risk', child, on_date, item%5B14%5D)%0A        set_children_note('note', child, on_date, item%5B15%5D)%0A

add note, add risk