import json

from google.appengine.ext import ndb
from google.appengine.api import memcache

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import Context
from django.conf import settings
from django.http import HttpResponseRedirect

from gae_django.auth.models import User

from july.people.models import Accumulator, Location, Project, Team
from google.appengine.api import channel
from july.live.models import Message, Connection
from july.api import to_dict


def index(request):
    """Render the home page"""
    
    # For now we are just using hard coded sections
    #sections = cache.get('front_page')
    #if sections is None:
    #    sections = Section.all().order('order').fetch(10)
    #    cache.set('front_page', sections, 120)
    
    stats = []
    total = 0
    people = []
    locations = []
    projects = []
    teams = []
    messages = []
    token = ''
    
    # this is only shown on authenticated page loads
    # to save on the overhead. 
    if request.user.is_authenticated():
        stats = Accumulator.get_histogram('global')
        total = sum(stats)
        location_future = Location.query().order(-Location.total).fetch_async(3)
        people_future = User.query().order(-ndb.GenericProperty('total')).fetch_async(5)
        project_future = Project.query().order(-Project.total).fetch_async(5)
        team_future = Team.query().order(-Team.total).fetch_async(3)
        message_future = Message.query().order(-Message.timestamp).fetch_async(10)
        # Live Connection
        #connection = Connection.get_or_insert(request.user.username, client_id=request.user.username)
        #connection_future = connection.put_async()
        
        # Julython live stuffs
        token_key = 'live_token:%s' % request.user.username
        token = memcache.get(token_key)
        if token is None:
            token = channel.create_channel(request.user.username)
            memcache.set(token_key, token, time=7200)

        
        locations = location_future.get_result()
        people = people_future.get_result()
        projects = project_future.get_result()
        teams = team_future.get_result()
        #connection_future.get_result()
        message_models = message_future.get_result()
        
        m_list = [to_dict(m) for m in message_models]
        m_list.reverse()
        messages = json.dumps(m_list)
    
    ctx = Context({
        'sections': [],
        'people': people,
        'projects': projects,
        'locations': locations,
        'teams': teams,
        'stats': json.dumps(stats),
        'total': total,
        'token': token,
        'messages': messages,
        'user': request.user,
        'MEDIA_URL': settings.MEDIA_URL,
        'STATIC_URL': settings.STATIC_URL})
    
    return render_to_response('index.html', context_instance=ctx)

def help_view(request):
    """Render the help page"""       
    ctx = Context({
        'user': request.user,
        'MEDIA_URL': settings.MEDIA_URL,
        'STATIC_URL': settings.STATIC_URL})
    
    return render_to_response('help.html', context_instance=ctx)

@login_required
def login_redirect(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/%s'%request.user.username )
    return HttpResponseRedirect('/')

def maintenance(request):
    """Site is down for maintenance, display this view for all."""
    ctx = Context({})
    
    return render_to_response('maintenance.html', context_instance=ctx)

def warmup(request):
    """Fire up the servers!"""
    from django.http import HttpResponse
    return HttpResponse('OK')

@@ -1562,191 +1562,10 @@
 ync(
-10)%0A        # Live Connection%0A        #connection = Connection.get_or_insert(request.user.username, client_id=request.user.username)%0A        #connection_future = connection.put_async(
+30
 )%0A  
@@ -2043,48 +2043,8 @@
 t()%0A
-        #connection_future.get_result()%0A
     

Add more messages on initial load