from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from authy.models import Profile

def ForbiddenUsers(value):
    forbidden_user = ['admin' ,'css' ,'js','authenticate','login','adminstrator','root','email','user','join','sql','static','python','delete']
    if value.lower() in forbidden_user:
        raise ValidationError("This is reverse word.")
    
def InvalidUser(value):
    if '@' in value or '+' in value or '-' in value:
        raise ValidationError("This is an Invalid user , Do not use these chars: @,-,+")

def UniqueEmail(value):
    if User.objects.filter(email__iexact = value).exists():
        raise ValidationError("User with this email already exists.")

def UniqueUser(value):
    if User.objects.filter(username__iexact = value).exists():
        raise ValidationError("User with this username already exists.")


class SignUpForm(forms.ModelForm):
    first_name       = forms.CharField(widget  = forms.TextInput(), max_length = 30 , required = True)
    last_name        = forms.CharField(widget  = forms.TextInput(), max_length = 30 , required = False)
    username         = forms.CharField(widget  = forms.TextInput(), max_length = 30 , required = True)
    email            = forms.CharField(widget  = forms.EmailInput(), max_length = 100 , required = True)
    password         = forms.CharField(widget  = forms.PasswordInput())
    confirm_password = forms.CharField(widget  = forms.PasswordInput(), required= True)
    
    class Meta:

        model = User
        fields = ('first_name','last_name','username','email','password')

    def __init__(self, *args , **kwargs):
        super(SignUpForm, self).__init__(*args , **kwargs)
        self.fields['username'].validators.append(ForbiddenUsers)
        self.fields['username'].validators.append(InvalidUser)
        self.fields['username'].validators.append(UniqueUser)
        self.fields['email'].validators.append(UniqueEmail)

    def clean(self):
        super(SignUpForm , self).clean()
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')

        if password != confirm_password:
            self.errors['password'] = self.error_class(['Passwords do not match . Try again'])
        return self.cleaned_data


class ChangePasswordForm(forms.ModelForm):
    id               = forms.CharField(widget = forms.HiddenInput())
    old_password     = forms.CharField(widget = forms.PasswordInput() , label = "Old password", required=True)
    new_password     = forms.CharField(widget = forms.PasswordInput(), label = "New Password" , required=True)
    confirm_password = forms.CharField(widget = forms.PasswordInput(),label = "Confirm new password" , required=True)

    class Meta:
        model = User
        fields  = ('id', 'old_password' ,'new_password','confirm_password')

    def clean(self):
        super(ChangePasswordForm,self).clean()
        id = self.cleaned_data.get('id')
        old_password = self.cleaned_data.get('old_password')
        new_password = self.cleaned_data.get('new_password')
        confirm_password = self.cleaned_data.get('confirm_password')
        user = User.objects.get(pk = id)

        if not user.check_password(old_password):
            self._errors['old_password'] = self.error_class(['Old password do not match'])
        
        if new_password != confirm_password:
            self._errors['new_password'] = self.error_class(['Password do not match'])

        return self.cleaned_data

class EditProfileForm(forms.ModelForm):
    picture = forms.ImageField(required=False)
    first_name = forms.CharField(widget= forms.TextInput() , max_length=50 , required=False)
    last_name = forms.CharField(widget= forms.TextInput() , max_length=50 , required=False)
    location = forms.CharField(widget= forms.TextInput() , max_length=20 , required=False)
    url = forms.URLField(widget= forms.TextInput() , max_length=50 , required=False)
    profile_info = forms.CharField(widget= forms.TextInput() , max_length=200 , required=False)

    class Meta:
        model = Profile
        fields = ('picture','first_name','last_name','location','url','profile_info')