from django import forms
from django.contrib.auth.models import Group
from django.contrib.auth.forms import ReadOnlyPasswordHashField

from .models import Account


class RegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    class Meta:
        model = Account
        fields = ('email', 'name', 'firstname', 'licence', 'club_joueur', 'password')

    name = forms.CharField(
        required=True,
        label='Nom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: uppercase",
            "onblur": "this.value=this.value.toUpperCase()"
        })
    )
    firstname = forms.CharField(
        required=True,
        label='Prénom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: capitalize",
            "onchange": "this.value=capitalize(this.value)"
        })
    )

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user


class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Mot de Passe', widget=forms.PasswordInput)
    password2 = forms.CharField(
        label='Confirmation mot de pâsse', widget=forms.PasswordInput)

    class Meta:
        model = Account
        fields = ('email', 'name', 'firstname', 'licence', 'club_joueur', 'is_staff', 'is_superuser')

    name = forms.CharField(
        required=True,
        label='Nom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: uppercase",
            "onblur": "this.value=this.value.toUpperCase()"
        })
    )
    firstname = forms.CharField(
        required=True,
        label='Prénom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: capitalize",
            "onchange": "this.value=capitalize(this.value)"
        })
    )

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Les mots de passe ne correspondent pas")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = Account
        fields = ('email', 'name', 'firstname', 'licence', 'password',
                  'is_active', 'is_superuser')

    name = forms.CharField(
        required=True,
        label='Nom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: uppercase",
            "onblur": "this.value=this.value.toUpperCase()"
        })
    )
    firstname = forms.CharField(
        required=True,
        label='Prénom',
        widget=forms.TextInput(attrs={
            "style": "text-transform: capitalize",
            "onchange": "this.value=capitalize(this.value)"
        })
    )

    def clean_password(self):
        # Regardless of what the user provides, return the initial value.
        # This is done here, rather than on the field, because the
        # field does not have access to the initial value
        return self.initial["password"]
