from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from wtforms import StringField, SubmitField, ValidationError, IntegerField
from wtforms.validators import InputRequired, Length


class CreateCorpusForm(FlaskForm):
    description = StringField(
        'Description',
        validators=[InputRequired(), Length(max=255)]
    )
    title = StringField('Title', validators=[InputRequired(), Length(max=32)])
    submit = SubmitField()


class CorpusFileBaseForm(FlaskForm):
    author = StringField(
        'Author',
        validators=[InputRequired(), Length(max=255)]
    )
    publishing_year = IntegerField(
        'Publishing year',
        validators=[InputRequired()]
    )
    title = StringField(
        'Title',
        validators=[InputRequired(), Length(max=255)]
    )
    address = StringField('Adress', validators=[Length(max=255)])
    booktitle = StringField('Booktitle', validators=[Length(max=255)])
    chapter = StringField('Chapter', validators=[Length(max=255)])
    editor = StringField('Editor', validators=[Length(max=255)])
    institution = StringField('Institution', validators=[Length(max=255)])
    journal = StringField('Journal', validators=[Length(max=255)])
    pages = StringField('Pages', validators=[Length(max=255)])
    publisher = StringField('Publisher', validators=[Length(max=255)])
    school = StringField('School', validators=[Length(max=255)])
    submit = SubmitField()


class CreateCorpusFileForm(CorpusFileBaseForm):
    vrt = FileField('File', validators=[FileRequired()])

    def validate_vrt(self, field):
        if not field.data.filename.lower().endswith('.vrt'):
            raise ValidationError('VRT files only!')


class EditCorpusFileForm(CorpusFileBaseForm):
    def prefill(self, corpus_file):
        ''' Pre-fill the form with data of an exististing corpus file '''
        self.address.data = corpus_file.address
        self.author.data = corpus_file.author
        self.booktitle.data = corpus_file.booktitle
        self.chapter.data = corpus_file.chapter
        self.editor.data = corpus_file.editor
        self.institution.data = corpus_file.institution
        self.journal.data = corpus_file.journal
        self.pages.data = corpus_file.pages
        self.publisher.data = corpus_file.publisher
        self.publishing_year.data = corpus_file.publishing_year
        self.school.data = corpus_file.school
        self.title.data = corpus_file.title


class ImportCorpusForm(FlaskForm):
    pass
