import datetime
from flask_wtf import FlaskForm
from wtforms import (
    StringField,
    TextAreaField,
    DateTimeField,
    HiddenField,
    PasswordField,
)
from wtforms.validators import DataRequired, ValidationError, Email, EqualTo


class NullableDateTimeField(DateTimeField):
    """Modify DateField to allow for Null values"""

    def process_formdata(self, valuelist):
        # Bypasses wtForms validation for blank datetime field.
        if valuelist:
            date_str = " ".join(valuelist).strip()
            if date_str == "":
                self.data = None
                return
            try:
                self.data = datetime.datetime.strptime(date_str, self.format)
            except ValueError:
                self.data = None
                raise ValueError(self.gettext("Not a valid date value"))


class SearchForm(FlaskForm):
    search = StringField("Search", validators=[DataRequired()])


class ReviewForm(FlaskForm):
    rating = HiddenField("Rating", validators=[DataRequired()])
    review_title = StringField("Headline")
    review_content = TextAreaField("Review")
    date_started = NullableDateTimeField("Date Started", format="%m/%d/%Y")
    date_finished = NullableDateTimeField("Date Finished", format="%m/%d/%Y")

    def validate_date_finished(self, date_finished):
        if self.date_started.data and date_finished.data:
            if self.date_started.data > date_finished.data:
                print("Date finished must be greater than or equal to date started")
                raise ValidationError(
                    "Date finished must be greater than or equal to date started."
                )

        elif self.date_started.data or date_finished.data:
            print("missing date")
            raise ValidationError("If setting read dates, both dates are required.")


class EditProfileForm(FlaskForm):
    display_name = StringField("Name", validators=[])
    email = StringField("Email", validators=[Email(message="Invalid Email Address.")])
    password = PasswordField(
        "Password",
        validators=[EqualTo("confirm_password", message="Passwords must match.")],
    )
    confirm_password = PasswordField("Confirm Password", validators=[])

[((36, 19, 36, 42), 'wtforms.StringField', 'StringField', ({(36, 31, 36, 41): '"""Headline"""'}, {}), "('Headline')", False, 'from wtforms import StringField, TextAreaField, DateTimeField, HiddenField, PasswordField\n'), ((37, 21, 37, 44), 'wtforms.TextAreaField', 'TextAreaField', ({(37, 35, 37, 43): '"""Review"""'}, {}), "('Review')", False, 'from wtforms import StringField, TextAreaField, DateTimeField, HiddenField, PasswordField\n'), ((55, 19, 55, 53), 'wtforms.StringField', 'StringField', (), '', False, 'from wtforms import StringField, TextAreaField, DateTimeField, HiddenField, PasswordField\n'), ((61, 23, 61, 71), 'wtforms.PasswordField', 'PasswordField', (), '', False, 'from wtforms import StringField, TextAreaField, DateTimeField, HiddenField, PasswordField\n'), ((24, 28, 24, 77), 'datetime.datetime.strptime', 'datetime.datetime.strptime', ({(24, 55, 24, 63): 'date_str', (24, 65, 24, 76): 'self.format'}, {}), '(date_str, self.format)', False, 'import datetime\n'), ((31, 47, 31, 61), 'wtforms.validators.DataRequired', 'DataRequired', ({}, {}), '()', False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n'), ((35, 47, 35, 61), 'wtforms.validators.DataRequired', 'DataRequired', ({}, {}), '()', False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n'), ((45, 22, 47, 17), 'wtforms.validators.ValidationError', 'ValidationError', ({(46, 20, 46, 82): '"""Date finished must be greater than or equal to date started."""'}, {}), "('Date finished must be greater than or equal to date started.')", False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n'), ((51, 18, 51, 84), 'wtforms.validators.ValidationError', 'ValidationError', ({(51, 34, 51, 83): '"""If setting read dates, both dates are required."""'}, {}), "('If setting read dates, both dates are required.')", False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n'), ((56, 45, 56, 84), 'wtforms.validators.Email', 'Email', (), '', False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n'), ((59, 20, 59, 80), 'wtforms.validators.EqualTo', 'EqualTo', (), '', False, 'from wtforms.validators import DataRequired, ValidationError, Email, EqualTo\n')]