from django import forms
from models import Project, Content, Media
import uuid
from utils import generate_uuid
from django.forms.widgets import Widget, RadioSelect
from django.contrib.admin import widgets as admin_widgets
from django.core.urlresolvers import reverse
from ldt.settings import BASE_URL
from ldt.forms import widgets as ldt_widgets
from django.utils.translation import ugettext_lazy as _

class LdtImportForm(forms.Form):
    importFile = forms.FileField()
    videoPath = forms.CharField(required=False)
    flatten = forms.BooleanField(required=False, initial=True)   
    
class LdtAddForm(forms.ModelForm):
    title = forms.CharField()
    # contents = forms.ModelMultipleChoiceField(Content.objects.all())
    # owner = forms.ModelChoiceField(Author.objects.all())
    class Meta:
        model = Project
        exclude = ("ldt_id", "ldt", "created_by", "changed_by", "creation_date", "modification_date", "state", "owner")   

class ReindexForm(forms.Form):
    contents = forms.ModelMultipleChoiceField(Content.objects.all())

class SearchForm(forms.Form):
    search = forms.CharField()
    field = forms.ChoiceField([(u"all", u"all"), (u"title", u"title"), (u"abstract", u"resume"), (u"tags", u"tags")])

class AddProjectForm (forms.Form):
    title = forms.CharField(widget=forms.TextInput(attrs={'class':'inputbox required'}))

class CopyProjectForm (forms.Form):
    title = forms.CharField()
    

class ContentForm(forms.ModelForm):

    #iri_id = forms.CharField(max_length=1024, widget=forms.HiddenInput, initial=generate_uuid)
    iriurl = forms.CharField(max_length=1024, widget=forms.HiddenInput, required=False)
    content_creation_date = forms.SplitDateTimeField(widget=ldt_widgets.LdtSplitDateTime, required=False, label=_("content.content_creation_date"))
    media_input_type = forms.ChoiceField(required=False, label=_("content.media_input_type"), choices=(("upload", _("file_upload")), ("url", _("url")), ("link", _("existing_media")), ("create", _("create_media")), ("none", _("none_media"))))
    
    def clean_iri_id(self):
        data = self.cleaned_data.get('iri_id')
        if not data:
            data = generate_uuid()
        return data
    
    def clean(self):
        cleaned_data = self.cleaned_data
        iriurl_data = cleaned_data.get('iriurl')
        iri_id_data = cleaned_data['iri_id']
        if not iriurl_data:
            iriurl_data = "%s/%s.iri" % (iri_id_data, iri_id_data)    
        cleaned_data['iriurl'] = iriurl_data
        return cleaned_data
    
    class Meta:
        model = Content
        exclude = ("creation_date", "update_date", "authors")
        widgets = {
                   'iri_id': forms.HiddenInput,
        }
        
    class Media:
        css = {
            'all' : ('admin/css/forms.css', 'admin/css/base.css', 'admin/css/widgets.css')
               }
        
class MediaForm(forms.ModelForm):
    
    media_creation_date = forms.SplitDateTimeField(widget=ldt_widgets.LdtSplitDateTime, required=False)
    media_file = forms.FileField(required=False);
    src = forms.CharField(required=False, max_length=1024)

    class Meta:
        model = Media
        exclude = ("creation_date", "update_date", "creator")
    
    class Media:
        css = {
            'all' : ('admin/css/forms.css', 'admin/css/base.css', 'admin/css/widgets.css')
               }
