from flask_wtf import FlaskForm
from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
                     ValidationError)
from wtforms.validators import DataRequired, Email, EqualTo


class EditEmailForm(FlaskForm):
    email = StringField('New email', validators=[Email(), DataRequired()])
    save_email = SubmitField('Save email')


class EditGeneralSettingsForm(FlaskForm):
    dark_mode = BooleanField('Dark mode')
    save_settings = SubmitField('Save settings')


class EditPasswordForm(FlaskForm):
    current_password = PasswordField('Current password',
                                     validators=[DataRequired()])
    password = PasswordField(
        'New password',
        validators=[DataRequired(), EqualTo('password_confirmation',
                                            message='Passwords must match.')]
    )
    password_confirmation = PasswordField(
        'Password confirmation',
        validators=[DataRequired(),
                    EqualTo('password', message='Passwords must match.')]
    )
    save_password = SubmitField('Save password')

    def __init__(self, user, *args, **kwargs):
        super(EditPasswordForm, self).__init__(*args, **kwargs)
        self.user = user

    def validate_current_password(self, field):
        if not self.user.verify_password(field.data):
            raise ValidationError('Invalid password.')