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

"""
Created on 04.10.2011

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

from . import models

USER_MODEL = get_user_model()


class HanchanForm(forms.ModelForm):
    """ Form to add/edit mahjong hanchans, grouped with formsets. """
    error_css_class = 'error'
    required_css_class = 'required'
    start = forms.SplitDateTimeField(label=_('start'), required=True)

    class Meta(object):
        """ Medadata for better usability of the HanchanForm """
        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):
        """ Overwrites some standard widgets for better usability. """
        super(HanchanForm, self).__init__(*args, **kwargs)
        player_queryset = USER_MODEL.objects.filter(
            is_active=True,
            membership=True)
        for i in range(1, 4):
            player = 'player%d' % i
            player_input_score = 'player%d_input_score' % i
            self.fields[player_input_score].widget.attrs['size'] = 6
            self.fields[player_input_score].widget.attrs['type'] = 'number'
            self.fields[player].queryset = player_queryset


class HanchanAdminForm(HanchanForm):
    """ Extends the HanchanForm for users with admin privileges.

    They are allowed to confirm/unconfirm Hanchans, this could be userful if
    one games smells fishy and needs the opinion of an referee."""

    class Meta(object):
        """ Extend the formfields to add the confirmed checkbox. """
        model = models.Hanchan
        fields = HanchanForm.Meta.fields + ('confirmed',)
