# Create your views here.
from models import *
from forms import *
from django.shortcuts import render_to_response
from ox.django.shortcuts import render_to_json_response
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required


def person_form(request):
    person = Person.objects.all()[0]
    form = PersonForm(instance=person)
    inlines = [inline(instance=person) for inline in form.inlines]
    #import pdb
    #pdb.set_trace()
    return render_to_response("test/person_form.html", {'form': form, 'inlines': inlines})


@login_required
def edit_profile(request):
    user = request.user
    person = Person.objects.get(user=user)
    
    #person = Person.objects.all()[0]
    if request.POST:
        form = PersonForm(request.POST, request.FILES, instance=person)
        inlines = [inline(request.POST, request.FILES, instance=person) for inline in form.inlines]
        if form.is_valid():
            instance = form.save()
            form.save()
            all_valid = True
            for inline in inlines:
                if inline.is_valid():
                    inline.save()
                else:
                    all_valid = False
            if all_valid:
                return HttpResponseRedirect("/edit_profile/")

    else:
        form = PersonForm(instance=person)
        inlines = [inline(instance=person) for inline in form.inlines]
    
    context = RequestContext(request, {
        'form': form,
        'inlines': inlines
    })
    return render_to_response("test/person_form.html", context)

def personpopup(request):
    form = PersonForm()
    if request.POST:
        return HttpResponse("<script>opener.dismissAddAnotherPopup(window, '%s', '%s')</script>" % ("45", "sanjay bhangar",)) 
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save()
#            return HttpResponse("<script>opener.dismissAddAnotherPopup(this, '%s', '%s')</script>" % (str(instance.id), instance.get_title()))
    else:
        form = PersonForm()
    context = RequestContext(request, {
        'form': form
    })
    return render_to_response("test/popup.html", context)



def autocomplete(request):
    q = request.GET.get('q', 'a')
    page = int(request.GET.get('page', '1'))
    page_limit = int(request.GET.get('page_limit', '10'))
    #qset_all = Person.objects.all()
    qset = Person.objects.filter(first_name__icontains=q)
    paginator = Paginator(qset, page_limit)
    results = paginator.page(page)
    items = [{'id': obj.id, 'first_name': obj.first_name, 'last_name': obj.last_name, 'about': obj.about, 'text': obj.first_name + " " + obj.last_name} for obj in results.object_list]
    return render_to_json_response({
        'items': items,
        'has_next': results.has_next()
    })
                                


def grouppopup(request):
    form = PersonForm()
    if request.POST:
        return HttpResponse("<script>opener.dismissAddAnotherPopup(window, '%s', '%s')</script>" % ("45", "sanjay bhangar",)) 
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save()
#            return HttpResponse("<script>opener.dismissAddAnotherPopup(this, '%s', '%s')</script>" % (str(instance.id), instance.get_title()))
    else:
        form = PersonForm()
    context = RequestContext(request, {
        'form': form
    })
    return render_to_response("test/popup.html", context)
