import json
from django.db import transaction
from django.http import JsonResponse

from common.decorators import ajax_required
from connections.models import Entry, Status
from meanings.models import LexicalUnit
from semantics.models import Frame, FrameOpinion, Argument, ArgumentRole, SemanticRole, RoleAttribute, RoleSubAttribute, \
    RoleType


@ajax_required
@transaction.atomic
def change_status(request):
    if request.method == 'POST':
        entry_id = request.POST['entry_id']
        status = request.POST['status']

        entry = Entry.get(pk=entry_id)
        entry.status = Status.objects.get(key=status)
        entry.save()
        return JsonResponse({})

    return JsonResponse({})


@ajax_required
@transaction.atomic
def create_new_slowal_frame(request):
    if request.method == 'POST':
        entry_id = request.POST['entry_id']
        lu_ids = json.loads(request.POST['lu_ids'])
        frame = Frame(opinion=FrameOpinion.objects.get(key='unk'), in_building=True)
        frame.save()

        entry = Entry.objects.get(pk=entry_id)
        for lu_id in lu_ids:
            lu = LexicalUnit.objects.get(pk=lu_id)
            lu.entry = entry
            lu.save()
            frame.lexical_units.add(lu)
            entry.lexical_units_count = entry.lexical_units_count + 1

        frame.save()
        entry.save()
        return JsonResponse({})

    return JsonResponse({})


@ajax_required
@transaction.atomic
def add_argument_to_frame(request):
    if request.method == 'POST':
        frame_id = request.POST['frame_id']
        frame = Frame.objects.get(id=frame_id)

        argument = Argument(frame=frame)
        change_role_base(argument, request)
        argument.save()

        return JsonResponse({})

    return JsonResponse({})


@ajax_required
@transaction.atomic
def change_role(request):
    if request.method == 'POST':
        frame_id = request.POST['frame_id']
        argument_id = request.POST['argument_id']

        frame_argument = Argument.objects.get(frame_id=frame_id, id=argument_id)
        change_role_base(frame_argument, request)
        frame_argument.save()
    return JsonResponse({})


def change_role_base(frame_argument, request):
    role_id = request.POST['role_id']
    role_type = request.POST['role_type']
    attribute_id = request.POST.get('attribute_id', None)
    sub_attribute_id = request.POST.get('sub_attribute_id', None)

    argument_role = ArgumentRole.objects.filter(role_id=role_id, attribute_id=attribute_id,
                                                sub_attribute_id=sub_attribute_id).first()
    if argument_role is None:
        argument_role = ArgumentRole(role=SemanticRole.objects.get(pk=role_id),
                                     attribute=None if attribute_id is None else RoleAttribute.objects.get(pk=attribute_id),
                                     sub_attribute=RoleSubAttribute.objects.get(pk=sub_attribute_id))
        argument_role.save()

    frame_argument.role = argument_role
    frame_argument.role_type = RoleType.objects.get(type=role_type)