import json
from django.shortcuts import get_object_or_404
from django.core import serializers
from django.http import HttpResponse
from .models import Unit
from .utils import UNIT_LIST_FIELD

BAD_REQUEST = HttpResponse(json.dumps({'error': 'Bad Request'}), status=400, content_type='application/json')

def unit_json_list(request):
    ''' List Json View for local available units '''
    if request.is_ajax():
        units = Unit.objects.available_units()
        data = serializers.serialize('json', list(units), fields=UNIT_LIST_FIELD)
        _raw_data = json.loads(data)
        for unit in _raw_data:
            if unit['fields']['is_alliance']:
                unit['fields'].update({'identifier': '{}{}'.format(unit['fields']['identifier'],' (Alianza)')})
            else:
                continue
        return HttpResponse(json.dumps(_raw_data), content_type='application/json', status=200)
    else:
        return BAD_REQUEST

def detail_unit_json(request, id_unit):
    ''' Detail view of unit '''
    if request.is_ajax():
        unit = Unit.objects.filter(pk=id_unit)

        if len(unit) == 0:
            return HttpResponse(json.dumps({'error': 'Unidad no encontrada'}), status=404, content_type='application/json')

        data = serializers.serialize('json', unit, fields=UNIT_LIST_FIELD)
        # Add crew list
        _raw_data = json.loads(data)
        _raw_data[0]['fields'].update({
            'crew_list' : unit.first().get_crew_list
        })
        return HttpResponse(json.dumps(_raw_data), content_type='application/json', status=200)
    else:
        return BAD_REQUEST

def alliance_unit_json_list(request):
    ''' List Json View for alliance available units '''
    if request.is_ajax():
        units = Unit.objects.available_alliance_units()
        data = serializers.serialize('json', list(units), fields=UNIT_LIST_FIELD)
        return HttpResponse(data, content_type='application/json', status=200)
    else:
        return BAD_REQUEST

[((8, 27, 8, 63), 'json.dumps', 'json.dumps', ({(8, 38, 8, 62): "{'error': 'Bad Request'}"}, {}), "({'error': 'Bad Request'})", False, 'import json\n'), ((15, 20, 15, 36), 'json.loads', 'json.loads', ({(15, 31, 15, 35): 'data'}, {}), '(data)', False, 'import json\n'), ((33, 15, 33, 74), 'django.core.serializers.serialize', 'serializers.serialize', (), '', False, 'from django.core import serializers\n'), ((35, 20, 35, 36), 'json.loads', 'json.loads', ({(35, 31, 35, 35): 'data'}, {}), '(data)', False, 'import json\n'), ((48, 15, 48, 78), 'django.http.HttpResponse', 'HttpResponse', (), '', False, 'from django.http import HttpResponse\n'), ((21, 28, 21, 49), 'json.dumps', 'json.dumps', ({(21, 39, 21, 48): '_raw_data'}, {}), '(_raw_data)', False, 'import json\n'), ((39, 28, 39, 49), 'json.dumps', 'json.dumps', ({(39, 39, 39, 48): '_raw_data'}, {}), '(_raw_data)', False, 'import json\n'), ((31, 32, 31, 77), 'json.dumps', 'json.dumps', ({(31, 43, 31, 76): "{'error': 'Unidad no encontrada'}"}, {}), "({'error': 'Unidad no encontrada'})", False, 'import json\n')]