from flask_wtf import FlaskForm, RecaptchaField

from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, DecimalField
from wtforms import validators, ValidationError
from wtforms.fields import EmailField

from models import User

class LoginForm(FlaskForm):
    email = EmailField('E-Mail', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    password = PasswordField('Password', [validators.DataRequired(), validators.Length(1,128)])
    remember_me = BooleanField('Remember me ?')
    #recaptcha = RecaptchaField()
    submit = SubmitField('Login')

class TwoFAForm(FlaskForm):
    token = StringField('Token', [validators.DataRequired(), validators.Length(6, 6)])
    submit = SubmitField('Confirm')

class RegistrationForm(FlaskForm):
    email = StringField('E-Mail', [validators.DataRequired(), validators.Length(6,35), validators.Email()])
    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('Error. Please try again.')
    password = PasswordField('Password', [validators.DataRequired(), validators.EqualTo('confirm', message='Both passwords must be equal')])
    confirm = PasswordField('Your password again', [validators.DataRequired()])
    accept_tos = BooleanField('I accept the <a href="/terms">Terms of Service</a>', [validators.DataRequired()])
    recaptcha = RecaptchaField()
    submit = SubmitField('REGISTER')
    
class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('Old Password', [validators.DataRequired()])
    password = PasswordField('New Password', [validators.DataRequired(), validators.EqualTo('confirm', message='Both passwords must be equal')])
    confirm = PasswordField('Your password again')
    submit = SubmitField('Renew Password')

class PasswordResetRequestForm(FlaskForm):
    email = EmailField('E-Mail', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    recaptcha = RecaptchaField()
    submit = SubmitField('Reset password', [validators.DataRequired()])

class PasswordResetForm(FlaskForm):
    email = EmailField('E-Mail', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    password = PasswordField('Password', [validators.DataRequired(), validators.EqualTo('confirm', message='Both password fields must be equal')])
    confirm = PasswordField('Your password again', [validators.DataRequired()])
    submit = SubmitField('Change password')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first() is None:
            raise ValidationError('Error. Please try again.')

