package com.example.views;

import com.example.components.Card;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.checkbox.CheckboxGroup;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.component.textfield.*;
import com.vaadin.flow.component.timepicker.TimePicker;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.validator.EmailValidator;
import com.vaadin.flow.data.validator.IntegerRangeValidator;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import java.text.DateFormatSymbols;
import java.util.List;
import java.util.Locale;


/**
 * Example view demonstrating a broad set of Vaadin form controls bound
 * to {@link Registration} via {@link Binder}.
 */
@Route("formular")
@PageTitle("Formular")
public class FormView extends VerticalLayout {

    private final Binder<Registration> binder = new Binder<>(Registration.class);

    public FormView() {
        addClassName("dialect-content");
        add(new Card("Registrierung", buildForm()));
        //<theme-editor-local-classname>
        addClassName("form-view-vertical-layout-1");
    }

    private Component buildForm() {
        TextField vorname = new TextField("Vorname");
vorname.setAutofocus(true);
        TextField nachname = new TextField("Nachname");
        EmailField email = new EmailField("E-Mail");
        PasswordField passwort = new PasswordField("Passwort");
        //<theme-editor-local-classname>
        passwort.addClassName("form-view-password-field-1");
        IntegerField alter = new IntegerField("Alter");
        alter.setReadOnly(true);
        BigDecimalField gehalt = new BigDecimalField("Gehalt (€)");
        DatePicker geburtsdatum = new DatePicker("Geburtsdatum");
        geburtsdatum.setLocale(Locale.GERMANY);

        DateFormatSymbols symbols = new DateFormatSymbols(Locale.GERMANY);
        List<String> monate = List.of(symbols.getMonths()).subList(0, 12);
        List<String> wochentage = List.of(symbols.getWeekdays()).subList(1, 8);
        List<String> wochentageKurz = List.of(symbols.getShortWeekdays()).subList(1, 8);

        geburtsdatum.setI18n(new DatePicker.DatePickerI18n()
                .setMonthNames(monate)
                .setWeekdays(wochentage)
                .setWeekdaysShort(wochentageKurz)
                .setFirstDayOfWeek(1)
                .setToday("Heute")
                .setCancel("Abbrechen"));
        TimePicker uhrzeit = new TimePicker("Bevorzugte Uhrzeit");

        ComboBox<String> land = new ComboBox<>("Land");
        land.setItems("Deutschland", "Österreich", "Schweiz", "Frankreich", "Niederlande");

        Select<String> abteilung = new Select<>();
        abteilung.setLabel("Abteilung");
        abteilung.setItems("Vertrieb", "Marketing", "Entwicklung", "Support", "Geschäftsführung");

        CheckboxGroup<String> interessen = new CheckboxGroup<>();
        interessen.setLabel("Interessen");
        interessen.setItems("Newsletter-Themen", "Webinare", "Produkt-Updates", "Events");

        RadioButtonGroup<String> geschlecht = new RadioButtonGroup<>();
        geschlecht.setLabel("Anrede");
        geschlecht.setItems("Frau", "Herr", "Divers", "Keine Angabe");

        Checkbox newsletter = new Checkbox("Newsletter abonnieren");

        TextArea bemerkung = new TextArea("Bemerkung");
        bemerkung.setMaxLength(500);

        binder.forField(vorname)
                .asRequired("Vorname wird benötigt")
                .bind(Registration::getVorname, Registration::setVorname);
        binder.forField(nachname)
                .asRequired("Nachname wird benötigt")
                .bind(Registration::getNachname, Registration::setNachname);
        binder.forField(email)
                .asRequired("E-Mail wird benötigt")
                .withValidator(new EmailValidator("Ungültige E-Mail-Adresse"))
                .bind(Registration::getEmail, Registration::setEmail);
        binder.forField(passwort)
                .bind(Registration::getPasswort, Registration::setPasswort);
        binder.forField(alter)
                .withValidator(new IntegerRangeValidator("Alter muss zwischen 0 und 120 liegen", 0, 120))
                .bind(Registration::getAlter, Registration::setAlter);
        binder.forField(gehalt)
                .bind(Registration::getGehalt, Registration::setGehalt);
        binder.forField(geburtsdatum)
                .bind(Registration::getGeburtsdatum, Registration::setGeburtsdatum);
        binder.forField(uhrzeit)
                .bind(Registration::getUhrzeit, Registration::setUhrzeit);
        binder.forField(land)
                .bind(Registration::getLand, Registration::setLand);
        binder.forField(abteilung)
                .bind(Registration::getAbteilung, Registration::setAbteilung);
        binder.forField(interessen)
                .bind(Registration::getInteressen, Registration::setInteressen);
        binder.forField(geschlecht)
                .bind(Registration::getGeschlecht, Registration::setGeschlecht);
        binder.forField(newsletter)
                .bind(Registration::isNewsletter, Registration::setNewsletter);
        binder.forField(bemerkung)
                .bind(Registration::getBemerkung, Registration::setBemerkung);

        Registration registration = new Registration();
        registration.setAlter(37);
        binder.readBean(registration);

        FormLayout formLayout = new FormLayout();
        formLayout.setResponsiveSteps(
                new FormLayout.ResponsiveStep("0", 1),
                new FormLayout.ResponsiveStep("500px", 2));
        formLayout.add(vorname, nachname, email, passwort, alter, gehalt,
                geburtsdatum, uhrzeit, land, abteilung);
        formLayout.setColspan(interessen, 2);
        formLayout.setColspan(geschlecht, 2);
        formLayout.setColspan(newsletter, 2);
        formLayout.setColspan(bemerkung, 2);
        formLayout.add(interessen, geschlecht, newsletter, bemerkung);

        Button speichern = new Button("Speichern", e -> {
            Registration ziel = new Registration();
            if (binder.writeBeanIfValid(ziel)) {
                Notification erfolg = Notification.show(
                        "Gespeichert: %s %s".formatted(ziel.getVorname(), ziel.getNachname()));
                erfolg.addThemeVariants(NotificationVariant.LUMO_SUCCESS);
            } else {
                Notification fehler = Notification.show("Bitte Eingaben prüfen");
                fehler.addThemeVariants(NotificationVariant.LUMO_ERROR);
            }
        });
        speichern.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        Button zuruecksetzen = new Button("Zurücksetzen",
                e -> binder.readBean(new Registration()));

        HorizontalLayout buttons = new HorizontalLayout(speichern, zuruecksetzen);

        VerticalLayout wrapper = new VerticalLayout(formLayout, buttons);
        wrapper.setPadding(false);
        wrapper.setSpacing(true);
        return wrapper;
    }
}
