# -*- encoding: utf-8 -*-
#
# Copyright (C) 2014 Shang Yuanchun <idealities@gmail.com>
#

import copy
import logging

from django.http import JsonResponse
from django.utils import timezone
from django.shortcuts import render
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.core.paginator import Paginator, EmptyPage

import bublfish.settings as settings
from account.models import User
from api.response import JsonpResponse
from api.response import CONTENT_TYPE_JSON
from api.response import KWARGS_JSON
from api.response import DATA_OK
from api.response import DATA_ERR
from api.request  import login_required, post_required

from api.models import Comment

try:
    import urlparse
except:
    import urllib.parse as urlparse

log = logging.getLogger(__name__)

def index(request):
    return JsonResponse(data = DATA_OK, **KWARGS_JSON)

DEFAULT_PAGE_SIZE = 10

def pull(request):
    """
    Request: url=http://foo.bar.com&page=1&limit=10

    Response:
    {
        "status": 200,
        "data": [
                {"id": 1, "date": "2014-10-28 18:09:00", "content": "呵呵", "parent": 0, "author": "ideal", "avatar": "http://"},
                {"id": 2, "date": "2014-10-28 18:10:00", "content": "哈哈", "parent": 0, "author": "ideal", "avatar": "http://"},
                ],
    }
    """
    referer = _parse_url(request.META.get('HTTP_REFERER'))
    pageurl = _parse_url(request.GET.get('url'))

    if (referer and pageurl) and (referer['host'] != pageurl['host']):
        from . import response
        return response.error(403, _('Wrong request'), request.GET.get('callback'))

    url = pageurl['url'] if pageurl else (referer['url'] if referer else None)
    if url is None:
        from . import response
        return response.error(400, _('Wrong parameters'), request.GET.get('callback'))

    data = copy.deepcopy(DATA_OK)
    data['data'] = []

    def fill_comments(comments):
        for comment in comments:
            user = User.objects.get(id=comment.user_id)
            data['data'].append({
                    "id": comment.comment_id,
                    "date": timezone.localtime(comment.comment_date).strftime("%Y-%m-%d %H:%M:%S"),
                    "content": comment.comment_content,
                    "parent": comment.comment_parent,
                    "author": user.username,
                    "avatar": user.avatar,
                    })

    # Normal comments
    comments  = Comment.objects.filter(comment_page=url,
                                comment_type=Comment.TYPE_NORMAL).order_by('-comment_date')
    try:
        limit = int(request.GET.get('limit'))
    except:
        limit = DEFAULT_PAGE_SIZE
    try:
        pagenum = int(request.GET.get('page'))
    except:
        pagenum = 1
    limit   = DEFAULT_PAGE_SIZE if limit <= 0 else limit
    pagenum = 1 if pagenum <= 0 else pagenum

    paginator = Paginator(comments, limit)
    try:
        comments  = paginator.page(pagenum)
    except EmptyPage:
        comments = []
    fill_comments(comments)

    # Reply comments
    # TODO: however this will fetch all reply comments.
    comments  = Comment.objects.filter(comment_page=url,
                                comment_type=Comment.TYPE_REPLY).order_by('comment_id')
    fill_comments(comments)

    return JsonpResponse(data = data, callback = request.GET.get('callback'),
               content_type = CONTENT_TYPE_JSON)

@post_required
@login_required
def post(request):
    """
    Request:
    {
     "page": "http://foo.bar.com/blog/page/1",
     "content": "Hello, 来自三体世界的评论",
     "parent" : 0,
    }
    """
    comment = Comment()
    comment.user_id = request.user.id
    comment.comment_date = timezone.now()
    comment.comment_page = _parse_url(request.POST.get('page'))['url']
    comment.comment_content = request.POST.get('content')
    if request.POST.get('parent', None):
        try:
            comment.comment_parent = int(request.POST.get('parent'))
        except:
            comment.comment_type   = Comment.TYPE_NORMAL
        else:
            comment.comment_type   = Comment.TYPE_REPLY

    if comment.comment_parent > 0:
        parent = Comment.objects.get(comment_id=parent)
    except Comment.DoesNotExist:
        from . import response
        return response.error(400, _('Bad data'),
                              request.POST.get('callback'))

    try:
        comment.clean_fields()
    except ValidationError as e:
        from . import response
        return response.error(400, _('Bad data'),
                              request.POST.get('callback'),
                              inner_data = e.message_dict if settings.DEBUG else None)

    comment.save()
    return JsonpResponse(data = DATA_OK, callback = request.POST.get('callback'),
                         **KWARGS_JSON)

def _parse_url(url):
    """
    """
    if url is None:
        return None
    parsed = urlparse.urlparse(url)
    return {'host': parsed.netloc, 'url': parsed.netloc + parsed.path}

@@ -685,16 +685,33 @@
 required
+%0Aimport api.utils
 %0A%0Afrom a
@@ -941,16 +941,131 @@
 E = 10%0A%0A
+class _User(object):%0A    username = _(%22anonymous%22)%0A    avatar   = api.utils.DEFAULT_GRAVATAR_URL%0A%0A_user = _User()%0A%0A
 def pull
@@ -2123,16 +2123,37 @@
 mments:%0A
+            try:%0A    
         
@@ -2200,16 +2200,65 @@
 ser_id)%0A
+            except:%0A                user = _user%0A
         
@@ -4390,16 +4390,33 @@
 nt %3E 0:%0A
+        try:%0A    
         
@@ -4463,16 +4463,20 @@
 parent)%0A
+    
     exce
@@ -4496,24 +4496,28 @@
 esNotExist:%0A
+    
         from
@@ -4535,32 +4535,36 @@
 esponse%0A        
+    
 return response.
@@ -4581,32 +4581,36 @@
  _('Bad data'),%0A
+    
                 

fix syntax error :( and add a _user