""" """

from flask_wtf import FlaskForm
from wtforms import (
    StringField,
    SelectField,
    RadioField,
    TextAreaField,
    validators,
    SubmitField,
)
from app.scodoc.sco_utils import EtatAssiduite


class EditAssiForm(FlaskForm):
    """
    Formulaire de modification d'une assiduité
    """

    def __init__(self, *args, **kwargs):
        "Init form, adding a filed for our error messages"
        super().__init__(*args, **kwargs)
        self.ok = True
        self.error_messages: list[str] = []  # used to report our errors

    def set_error(self, err_msg, field=None):
        "Set error message both in form and field"
        self.ok = False
        self.error_messages.append(err_msg)
        if field:
            field.errors.append(err_msg)

    def disable_all(self):
        "Disable all fields"
        for field in self:
            field.render_kw = {"disabled": True}

    assi_etat = RadioField(
        "État:",
        choices=[
            (EtatAssiduite.ABSENT.value, EtatAssiduite.ABSENT.version_lisible()),
            (EtatAssiduite.RETARD.value, EtatAssiduite.RETARD.version_lisible()),
            (EtatAssiduite.PRESENT.value, EtatAssiduite.PRESENT.version_lisible()),
        ],
        default="absent",
        validators=[
            validators.DataRequired("spécifiez le type d'évènement à signaler"),
        ],
    )
    modimpl = SelectField(
        "Module",
        choices={},  # will be populated dynamically
    )
    description = TextAreaField(
        "Description",
        render_kw={
            "id": "description",
            "cols": 75,
            "rows": 4,
            "maxlength": 500,
        },
    )
    date_debut = StringField(
        "Date de début",
        validators=[validators.Length(max=10)],
        render_kw={
            "class": "datepicker",
            "size": 10,
            "id": "assi_date_debut",
        },
    )
    heure_debut = StringField(
        "Heure début",
        default="",
        validators=[validators.Length(max=5)],
        render_kw={
            "class": "timepicker",
            "size": 5,
            "id": "assi_heure_debut",
        },
    )
    heure_fin = StringField(
        "Heure fin",
        default="",
        validators=[validators.Length(max=5)],
        render_kw={
            "class": "timepicker",
            "size": 5,
            "id": "assi_heure_fin",
        },
    )
    date_fin = StringField(
        "Date de fin",
        validators=[validators.Length(max=10)],
        render_kw={
            "class": "datepicker",
            "size": 10,
            "id": "assi_date_fin",
        },
    )
    entry_date = StringField(
        "Date de dépôt ou saisie",
        validators=[validators.Length(max=10)],
        render_kw={
            "class": "datepicker",
            "size": 10,
            "id": "entry_date",
        },
    )
    entry_time = StringField(
        "Heure dépôt",
        default="",
        validators=[validators.Length(max=5)],
        render_kw={
            "class": "timepicker",
            "size": 5,
            "id": "assi_heure_fin",
        },
    )

    submit = SubmitField("Enregistrer")
    cancel = SubmitField("Annuler", render_kw={"formnovalidate": True})