from flask_wtf import FlaskForm
# what kind of files are allowed to be uploaded
from flask_wtf.file import FileField, FileAllowed, FileRequired
# now we can use this for the account update
from flask_login import current_user
from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField, SelectField, HiddenField, validators, IntegerField, RadioField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError, InputRequired
from models import *  # you forgot this and it took forever to notice the mistake!!!


class LoginForm(FlaskForm):
    studentID = StringField('Student ID', validators=[
                            DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')

class RegistrationForm(FlaskForm):

    username = StringField ('Name in English', 
                                validators=[DataRequired(), Length(min=2, max=20)])    
    studentID = StringField ('Student ID', 
                                validators=[DataRequired()])
    email = StringField('Email', 
                                validators=[DataRequired(), Email()] )  
    device = RadioField('Main Phone Device', 
                                choices = [('Apple', 'Apple iphone'), ('Android', 'Android Phone')])                                
    password = PasswordField('Password', 
                                validators=[DataRequired()] )
    confirm_password = PasswordField('Confirm Password',  
                                        validators=[DataRequired(), EqualTo('password')] )
    submit = SubmitField('Join')

    def validate_username(self, username):  # the field is username
        user = User.query.filter_by(username=username.data).first()  #User was imported at the top # first means just find first instance?
        if user:  # meaning if True
            raise ValidationError('Another student has that username, please add family name')  # ValidationError needs to be imported from wtforms
    
    def validate_email(self, email): 
        user = User.query.filter_by(email=email.data).first()  
        if user:  
            raise ValidationError('That email has an account already, did you forget your password?') 

    def validate_studentID(self, studentID): 
        try:
            int(studentID.data)
        except:
            raise ValidationError('9 numbers; no S') 
        user = User.query.filter_by(studentID=studentID.data).first()  
        if user:           
            raise ValidationError('That student ID already has an account, did you forget your password?')  