from flask import flash
from flask.ext.wtf import Form
from wtforms import TextField, PasswordField, BooleanField, SubmitField
from wtforms.validators import Required, Email, EqualTo
from werkzeug import check_password_hash, generate_password_hash
from toolbox.models import User
from mongoengine.queryset import Q


class ForgotPasswordForm(Form):
    email = TextField('Email address', [Required(), Email()])
    submit = SubmitField('Reset password')

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        if User.objects(email=self.email.data).first() is None:
            flash("This email address is not in the system.")
            return False
        return True


class LoginForm(Form):
    email = TextField('Email address', [Required()])
    password = PasswordField('Password', [Required()])
    submit = SubmitField('Login')

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        try:
            user = User.objects.get(email=self.email.data)
        except User.DoesNotExist:
            flash("This email address is not in the system.")
            return False

        if not user.confirmed:
            flash("Account not yet confirmed. Check your email. ")
            return False

        if not check_password_hash(user.password, self.password.data):
            flash("Invalid password, please try again.")
            return False

        self.user = user
        return True


class ResetPasswordForm(Form):
    password = PasswordField('Password', [Required()])
    confirm = PasswordField('Repeat Password', [
        Required(),
        EqualTo('password', message='Passwords must match')
    ])
    submit = SubmitField('Reset password')

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        if self.password.data == self.confirm.data:
            return True
        return False


class ConfirmForm(Form):
    password = PasswordField('Choose a Password', [Required()])
    confirm = PasswordField('Repeat Password', [
        Required(),
        EqualTo('password', message='Passwords must match')
    ])
    submit = SubmitField('Register')

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        return True
