from flask_wtf import Form
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import Required, Length, Email, Regexp, EqualTo
from wtforms import ValidationError
from ..models import User, Teacher, Student


class LoginForm(Form):
    email = StringField('请输入邮箱地址', validators=[Required(), Length(1, 64),
                                             Email()])
    password = PasswordField('请输入密码', validators=[Required()])
    remember_me = BooleanField('保持登录状态')
    submit = SubmitField('登录')


class RegistrationForm(Form):
    email = StringField('邮箱地址', validators=[Required(), Length(1, 64),
                                           Email()])
    username = StringField('用户名', validators=[
        Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                                          'Usernames must have only letters, '
                                          'numbers, dots or underscores')])
    password = PasswordField('密码', validators=[
        Required(), EqualTo('password2', message='两次输入不一致')])
    password2 = PasswordField('再次输入密码', validators=[Required()])
    submit = SubmitField('注册')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱地址已经被使用')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已经被使用')


class RegistrationTeacherForm(Form):
    email = StringField('邮箱地址', validators=[Required(), Length(1, 64),
                                           Email()])
    username = StringField('用户名', validators=[
        Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                                          'Usernames must have only letters, '
                                          'numbers, dots or underscores')])
    teacher_name = StringField('教师真实姓名', validators=[Required()])
    password = PasswordField('密码', validators=[
        Required(), EqualTo('password2', message='两次输入不一致')])
    password2 = PasswordField('再次输入密码', validators=[Required()])


    submit = SubmitField('注册')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱地址已经被使用')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已经被使用')


    def validate_teacher_name(self, field):
        if User.query.filter_by(teacher_name=field.data).first():
            raise ValidationError('教师:%s已经被绑定，请先解绑或者换一个名字。' %field.data)
        if not Teacher.query.filter_by(name=field.data).first():
            raise ValidationError('系统内没有名为：%s的教师，请先联系管理员将此教师录入系统后再注册账号。' %field.data)


class RegistrationStudentForm(Form):
    email = StringField('邮箱地址', validators=[Required(), Length(1, 64),
                                           Email()])
    username = StringField('用户名', validators=[
        Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                                          'Usernames must have only letters, '
                                          'numbers, dots or underscores')])
    student_name = StringField('学生真实姓名', validators=[Required()])
    password = PasswordField('密码', validators=[
        Required(), EqualTo('password2', message='两次输入不一致')])
    password2 = PasswordField('再次输入密码', validators=[Required()])


    submit = SubmitField('注册')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱地址已经被使用')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已经被使用')


    def validate_student_name(self, field):
        if User.query.filter_by(student_name=field.data).first():
            raise ValidationError('学生:%s已经被绑定，请先解绑或者换一个名字。比如：%s2' %(field.data,field.data))
        if not Student.query.filter_by(name=field.data).first():
            raise ValidationError('系统内没有名为：%s的学生，请先联系管理员将此学生录入系统后再注册账号。' %field.data)


class ChangePasswordForm(Form):
    old_password = PasswordField('旧密码', validators=[Required()])
    password = PasswordField('新密码', validators=[
        Required(), EqualTo('password2', message='Passwords must match')])
    password2 = PasswordField('再次输入密码', validators=[Required()])
    submit = SubmitField('更新密码')


class PasswordResetRequestForm(Form):
    email = StringField('Email', validators=[Required(), Length(1, 64),
                                             Email()])
    submit = SubmitField('Reset Password')


class PasswordResetForm(Form):
    email = StringField('Email', validators=[Required(), Length(1, 64),
                                             Email()])
    password = PasswordField('New Password', validators=[
        Required(), EqualTo('password2', message='Passwords must match')])
    password2 = PasswordField('Confirm password', validators=[Required()])
    submit = SubmitField('Reset Password')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first() is None:
            raise ValidationError('Unknown email address.')


class ChangeEmailForm(Form):
    email = StringField('New Email', validators=[Required(), Length(1, 64),
                                                 Email()])
    password = PasswordField('Password', validators=[Required()])
    submit = SubmitField('Update Email Address')

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