from django import forms
from django.forms import PasswordInput
from django.contrib.auth import authenticate
from django.contrib.auth.models import User

import pdb

class AccountForm(forms.Form):
	old_password = forms.CharField(label='Old password', max_length=500, widget=PasswordInput())
	new_password = forms.CharField(label='New password', max_length=500, widget=PasswordInput())
	confirm_password = forms.CharField(label='Confirm new password', max_length=500, widget=PasswordInput())
	
	def __init__(self, user, *args, **kwargs):
		self.user = user
		super(AccountForm, self).__init__(*args, **kwargs)

	
	def clean(self):
		#pdb.set_trace()
		
		old_password = self.cleaned_data.get('old_password')
		new_password = self.cleaned_data.get('new_password')
		confirm_password = self.cleaned_data.get('confirm_password')
		
		if(not self.user.check_password(old_password)):
			raise forms.ValidationError("Old password is invalid")
			
		if(confirm_password != new_password):
			raise forms.ValidationError("Confirm password doesn't match new password") 
	
class SessionForm(forms.Form):
	name=forms.CharField(label='Name', max_length=100, required = True)		
	description = forms.CharField(label='Description', max_length=5000, required = False) 

class AOIForm(forms.Form):
	aoi = forms.ImageField(label='Select an image', widget=forms.FileInput(attrs={'class': 'buttonb'})) 

class AOIUpdateForm(forms.Form):
	duration = forms.CharField(label='Duration', max_length=100, required = True)	
	
class LoginForm(forms.Form):
	username = forms.CharField(label='Username', max_length=500)
	password = forms.CharField(label='Password', max_length=500, widget=PasswordInput())

	def clean(self):
		username = self.cleaned_data.get('username')
		password = self.cleaned_data.get('password')
		user = authenticate(username=username, password=password)
		if not user or not user.is_active:
			raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
		return self.cleaned_data

	def authenticate(self, request):
		username = self.cleaned_data.get('username')
		password = self.cleaned_data.get('password')
		user = authenticate(username=username, password=password)
		return user	
	
class RegisterForm(forms.Form):
	username = forms.CharField(label='Username', max_length=500)
	password = forms.CharField(label='Password', max_length=500, widget=PasswordInput())
	first_name = forms.CharField(label='First Name', max_length=500)
	last_name = forms.CharField(label='Last Name', max_length=500)
	email = forms.EmailField(label='E-mail', max_length=500)
	
	def clean_username(self):
		username = self.cleaned_data.get('username')
		try:
			User.objects.get(username__iexact=username)
		except User.DoesNotExist:
			return username
		raise forms.ValidationError("This username already exists.")
		
	def clean_email(self):
		email = self.cleaned_data.get('email')
		try:
			User.objects.get(email__iexact=email)
		except User.DoesNotExist:
			return email
		raise forms.ValidationError("This email already exists.")