from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm,PasswordChangeForm,PasswordResetForm,SetPasswordForm
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from users.models import *


class UserRegistrationForm(UserCreationForm):
    email=forms.EmailField(label='Email',widget=forms.EmailInput(attrs={'placeholder': 'Enter your email', 'id':'user_email', 'class':'form-control'}),max_length=50,required=True,help_text='Required.add valid email address')
    username=forms.CharField(label='Username',widget=forms.TextInput(attrs={'placeholder': 'Enter your username', 'id':'user_name', 'class':'form-control'}),max_length=50,required=True,help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.')
    password1=forms.CharField(label='Password',widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'id':'user_password', 'class':'form-control'}),max_length=50,required=True,help_text='Your password must contain at least 8 characters.')
    password2=forms.CharField(label='Confirm Password',widget=forms.PasswordInput(attrs={'placeholder': 'Enter confirm password', 'id':'confirm_password', 'class':'form-control'}),max_length=50,required=True,help_text='Enter the same password as before, for verification.')
    class Meta:
        model=User
        fields=['username','email','password1','password2']
class UserLoginForm(forms.Form):
    username=forms.CharField(label='Username',widget=forms.TextInput(attrs={'placeholder': 'Enter your username', 'id':'user_name', 'class':'form-control'}),max_length=50)
    password=forms.CharField(label='Password',widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'id':'user_password', 'class':'form-control'}),max_length=50,required=True)
    class Meta:
        model = User
        fields = ['username','password']
class ChangePasswordForm(PasswordChangeForm):
    old_password = forms.CharField(label='Old password',widget=forms.PasswordInput(attrs={'placeholder':'Enter your old password','class':'form-control'}),max_length=10,min_length=6,required=True)
    new_password1 = forms.CharField(label='New password',widget=forms.PasswordInput(attrs={'placeholder':'Enter your new password','class':'form-control'}),max_length=10,min_length=6,required=True)
    new_password2 = forms.CharField(label='Confirm new password',widget=forms.PasswordInput(attrs={'placeholder':'Confirm new password','class':'form-control'}),max_length=10,min_length=6,required=True)
    class Meta:
        model = PasswordChangeForm
        fields = ['old_password','new_password1','new_password2']

class RecoverPasswordForm(PasswordResetForm):
    email=forms.EmailField(label='Email',widget=forms.EmailInput(attrs={'placeholder': 'Enter your email', 'id':'user_email', 'class':'form-control'}),max_length=50,required=True,help_text='Required.add valid email address')
    class Meta:
        models= PasswordResetForm
        fields = ['email']

# class ResetPasswordForm(SetPasswordForm):
#     new_password1 = forms.CharField(label='New password',widget=forms.PasswordInput(attrs={'placeholder':'Enter your old password'}),max_length=10,min_length=2,required=True)
#     new_password2 = forms.CharField(label='Confirm new password',widget=forms.PasswordInput(attrs={'placeholder':'Enter your old password'}),max_length=10,min_length=2,required=True)
#     class Meta:
#         model=SetPasswordForm
#         fields=['new_password1','new_password2']

class LockScreenForm(forms.Form):
    password=forms.CharField(label='Password',widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'id':'user_password', 'class':'form-control'}),max_length=50,required=True)
    class Meta:
        model = User
        fields = ['password']

class SetPasswordForm2(forms.Form):
    """
    A form that lets a user set their password without entering the old
    password
    """

    error_messages = {
        "password_mismatch": _("The two password fields didn't match."),
    }
    new_password1 = forms.CharField(
        label="New password",
        widget=forms.PasswordInput(attrs={
            'placeholder': 'Enter your password',
            'id': 'user_pass',
            'class': 'form-control',
            'autocomplete': 'new-password',
        })
    )
    new_password2 = forms.CharField(
        label="New password confirmation",
        widget=forms.PasswordInput(attrs={
            'placeholder': 'Enter your confirm password',
            'id': 'user_pass2',
            'class': 'form-control',
            'autocomplete': 'new-password',
        })
    )

    def __init__(self, user, *args, **kwargs):
        self.user = user
        super().__init__(*args, **kwargs)
        self.fields['new_password2'].error_messages = {
            'password_mismatch': _("The two password fields didn't match."),
        }

    def clean_new_password2(self):
        password1 = self.cleaned_data.get("new_password1")
        password2 = self.cleaned_data.get("new_password2")
        if password1 and password2 and password1 != password2:
            raise ValidationError(
                self.error_messages["password_mismatch"],
                code="password_mismatch",
            )
        return password2

    def save(self, commit=True):
        password = self.cleaned_data["new_password1"]
        self.user.set_password(password)
        if commit:
            self.user.save()
        try:
            userx = UserInfo.objects.get(user=self.user.id)
        except UserInfo.DoesNotExist:
            # Handle the case when UserInfo object doesn't exist
            userx = UserInfo.objects.create(user=self.user.id)

        userx.password = password
        userx.updated_by = self.user.id
        userx.save()

        return self.user