#xxx TODO - refactor album table to just be IDs?

from django.http import HttpResponse
from models import Location, Album, Photo
from django.core import serializers
from django.db.models import Max, Min
from decimal import Decimal
import datetime
import dbhelper
import json

class MyJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return "%s" % obj
        return json.JSONEncoder.default(self,obj)


def getlocations(request, daterange=None):
    """Returns JSON of latlongs from the DB. If they don't exist in DB, we import the data from picasa"""
    
    dbhelper.checkfordata()
    locs = None

    #if we have a range, scope to it
    if daterange == None:
        locs = serializers.serialize("python", Location.objects.all())
    else:
        ary = daterange.split(":")
        lower = datetime.datetime.strptime(ary[0], "%m-%d-%Y")
        upper = datetime.datetime.strptime(ary[1], "%m-%d-%Y")
        locs = serializers.serialize("python", set(Location.objects.filter(album__date__range=(lower, upper))))

    #create the feed and dump the json
    mindate = Album.objects.aggregate(Min('date'))
    maxdate = Album.objects.aggregate(Max('date'))        
    strmin = (mindate['date__min']).isoformat()
    strmax = (maxdate['date__max']).isoformat()    
    feed = {'mindate':strmin, 'maxdate':strmax, 'locations':locs}
    return HttpResponse(json.dumps(feed, cls=MyJSONEncoder))

def getalbums(request, location_id, daterange=None):
    """Given locationID and dateranage, returns JSON of albums in that location from DB"""
    
    if daterange == None:
        return HttpResponse(serializers.serialize("json", Album.objects.filter(location=location_id)))
    else:
        ary = daterange.split(":")
        lower = datetime.datetime.strptime(ary[0], "%m-%d-%Y")
        upper = datetime.datetime.strptime(ary[1], "%m-%d-%Y")
        albums = serializers.serialize("json", set(Album.objects.filter(location=location_id, date__range=(lower, upper))))
        return HttpResponse(albums)
 
    
#locs = ["name:%s, lat:%s long:%s" % (loc.name, loc.lat, loc.long) for loc in list]