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

from .models import User


# Create your forms here.

class UserSignInForm(forms.Form):
    """ User Sign In Form """

    email       = forms.EmailField(label='email', max_length=100, widget=forms.EmailInput(attrs={'placeholder': 'Email', 'autocomplete':'off', 'class': 'form-control bg-transparent', 'id':'email'}))
    password    = forms.CharField(label='password', max_length=100, widget=forms.PasswordInput(attrs={'placeholder': 'Password', 'autocomplete':'off', 'class': 'form-control bg-transparent', 'id':'password'}))
    remember_me = forms.BooleanField(label='remember_me', required=False, widget=forms.CheckboxInput( attrs={'class': 'form-control', 'id':'remember_me'}))

    def clean(self):
        email = self.cleaned_data.get('email')
        password = self.cleaned_data.get('password')

        # check if email and password exist in database
        if email and password:
            user = User.objects.filter(email=email).first()
            if not user:
                raise forms.ValidationError(_('Email does not exist'), code='invalid')
            if not user.check_password(password):
                raise forms.ValidationError(_('Password is incorrect'), code='invalid')
            if not user.is_active:
                raise forms.ValidationError(_('User is not active'), code='invalid')
        return super(UserSignInForm, self).clean()

class UserSignUpForm(forms.Form):
    """ User Sign Up Form """
    
    first_name       = forms.CharField(label='first_name', required=False, max_length=100, widget=forms.TextInput(attrs={'placeholder': 'First Name', 'class': 'form-control client-info', 'id':'first_name'}))
    last_name        = forms.CharField(label='last_name',  required=False, max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Last Name', 'class': 'form-control client-info', 'id':'last_name'}))
    email            = forms.EmailField(label='email', max_length=100, widget=forms.EmailInput(attrs={'placeholder': 'Email', 'class': 'form-control client-info', 'id':'email'}))
    password         = forms.CharField(label='password', max_length=100, widget=forms.PasswordInput(attrs={'placeholder': 'Password', 'class': 'form-control client-info', 'id':'password'}))
    password_confirm = forms.CharField(label='password_confirm', max_length=100, widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password', 'class': 'form-control client-info', 'id':'password_confirm'}))

    def clean(self):
        email = self.cleaned_data.get('email')
        password = self.cleaned_data.get('password')
        password_confirm = self.cleaned_data.get('password_confirm')
        # Check if email already exists
        if email:
            user = User.objects.filter(email=email).first()
            print(user)
            if user:
                raise forms.ValidationError('Email already exists')

        # Check if passwords match
        if password != password_confirm:
            raise forms.ValidationError('Passwords don\'t match')

        return super(UserSignUpForm, self).clean()