# encoding: utf-8
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.views.generic import ListView
from django.utils.datastructures import MultiValueDictKeyError
from django import forms
from django.core.urlresolvers import reverse

from twython import Twython, TwythonAuthError, TwythonError

import datetime as date
from oauth_hook import OAuthHook
import requests
from urlparse import parse_qs
import json
from pytz import timezone

from news.models import *
from news.utils import from4id, get_absolute_uri, format_email
from settings import FACEBOOK_APP_SECRET, FACEBOOK_APP_ID, MESSAGE_FORMAT

import logging
logger = logging.getLogger(__file__)

class NewsForm(forms.Form):
    class Meta:
        model = News
    emailto = default_email_to

class NewsList(ListView):
    #context_object_name="news_list"
    template_name = 'news/index.html'

    def get_queryset(self):
        q1 = News.objects.filter(publishid__isnull=True).order_by('-modifed')
        q2 = News.objects.filter(publishid__isnull=False)
        return q1 | q2

def index(request, **kwargs):
    form = NewsForm()
    unpublished = News.objects.filter(publishid__isnull=True).order_by('-modifed')
    return render_to_response('news/index.html',
        {'form': form, 'news_list' : unpublished },
        context_instance=RequestContext(request))

def edit(request, **kwargs):
    notice = None
    if 'mid' in kwargs:
        mid = kwargs['mid']
    else:
        mid = None
    if request.method == 'POST':
        tofacebook = False
        twitter = False
        email = False
        try:
            subject  = request.POST['subject']
            message  = request.POST['message']
            creator  = request.POST['name']
            toemail  = request.POST['emailto']
            email_message = request.POST['email']
            if 'tofacebook' in request.POST:
                tofacebook = True
            if 'totwitter' in request.POST:
                twitter = True
            id       = request.POST['newsid']
        except MultiValueDictKeyError as error:
            return new(request, **kwargs)

        try:
            news = News.objects.get(id__exact=int(id))

        except (News.DoesNotExist, ValueError) as error:
            news = News(subject=subject, message=message, creator=creator,
                        toemail=toemail, totwitter=twitter,
                        tofacebook = tofacebook)
            try:
                news.full_clean()
            except ValidationError:
                kwargs['error'] = 'Unvalid xml detected!!!!'
                kwargs['form'] = form
                return new(request, **kwargs)
            news.save()
            #return HttpResponseRedirect(reverse('new', news.id,
            #                   kwargs={'message' : message}))
            #return render_to_response('news/new.html',{'form': news,
            #                                   'message' : notice},
            #                        context_instance=RequestContext(request))
            kwargs['mid'] = news.id
            return new(request, **kwargs)

        if 'delete' in request.POST:
            news.delete()
            return redirect('list')
        if 'unpublish' in request.POST:
            news.unpublish()
            return new(request, **kwargs)
        news.subject = subject
        news.message = message.strip()
        news.creator = creator
        news.tofacebook = tofacebook
        news.totwitter = twitter
        news.toemail = toemail
        try:
            news.full_clean()
        except ValidationError:
            kwargs['error'] = 'Unvalid xml detected!!!!'
            kwargs['form'] = news
            return new(request, **kwargs)
        news.save()
        notice = 'Saved successfully!\n'
        if 'publish' in request.POST:
            news.publish(email_message=email_message)
        kwargs['message'] = notice
        kwargs['form'] = news
        return new(request, **kwargs)

    if mid:
        news = get_object_or_404(News, id__exact=mid)
        return new(request, **kwargs)
    else:
        return redirect(new)

def new(request, **kwargs):
    if 'mid' in kwargs:
        mid = kwargs['mid']
        if 'log' not in kwargs:
            kwargs['log'] = Logs.objects.filter(news_id__exact=mid).order_by(
                                                                '-date').all()
        if 'form' not in kwargs:
            kwargs['form'] = get_object_or_404(News, id__exact=mid)
    if 'form' not in kwargs:
        kwargs['form'] = NewsForm()
    return render_to_response('news/new.html', kwargs,
                        context_instance=RequestContext(request))

def FBRenewToken(request, **kwargs):
    # https://developers.facebook.com/docs/authentication/applications/
    # https://developers.facebook.com/docs/authentication/
    url = "https://www.facebook.com/dialog/oauth?client_id=%s" % FACEBOOK_APP_ID
    url += "&scope=publish_actions&redirect_uri=%stoken/&response_type=token" % (
        get_absolute_uri(request))
    return redirect(url)

def FBGetToken(request, **kwargs):