import datetime
from django import forms
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, HttpResponseRedirect
from django_mako_plus.controller import view_function
from .. import dmp_render, dmp_render_to_response

from catalog.models import Event, Venue, Area

##
# Show
#
@view_function
def process_request(request):
    events = Event.objects.all()

    template_vars = {
        'events' : events,
    }

    return dmp_render_to_response(request, 'events.show.html', template_vars)



##
# Create
#
@view_function
@permission_required('catalog.add_event', login_url='/homepage/index/')
def new(request):
    form = CreateEventForm(request.POST or None)

    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/catalog/events/')

    template_vars = {
        'form' : form
    }

    return dmp_render_to_response(request, 'events.new.html', template_vars)

class CreateEventForm(forms.ModelForm):
    class Meta:
        model = Event
        fields = '__all__'

    name = forms.CharField(label='Name', required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
    description = forms.CharField(label='Description', required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
    venue = forms.ModelChoiceField(label='Venue', required=True, empty_label=None, queryset=Venue.objects.all(), widget=forms.Select(attrs={'class' : 'form-control'}))
    start_date = forms.DateField(required=True, input_formats=['%Y-%m-%d'], widget=forms.TextInput(attrs={'class':'form-control date-time-picker'}))
    end_date = forms.DateField(required=True, input_formats=['%Y-%m-%d'], widget=forms.TextInput(attrs={'class':'form-control date-time-picker'}))


##
# Edit
#
@view_function
@permission_required('catalog.change_event', login_url='/homepage/index/')
def edit(request):
    try:
        event = Event.objects.get(id=request.urlparams[0])
    except Event.DoesNotExist:
        return HttpResponseRedirect('/catalog/events/')

    form = EditEventForm(request.POST or None, instance=event)

    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/catalog/events.edit/%i' % event.id)

    template_vars = {
        'event' : event,
        'form' : form,
    }

    return dmp_render_to_response(request, 'events.edit.html', template_vars)

class EditEventForm(forms.ModelForm):
    class Meta:
        model = Event
        fields = '__all__'

    name = forms.CharField(label='Name', required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
    description = forms.CharField(label='Description', required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
    venue = forms.ModelChoiceField(label='Venue', required=True, empty_label=None, queryset=Venue.objects.all(), widget=forms.Select(attrs={'class' : 'form-control'}))
    start_date = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control date-time-picker'}))
    end_date = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control date-time-picker'}))




##
# Delete
#
@view_function
@permission_required('catalog.delete_event', login_url='/homepage/index/')
def delete(request):
    try:
        event = Event.objects.get(id=request.urlparams[0])
    except Event.DoesNotExist:
        return HttpResponseRedirect('/catalog/events/')

    event.delete()

    return HttpResponseRedirect('/catalog/events/')


##
# Add Area
#
@view_function
@permission_required('catalog.add_area', login_url='/homepage/index/')
def add_area(request):
    event = Event.objects.get(id=request.urlparams[0])

    form = CreateAreaForm(request.POST or None)

    if form.is_valid():
        a = Area()
        a.name = form.cleaned_data.get('name')
        a.description = form.cleaned_data.get('description')
        a.place_number = form.cleaned_data.get('place_number')
        a.event = event
        a.save()
        return HttpResponseRedirect('/catalog/events.edit/%i' % event.id)

    template_vars = {
        'event': event,
        'form' : form
    }

    return dmp_render_to_response(request, 'areas.new.html', template_vars)

class CreateAreaForm(forms.Form):
    name = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control'}))
    description = forms.CharField(required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
    place_number = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'class' : 'form-control'}))
