from flask_wtf import FlaskForm, RecaptchaField

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


class LoginForm(FlaskForm):
    email = EmailField('Електронна Поща', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    password = PasswordField('Парола', [validators.DataRequired(), validators.Length(1,128)])
    remember_me = BooleanField('Запомни ме')
    #recaptcha = RecaptchaField()
    submit = SubmitField('Вход')

class TwoFAForm(FlaskForm):
    token = StringField('Token', [validators.DataRequired(), validators.Length(6, 6)])
    submit = SubmitField('Потвърди кода')

class RegistrationForm(FlaskForm):
    email = StringField('Електронна Поща', [validators.DataRequired(), validators.Length(6,35), validators.Email()])
    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('Грешка. Опитайте пак с друг email адрес.')
    password = PasswordField('Парола', [validators.DataRequired(), validators.EqualTo('confirm', message='Паролите трябва да съвпадат')])
    confirm = PasswordField('Повторете паролата', [validators.DataRequired()])
    accept_tos = BooleanField('Приемам <a href="/terms">Условията за Използване</a> на услугата', [validators.DataRequired()])
    recaptcha = RecaptchaField()
    submit = SubmitField('Регистрация')
    
class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('Стара парола', [validators.DataRequired()])
    password = PasswordField('Нова Парола', [validators.DataRequired(), validators.EqualTo('confirm', message='Паролите трябва да съвпадат')])
    confirm = PasswordField('Повторете паролата')
    submit = SubmitField('Обнови паролата')

class PasswordResetRequestForm(FlaskForm):
    email = EmailField('Електронна Поща', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    recaptcha = RecaptchaField()
    submit = SubmitField('Възстановяване на парола', [validators.DataRequired()])

class PasswordResetForm(FlaskForm):
    email = EmailField('Електронна Поща', [validators.DataRequired(), validators.Length(1,64), validators.Email()])
    password = PasswordField('Парола', [validators.DataRequired(), validators.EqualTo('confirm', message='Паролите трябва да съвпадат')])
    confirm = PasswordField('Повторете паролата', [validators.DataRequired()])
    submit = SubmitField('Промяна на паролата')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first() is None:
            raise ValidationError('Грешка. Опитайте пак с друг email адрес.')

