from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.contrib.auth import authenticate


class UserLoginForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'class': 'validate', 'placeholder': 'Nom utilisateur'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Mot de passe'}))

    def clean(self, *args, **kwargs):
        username = self.cleaned_data.get("username")
        password = self.cleaned_data.get("password")

        if username and password:
            user = authenticate(username=username, password=password)
            if not user:
                raise forms.ValidationError("Cet utilisateur n'existe pas!")
            if not user.check_password(password):
                raise forms.ValidationError("Mot de passe incorrect!")
            if not user.is_active:
                raise forms.ValidationError("Cet utilisateur n'est pas actif")
        return super(UserLoginForm, self).clean(*args, **kwargs)


class RegistrationForm(UserCreationForm):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Nom utilisateur'}))
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'placeholder': 'Mot de passe'}))
    password2 = forms.CharField(
        label='Mot de passe de confirmation',
        help_text='Entrez le même mot de passe.',
        widget=forms.PasswordInput(attrs={'placeholder': 'Inserer à nouveau le mot de passe'}))

    class Meta:
        model = User
        fields = ['username', 'password1', '<PASSWORD>', ]

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        if commit:
            user.save()
        return user
