"""Formulaire local illustrant les widgets Tkinter."""

import tkinter as tk
from tkinter import ttk


def valider_identifiant(texte):
    return bool(texte.strip())


def valider_format(valeur):
    return valeur in ("csv", "json")


def main():
    root = tk.Tk()
    root.title("Contrôle des lots")
    cadre = ttk.Frame(root, padding=12)
    cadre.grid(row=0, column=0)

    identifiant = tk.StringVar(value="")
    inclure_rejets = tk.BooleanVar(value=False)
    format_export = tk.StringVar(value="csv")
    statut = tk.StringVar(value="Lot en attente")

    ttk.Label(cadre, text="Identifiant du lot").grid(row=0, column=0, sticky="w")
    ttk.Entry(cadre, textvariable=identifiant).grid(row=0, column=1)
    ttk.Checkbutton(cadre, text="Inclure les rejets", variable=inclure_rejets).grid(row=1, column=0, columnspan=2, sticky="w")
    ttk.Radiobutton(cadre, text="CSV", variable=format_export, value="csv").grid(row=2, column=0)
    ttk.Radiobutton(cadre, text="JSON", variable=format_export, value="json").grid(row=2, column=1)

    liste = tk.Listbox(cadre, height=2)
    liste.insert(tk.END, "lot-01")
    liste.insert(tk.END, "lot-02")
    liste.grid(row=3, column=0, columnspan=2)

    def utiliser_selection():
        selection = liste.curselection()
        if not selection:
            statut.set("Sélectionne un lot dans la liste")
            return
        identifiant.set(liste.get(selection[0]))
        statut.set("Lot sélectionné")

    def valider():
        if not valider_identifiant(identifiant.get()):
            statut.set("Saisis un identifiant de lot")
        elif not valider_format(format_export.get()):
            statut.set("Choisis un format valide")
        else:
            choix_rejets = "option activée" if inclure_rejets.get() else "option désactivée"
            statut.set(f"Lot prêt : {identifiant.get().strip()} ({format_export.get()}) ; rejets : {choix_rejets}")

    ttk.Label(cadre, textvariable=statut).grid(row=4, column=0, columnspan=2)
    ttk.Button(cadre, text="Valider", command=valider).grid(row=5, column=0)
    ttk.Button(cadre, text="Fermer", command=root.destroy).grid(row=5, column=1)
    ttk.Button(cadre, text="Utiliser la sélection", command=utiliser_selection).grid(row=6, column=0, columnspan=2)
    root.mainloop()


if __name__ == "__main__":
    main()
