# -*- coding: utf-8 -*-
from django import forms
from cvkbrno.models import Outing,Member,Boat,BoatCategory,Races,MemberWork
from django.contrib.auth.models import User
from django.forms import ModelForm
from models import Outing
from django.contrib.admin import widgets
from django.contrib import admin
from django.utils import timezone,translation
from django.forms.widgets import CheckboxSelectMultiple
from django.forms.models import ModelMultipleChoiceField
from django.utils.translation import ugettext_lazy as _

import datetime

def is_future_date_validator(value):
    if value < timezone.now().date():
        raise forms.ValidationError("{0} is not a future date.".format(value))



# forms here

class CustomSelectMultiple(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s" %(obj.name)

class OutingForm(ModelForm):
    class Meta:
	model = Outing
	fields = ['distance']

	

class OutingCloseForm(ModelForm):
    class Meta:
	model = Outing
	fields = ['distance','comment']
	widgets = {
	    'comment': forms.Textarea,
	    }
	
class DateInput(forms.DateInput):
    input_type = 'date'

class TimeInput(forms.TimeInput):
    input_type = 'time'

class OutingReserveForm(ModelForm):
    startdate = forms.DateField(widget=DateInput(),label = _("Start Date"),
				validators=[is_future_date_validator])
    starttime = forms.TimeField(widget=TimeInput(format='%H:%M'),
				label = _("Start Time"))
    endtime = forms.TimeField(widget=TimeInput(format='%H:%M'),
				label = _("End Time"))
    class Meta:
	model = Outing
	fields = ['distance']

class RacesEditForm(ModelForm):
    startdate = forms.DateField(widget=DateInput(),label = _("Start Date"),
				)
    starttime = forms.TimeField(widget=TimeInput(format='%H:%M'),
				label = _("Start Time"))
    enddate = forms.DateField(widget=DateInput,label = _("End Date"),
				)
    endtime = forms.TimeField(widget=TimeInput(format='%H:%M'),
				label = _("End Time"))


    def clean(self):
	cleaned_data = super(RacesEditForm, self).clean()
	enddate = cleaned_data.get('enddate')
	endtime = cleaned_data.get('endtime')
	startdate = cleaned_data.get('startdate')
	starttime = cleaned_data.get('starttime')
	if startdate and starttime:
	    startdatetime = (str(startdate) + ' ' + str(starttime))
	    startdatetime = datetime.datetime.strptime(startdatetime,"%Y-%m-%d %H:%M:%S")
	    startdatetime = timezone.make_aware(startdatetime)
	    if timezone.now() >= startdatetime:
		msg = _("Start date/time must be in the future")
		self.add_error('startdate', msg)
		self.add_error('starttime', msg)

	if enddate and endtime:
	    enddatetime = (str(enddate) + ' ' + str(endtime))
	    enddatetime = datetime.datetime.strptime(enddatetime,"%Y-%m-%d %H:%M:%S")
	    enddatetime = timezone.make_aware(enddatetime)
	    if startdatetime >= enddatetime:
		msg = _("End date/time must be after Start date/time")
		self.add_error('enddate', msg)
		self.add_error('endtime', msg)

	return cleaned_data

    class Meta:
	model = Races
	fields = ['name','startdate','starttime','enddate','endtime','transportboats']
	widgets = {
	    'transportboats': CheckboxSelectMultiple(),
	    }

class MemberEditForm(ModelForm):
    class Meta:
	model = Member
	fields = ['status','contributions','categories','hoursworked']
	widgets = {
	    'categories': CheckboxSelectMultiple(),
	    }

class UserEditForm(ModelForm):
    email = forms.CharField(required=False)
    class Meta:
	model = User
	fields = ['first_name','last_name','email']

class UserCreateForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())
    email = forms.CharField(required=False)
    class Meta:
	model = User
	fields = ['first_name','last_name','email','username','password']

class PassWordConfirmForm(forms.Form):
    passwordconfirm = forms.CharField(widget=forms.PasswordInput(),
				      label="repeat your password")

class BoatEditForm(ModelForm):
    class Meta:
	model = Boat
	fields = ['boatcode','boatname','categories',
		  'nrseats','status','comment']
	widgets = {
	    'comment': forms.Textarea,
	    'categories': CheckboxSelectMultiple(),
	    }

class BoatCategoryEditForm(ModelForm):
    class Meta:
	model = BoatCategory
	fields = ['name']

class MemberWorkCreateForm(ModelForm):
    class Meta:
	model = MemberWork
	fields = ['date','name','hours','comment','worker']
	widgets = {
	    'date': DateInput(),
	    'comment': forms.Textarea,
	    'worker': CheckboxSelectMultiple(),
	    }
