# -*- encoding: utf-8 -*-
__author__ = 'christian'

from django import forms
from django.utils.translation import ugettext as _

from . import models


class GameForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'

    class Meta(object):
        fields = [
            'player1', 'player1_score',
            'player2', 'player2_score',
            'player3', 'player3_score',
            'player4', 'player4_score',
            'player5', 'player5_score',
            'player6', 'player6_score',
            'comment'
        ]
        model = models.Game

    def clean(self):
        cleaned_data = super(GameForm, self).clean()
        players_in_game = set()
        for player_nr in (
        'player1', 'player2', 'player3', 'player4', 'player5', 'player6'):
            current_player = cleaned_data.get(player_nr)
            if current_player and current_player in players_in_game:
                msg = _("%s may only participate once." % current_player)
                self._errors[player_nr] = self.error_class([msg])
                del cleaned_data[player_nr]
            players_in_game.add(current_player)
        return cleaned_data
