from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import DataRequired,Email,EqualTo
from wtforms import ValidationError
from flask_wtf.file import FileAllowed,FileField
from flask_login import current_user
from CodeCompany.models import User

#########################################################

class LoginForm(FlaskForm):

    email=StringField("Email",validators=[DataRequired(),Email()])
    password=PasswordField("Password",validators=[DataRequired()])
    submit=SubmitField("Login")

#########################################################

class RegistrationForm(FlaskForm):

    email=StringField("Email",validators=[DataRequired(),Email()])
    username=StringField("Username",validators=[DataRequired()])
    password=PasswordField("Password",validators=[DataRequired(),EqualTo("confirm_password",message="Password must match")])
    confirm_password=PasswordField("Confirm Passsword",validators=[DataRequired()])
    submit=SubmitField("Register")

    def check_email(self,field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError("Email is already registered")

    def check_username(self,field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError("Username already exits")

#####################################################################

class UpdateUserForm(FlaskForm):

    email = StringField("Email", validators=[DataRequired(), Email()])
    username = StringField("Username", validators=[DataRequired()])
    picture=FileField("Upload Profile Picture",validators=[FileAllowed(["jpg","png"])])
    submit=SubmitField("Update")

    def check_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError("Email is already registered")

    def check_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError("Username already exits")