# -*- encoding: utf-8 -*-

"""
Created on 04.10.2011

@author: christian
"""
from django.contrib.auth import get_user_model
import django.forms
from django.utils import timezone
from django.utils.translation import ugettext as _

from utils.html5 import forms
from . import models

USER_MODEL = get_user_model()


class HanchanForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    start = forms.DateTimeField(label=_('start'), required=True)

    class Meta(object):
        model = models.Hanchan
        fields = ( 'start',
                  'player1', 'player1_input_score',
                  'player2', 'player2_input_score',
                  'player3', 'player3_input_score',
                  'player4', 'player4_input_score',
                  'comment')
        widgets = {'event': forms.HiddenInput(),
                   'comment': forms.widgets.Textarea(attrs={'rows': 4, 'cols': 40})
                  }

    def __init__(self, *args, **kwargs):
        super(HanchanForm, self).__init__(*args, **kwargs)
        # self.fields['event'].widget.attrs['disabled'] = True
        for i in xrange(1, 4):
            self.fields['player%d_input_score' % i].widget.attrs['size'] = 6
            self.fields['player%d_input_score' % i].widget.attrs['type'] = 'number'

    '''
    def clean_start(self):
        u"""Das Datum darf nicht in der Zukunft liegen und es muss innerhalb
        der Dauer des Events liegen."""
        start = self.cleaned_data['start']
        event = self.cleaned_data['event']
        if start > timezone.now():
            raise django.forms.ValidationError(
                _("It's not allowed to enter future games."))
        if not event.start <= start <= event.end:
            raise django.forms.ValidationError(
                _("Only games running during this event are allowed."))
        return start
    '''

class HanchanAdminForm(HanchanForm):
    class Meta(object):
        model = models.Hanchan
        fields = HanchanForm.Meta.fields + ('confirmed',)



class SeasonSelectForm(django.forms.Form):
    season = django.forms.ChoiceField(label='', choices=('a', 'b', 'c'))

    def __init__(self, user, *args, **kwargs):
        super(SeasonSelectForm, self).__init__(args, kwargs)
        season_list = models.LadderRanking.objects.filter(user=user)
        season_list = season_list.select_related('user')
        season_list = season_list.values_list('season__id', 'season__name')
        self.fields['season'] = django.forms.ChoiceField(choices=season_list)
