from project.main.user import User
from project.main.validator import ValidPassword
from flask_wtf import FlaskForm
from flask_login import current_user
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError

class LoginForm(FlaskForm):
    user_name  = StringField('Username', validators=[DataRequired()], render_kw={"placeholder": "john_doe"})
    password = PasswordField('Password', validators=[DataRequired(), ValidPassword], render_kw={"placeholder": "password"})
    submit = SubmitField('Sign In')

class RegistrationForm(FlaskForm):
    user_name = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)], render_kw={"placeholder": "john_doe"})
    email = StringField('Email Address', validators=[DataRequired(), Length(min=2, max=120), Email()], render_kw={"placeholder": "<EMAIL>"})
    password = PasswordField('Password', validators=[DataRequired()], render_kw={"placeholder": "password"})
    submit = SubmitField('Sign Up')


    def validate_username(self, user_name):
        user = User.query.filter_by(user_name=user_name.data).first()
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')


    def validate_email_address(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('That email address is taken. Please choose a different one.')

from project.main.user import User
from project.main.validator import ValidPassword
from flask_wtf import FlaskForm
from flask_login import current_user
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError

class LoginForm(FlaskForm):
    user_name  = StringField('Username', validators=[DataRequired()], render_kw={"placeholder": "john_doe"})
    password = PasswordField('Password', validators=[DataRequired(), ValidPassword], render_kw={"placeholder": "password"})
    submit = SubmitField('Sign In')

class RegistrationForm(FlaskForm):
    user_name = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)], render_kw={"placeholder": "john_doe"})
    email = StringField('Email Address', validators=[DataRequired(), Length(min=2, max=120), Email()], render_kw={"placeholder": "<EMAIL>"})
    password = PasswordField('Password', validators=[DataRequired()], render_kw={"placeholder": "password"})
    submit = SubmitField('Sign Up')


    def validate_username(self, user_name):
        user = User.query.filter_by(user_name=user_name.data).first()
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')


    def validate_email_address(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('That email address is taken. Please choose a different one.')
