from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.shortcuts import get_object_or_404, render_to_response, redirect
from django.template import RequestContext
from models import Image, Fragment
from django.db.models.aggregates import Max



def home(request):
    
    # if the request has a "search" get parameter, we search this term
    search = None
    nb_results = 1
    if "search" in request.GET:
        search = request.GET["search"]
        field = "all"
        if "field" in request.GET:
            field = request.GET["field"]
    
    
    img_list = Image.objects.annotate(date_modif=Max('fragment__date_saved')).exclude(date_modif=None).order_by('-date_modif')[:12]
    frg_list = Fragment.objects.all().order_by('-date_saved')[:12]
    
    return render_to_response("egonomy_home.html",
                              {'img_list':img_list, 'fragment_list':frg_list, "search":search, "nb_results":nb_results},
                              context_instance=RequestContext(request))


def annotate_picture(request, image_id):

    img = get_object_or_404(Image, id=image_id)
    frg_list = Fragment.objects.filter(image=img).order_by('-date_saved')
    last_frg = None
    if frg_list.count()>0:
        last_frg = frg_list[0]
    
    return render_to_response("egonomy_annotate_picture.html",
                              {'img': img, 'fragment_list': frg_list, 'last_frg':last_frg},
                              context_instance=RequestContext(request))


def view_fragment(request, fragment_pk):
    
    frg = get_object_or_404(Fragment, pk=fragment_pk)
    frg_list = Fragment.objects.filter(image=frg.image)
    
    return render_to_response("egonomy_view_fragment.html",
                              {'fragment': frg, 'fragment_list': frg_list},
                              context_instance=RequestContext(request))

@login_required
def create_fragment(request, image_id):
    
    img = get_object_or_404(Image, id=image_id)
    frg_list = Fragment.objects.filter(image=img)
    
    return render_to_response("egonomy_create_fragment.html",
                              {'img': img, 'fragment_list': frg_list},
                              context_instance=RequestContext(request))

@login_required
def save_fragment(request):
    
    frg_title = request.POST["fragment_title"]
    frg_desc = request.POST["fragment_description"]
    frg_kw = request.POST["users_keywords"]
    frg_path = request.POST["fragment_path"]
    frg_image_id = request.POST["image_id"]
    img = get_object_or_404(Image, id=frg_image_id)
    
    frg = Fragment()
    frg.image = img
    frg.coordinates = frg_path
    frg.title = frg_title
    frg.description = frg_desc
    frg.tags = frg_kw
    frg.author = request.user
    
    frg.save()
    
    return redirect("view_fragment", fragment_pk=frg.pk)


def all_pictures(request):
    
    # Get the cur_page_nb number parameter if possible
    cur_page_nb = request.GET.get("page") or 1
    cur_page_nb = int(cur_page_nb)
    
    img_list = Image.objects.all().order_by('pk')
    nb = 32
    paginator = Paginator(img_list, nb)
    try:
        results = paginator.page(cur_page_nb)
    except (EmptyPage, InvalidPage):
        results = paginator.page(paginator.num_pages)

    return render_to_response("egonomy_all_pictures.html",
                              {'results':results, 'nb_pages':paginator.num_pages, 'cur_page_nb':cur_page_nb},
                              context_instance=RequestContext(request))


def all_fragments(request):
    
    # Get the cur_page_nb number parameter if possible
    cur_page_nb = request.GET.get("page") or 1
    cur_page_nb = int(cur_page_nb)
    
    frg_list = Fragment.objects.all().order_by('pk')
    nb = 32
    paginator = Paginator(frg_list, nb)
    try:
        results = paginator.page(cur_page_nb)
    except (EmptyPage, InvalidPage):
        results = paginator.page(paginator.num_pages)

    return render_to_response("egonomy_all_fragments.html",
                              {'results':results, 'nb_pages':paginator.num_pages, 'cur_page_nb':cur_page_nb},
                              context_instance=RequestContext(request))



