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

from django import forms
from django.conf import settings
import datetime
import pfusers

class ConfirmForm(forms.Form):
    pass

class UserForm(forms.Form):
    name = forms.RegexField(label=u"Nom d'utilisateur (login)", regex='^[a-z0-9\.\-_]+$', min_length=3, max_length=16, required=True,
            widget=forms.TextInput(attrs={'readonly': True, 'size':'16', 'autocomplete':'off', 'class':'span2'}))
    disabled = forms.BooleanField(label=u'Désactivé',required=False)
    password = forms.CharField(label=u"Nouveau mot de passe", min_length=3, max_length=64, required=False, 
            widget=forms.PasswordInput(attrs={'size':'32', 'autocomplete':'off', 'class':'span3'}))
    password2 = forms.CharField(label=u"Mot de passe (vérification)", min_length=3, max_length=64, required=False,
            widget=forms.PasswordInput(attrs={'size':'32', 'autocomplete':'off', 'class':'span3'}))
    expires = forms.DateField(label=u"Date d'expiration", required=False,
            widget=forms.TextInput(attrs={'size':'16', 'class':'span2 datepicker'}))
    descr = forms.CharField(label=u'Description (nom long)', max_length=256, required=False,
            widget=forms.TextInput(attrs={'size':'256', 'class':'span6'}))
    multiple = forms.BooleanField(label=u'Connexions multiples autorisées',required=False)

    def is_valid(self):
        valid = super(UserForm, self).is_valid()
        if valid:
            # check passwords
            password = self.cleaned_data.get('password')
            password2 = self.cleaned_data.get('password2')
            if password != password2:
                self.errors['password'] = [u'Les deux mots de passe doivent être identiques']
                valid = False
        return valid

class NewUserForm(UserForm):
    name = forms.RegexField(label=u"Nom d'utilisateur (login)", regex='^[a-z0-9\.\-_]+$', min_length=3, max_length=16, required=True,
            widget=forms.TextInput(attrs={'size':'16', 'autocomplete':'off', 'class':'span2'}),
            help_text="Uniquement lettres, chiffres, point, trait d'union et «_»")
    # force password and expire fields
    password = forms.CharField(label=u"Mot de passe", min_length=3, max_length=64, required=True,
            widget=forms.PasswordInput(attrs={'size':'32', 'autocomplete':'off', 'class':'span3'}))
    password2 = forms.CharField(label=u"Mot de passe (vérification)", min_length=3, max_length=64, required=True,
            widget=forms.PasswordInput(attrs={'size':'32', 'autocomplete':'off', 'class':'span3'}))
    expires = forms.DateField(label=u"Date d'expiration", required=(settings.IDP_UA_MAX_EXPIRES > 0),
            widget=forms.TextInput(attrs={'size':'16', 'class':'span2 datepicker'}))

    def is_valid(self):
        valid = super(NewUserForm, self).is_valid()
        if valid:
            name = self.cleaned_data.get('name')
            all_pfusers = pfusers.get_all_pfusers()
            if all_pfusers.get(name, None) is not None:
                self.errors['name'] = [u'Un utilisateur avec ce login existe déjà.']
                valid = False
        return valid

