# -*- coding: utf-8 -*-
from datetime import datetime
import os.path
import random
import traceback

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404

from common.http import HttpResponseJson

def render_to(template_path):
    """
    Expects the dict from view. Render returned dict with
    RequestContext.
    """

    def decorator(func):
        def wrapper(request, *args, **kwargs):
            output = func(request, *args, **kwargs)
            if not isinstance(output, dict):
                return output
            kwargs = {'context_instance': RequestContext(request)}
            if 'MIME_TYPE' in output:
                kwargs['mimetype'] = output.pop('MIME_TYPE')
            if 'TEMPLATE' in output:
                template = output.pop('TEMPLATE')
            else:
                template = template_path
            return render_to_response(template, output, **kwargs)
        return wrapper

    return decorator


def paged(paged_list_name, per_page=20, per_page_var='per_page'):
    """
    Parse page from GET data and pass it to view. Split the
    query set returned from view.
    """
    
    def decorator(func):
        def wrapper(request, *args, **kwargs):
            result = func(request, *args, **kwargs)
            if not isinstance(result, dict):
                return result
            try:
                page = int(request.GET.get('page', 1))
            except ValueError:
                page = 1

            real_per_page = per_page

            if per_page_var:
                try:
                    value = int(request.GET[per_page_var])
                except (ValueError, KeyError):
                    pass
                else:
                    if value > 0:
                        real_per_page = value

            from django.core.paginator import Paginator, EmptyPage
            paginator = Paginator(result['paged_qs'], real_per_page)
            try:
                result[paged_list_name] = paginator.page(page).object_list
            except EmptyPage:
                raise Http404()
            result['page'] = page
            result['pages'] = paginator.num_pages
            result['per_page'] = real_per_page
            return result
        return wrapper

    return decorator


def ajax(func):
    """
    Wrap response of view into JSON format.

    Checks request.method is POST. Return error in JSON in other case.
    """

    def wrapper(request, *args, **kwargs):
        if request.method == 'POST':
            try:
                response = func(request, *args, **kwargs)
            except Exception, ex:
                response = {'error': traceback.format_exc()}
        else:
            response = {'error': {'type': 403, 'message': 'Accepts only POST request'}}
        if isinstance(response, dict):
            return HttpResponseJson(response)
        else:
            return response
    return wrapper
