from django import forms
from django.contrib.auth import (
	authenticate, get_user_model,
	login, logout,
	)
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist


User = get_user_model()


class UserLoginForm(forms.Form):
	username = forms.CharField()
	password = forms.CharField(widget=forms.PasswordInput)

	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("This user does not exists")
			if not user.check_password(password):
				raise forms.ValidationError("Incorrect password")
			if not user.is_active:
				raise forms.ValidationError("This user is not longer active")
		return super(UserLoginForm, self).clean(*args, **kwargs)

class UserRegisterForm(forms.Form):
	username = forms.CharField(label="Username", 
		widget=forms.TextInput) 
	email = forms.EmailField(label="Email address")
	password1 = forms.CharField(widget=forms.PasswordInput, label="Password")
	password2 = forms.CharField(widget=forms.PasswordInput, label="Confirm password")
	
	
	def clean_password2(self):
		if 'password1' in self.cleaned_data:
			password1 = self.cleaned_data['password1']
			password2 = self.cleaned_data['password2']
			if password1 == password2:
				return password2
			raise forms.ValidationError('Password do not match.')
	
	def clean_username(self):
		username = self.cleaned_data['username']
		#if not re.search(r'^w+$', username):
		#	raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
		try:
			User.objects.get(username = username)
		except ObjectDoesNotExist:
			return username
		raise forms.ValidationError('Username is already taken.')		



	