import settings,facebook,urllib,cgi,datetime

from timestack import utils
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse, HttpResponse
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from timestack.models import *

#render_to_rensponse
def index(request):
    params = dict(LOGIN_URL='https://www.facebook.com/dialog/oauth?client_id=' + str(settings.FACEBOOK_APP_ID) + \
                    '&redirect_uri='+urllib.quote(settings.FACEBOOK_REDIRECT_URI) + \
                    '&scope=read_stream,offline_access,user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags')
    return render_to_response('timestack/index.html',params,context_instance=RequestContext(request))

def login(request, next='/home/'):
    error = ''
    if request.method == 'GET':
        if 'code' in request.GET:
            args = {
                'client_id': settings.FACEBOOK_APP_ID,
                'redirect_uri': (settings.FACEBOOK_REDIRECT_URI),
                'client_secret': settings.FACEBOOK_APP_SECRET,
                'code': request.GET['code'],
            }
            url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)
            response = cgi.parse_qs(urllib.urlopen(url).read())
            access_token = response['access_token'][0]
            #access by Backend
            user = auth.authenticate(token=access_token)
            if user:
                if user.is_active:
                    auth.login(request, user)
                    if (datetime.datetime.now()-user.date_joined).days < 2:
                        request.session['first_time']=True
                    try:
                        if settings.STAFFS.index(user.username)>=0:
                            user.is_staff=True
                            user.save()  
                    except:
                        user.is_staff=False
                        user.save()  
                        pass
                    return redirect(next)
                else:
                    error = 'AUTH_DISABLED'
            else:
                error = 'AUTH_FAILED'
        elif 'error_reason' in request.GET:
            error = 'AUTH_DENIED'
        else:
            login_url='https://www.facebook.com/dialog/oauth?client_id=' + str(settings.FACEBOOK_APP_ID) + \
                '&redirect_uri='+urllib.quote(settings.FACEBOOK_REDIRECT_URI) + \
                '&scope=read_stream,offline_access,user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags'
            return HttpResponseRedirect(login_url)    
    request.session['error']="Cannot login"  
    return HttpResponseRedirect('/') 
      
def logout(request):
    if request.user.is_authenticated():
        auth.logout(request)
    request.session['msg']="Logged out" 
    # return HttpResponseRedirect('/')
    return redirect('http://m.facebook.com/logout.php?confirm=1&next='+ urllib.quote(settings.URI))

def unauthorized(request):
    params = {}
    return render_to_response('timestack/unauthorized.html',params,context_instance=RequestContext(request))

@login_required
def home(request):
    if request.user.username in settings.STAFFS:
        songs = Song.objects.filter(activate=True)
        first_song = None
        playlist = ''
        if len(songs)>0:
            first_song = songs[0]
            delim = ''
            if len(songs)>1:
                for song in songs[1:]:
                    playlist += ('%s%s' % (delim,song.sid))
                    delim = ','
        params = {
            'song_enable':True if len(songs)>0 else False,
            'first_song':'' if first_song is None else first_song,
            'playlist':'' if not playlist else playlist,
        }
        return render_to_response('timestack/home.html',params,context_instance=RequestContext(request))
    return redirect('/unauthorized/')

@login_required
def get_photos(request, page=1, limit=25, person=0):
    if request.user.username in settings.STAFFS:
        albums = Album.objects.all()
        pictures = []
        sources = []
        if len(albums) >= int(page) & int(page)>0:  
            args = {'limit': 200}
            album = albums[int(page)-1]
            photos = facebook.GraphAPI(album.owner.profile.access_token).request(str(album)+'/photos',args)
            for photo in photos['data']:
                pictures.append(photo['picture'])
                sources.append(photo['source'])
            res = dict(pictures=pictures,sources=sources,page=page,limit=limit,albums=len(albums))
        else: 
            args = {
                'offset': (int(page)-len(albums)-1)*int(limit),
                'limit': limit,
            }
            i = 0
            for staff in settings.STAFFS:
                photos = facebook.GraphAPI(request.user.profile.access_token).request(staff+'/photos',args)
                i=i+1
                index = 0
                for photo in photos['data']:
                    for tag in photo['tags']['data']:
                        banned = True if tag['id'] in settings.BANNED else False
                    if not banned:
                        pictures.insert(index,photo['picture'])
                        sources.insert(index,photo['source'])
                        index = index+i
                # person_index = person_index+1 if person_index+1<len(settings.STAFFS) else 0
            res = dict(pictures=pictures,sources=sources,page=page,limit=limit,albums=len(albums))
    return HttpResponse(utils._build_json(res),mimetype='application/json')

@login_required
def add_song(request):
    if request.user.username in settings.STAFFS:
        if request.method == 'POST':
            data = request.POST
            youtube_url = data.get('link')
            if(youtube_url.find('www.youtube.com/watch?v=')>=0):
                start = youtube_url.find('v=') + 2
                end = youtube_url.find('&',start-2)
                code = youtube_url[start:] if end<0 else youtube_url[start:end]
                # fetch youtube feed json data
                json_string = urllib.urlopen('http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % code).read()
                json = utils._parse_json(json_string)
                s = Song(sid=code,owner=request.user,title=json['entry']['title']['$t'],activate=True)
                s.save()
                return HttpResponse(utils._build_json(dict(result=True,title=json['entry']['title']['$t'])),mimetype='application/javascript')
            return HttpResponse(utils._build_json(dict(result=False)),mimetype='application/json')
    return HttpResponse(utils._build_json(dict(result=False)),mimetype='application/json')

@login_required
def get_playlist(request):
    if request.user.username in settings.STAFFS:
        songs = Song.objects.filter(activate=True)