# Copyright 2009 - Participatory Culture Foundation
# 
# This file is part of Miro Community.
# 
# Miro Community is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# 
# Miro Community is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with Miro Community.  If not, see <http://www.gnu.org/licenses/>.

from django import template
from django.core.urlresolvers import reverse

from localtv import models
from localtv.inline_edit import forms
from localtv.playlists.models import Playlist
from localtv.playlists.forms import PlaylistForm

WIDGET_DIRECTORY = {
    Playlist: {
        'info': {
            'form': PlaylistForm},
        },
    models.Video: {
        'name': {
            'form': forms.VideoNameForm},
        'when_published': {
            'form': forms.VideoWhenPublishedForm},
        'authors': {
            'form': forms.VideoAuthorsForm,
            'render_template':
                'localtv/inline_edit/render_widget_checklist.html'},
        'categories': {
            'form': forms.VideoCategoriesForm,
            'render_template':
                'localtv/inline_edit/render_widget_checklist.html'},
        'tags': {
            'form': forms.VideoTagsForm},
        'description': {
            'form': forms.VideoDescriptionField},
        'website_url': {
            'form': forms.VideoWebsiteUrlField},
        'editors_comment': {
            'form': forms.VideoEditorsComment},
        'thumbnail': {
            'form': forms.VideoThumbnailForm,
            'render_template':
                'localtv/inline_edit/render_widget_thumbnail.html'},
        },
    }

register = template.Library()

def get_display_content(model_instance, field_name,
                        display_template_name=None):
    try:
        widget_data = WIDGET_DIRECTORY[model_instance.__class__][field_name]
    except KeyError:
        return ''
    display_template = template.loader.get_template(
        display_template_name or widget_data.get(
            'default_display_template') or
        'localtv/inline_edit/%s_%s.html' % (
            model_instance._meta.object_name.lower(),
            field_name))
    return display_template.render(
        template.Context(
            {'instance': model_instance}))

@register.simple_tag
def editable_widget(model_instance, field_name, display_template_name=None,
                    form=None):
    try:
        widget_data = WIDGET_DIRECTORY[model_instance.__class__][field_name]
    except KeyError:
        return ''# maybe raise an error here instead saying "no such model or
                 # field could be found"?

    if form is None:
        form = widget_data['form'](instance=model_instance)

    # render the display template
    
    display_content = get_display_content(model_instance, field_name,
                                          display_template_name)


    # render the wrapper template, with display template data intact
    render_template = template.loader.get_template(
        widget_data.get('render_template',
                        'localtv/inline_edit/render_widget.html'))
    
    post_url = reverse(
        widget_data.get('reversible_post_url',
                        'localtv_admin_%s_edit_%s' % (
                model_instance._meta.object_name.lower(),
                field_name)), args=(model_instance.id,))

    return render_template.render(
        template.Context(
            {'instance': model_instance,
             'display_content': display_content,
             'post_url': post_url,
             'form': form}))

@@ -2105,32 +2105,41 @@
 display_content(
+request, 
 model_instance, 
@@ -2647,32 +2647,39 @@
    template.
+Request
 Context(
 %0A           
@@ -2658,32 +2658,40 @@
 .RequestContext(
+request,
 %0A            %7B'i
@@ -2739,16 +2739,36 @@
 mple_tag
+(takes_context=True)
 %0Adef edi
@@ -2780,16 +2780,25 @@
 _widget(
+context, 
 model_in
@@ -2869,32 +2869,65 @@
     form=None):%0A
+    request = context%5B'request'%5D%0A
     try:%0A       
@@ -3301,16 +3301,25 @@
 content(
+request, 
 model_in
@@ -3935,24 +3935,31 @@
 emplate.
+Request
 Context(
 %0A       
@@ -3950,16 +3950,24 @@
 Context(
+request,
 %0A       

Use a RequestContext when rendering editable field widgets