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.HasDynamicTitle;
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")
public class FormView extends VerticalLayout implements HasDynamicTitle {

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

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

    @Override
    public String getPageTitle() {
        return getTranslation("page.form");
    }

    private Component buildForm() {
        TextField vorname = new TextField(getTranslation("form.firstName"));
vorname.setAutofocus(true);
        TextField nachname = new TextField(getTranslation("form.lastName"));
        EmailField email = new EmailField(getTranslation("form.email"));
        PasswordField passwort = new PasswordField(getTranslation("form.password"));
        //<theme-editor-local-classname>
        passwort.addClassName("form-view-password-field-1");
        IntegerField alter = new IntegerField(getTranslation("form.age"));
        alter.setReadOnly(true);
        BigDecimalField gehalt = new BigDecimalField(getTranslation("form.salary"));
        DatePicker geburtsdatum = new DatePicker(getTranslation("form.birthdate"));
        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(getTranslation("form.today"))
                .setCancel(getTranslation("form.cancel")));
        TimePicker uhrzeit = new TimePicker(getTranslation("form.time"));
        uhrzeit.setLocale(Locale.GERMANY);
        uhrzeit.setI18n(new TimePicker.TimePickerI18n()
                .setBadInputErrorMessage(getTranslation("form.time.badInput"))
                .setRequiredErrorMessage(getTranslation("form.time.required"))
                .setMinErrorMessage(getTranslation("form.time.min"))
                .setMaxErrorMessage(getTranslation("form.time.max")));

        ComboBox<String> land = new ComboBox<>(getTranslation("form.country"));
        land.setItems(getTranslation("country.de"), getTranslation("country.at"),
                getTranslation("country.ch"), getTranslation("country.fr"), getTranslation("country.nl"));

        Select<String> abteilung = new Select<>();
        abteilung.setLabel(getTranslation("form.department"));
        abteilung.setItems(getTranslation("dept.sales"), getTranslation("dept.marketing"),
                getTranslation("dept.dev"), getTranslation("dept.support"), getTranslation("dept.management"));

        CheckboxGroup<String> interessen = new CheckboxGroup<>();
        interessen.setLabel(getTranslation("form.interests"));
        interessen.setItems(getTranslation("interest.newsletter"), getTranslation("interest.webinars"),
                getTranslation("interest.updates"), getTranslation("interest.events"));

        RadioButtonGroup<String> geschlecht = new RadioButtonGroup<>();
        geschlecht.setLabel(getTranslation("form.salutation"));
        geschlecht.setItems(getTranslation("salutation.mrs"), getTranslation("salutation.mr"),
                getTranslation("salutation.diverse"), getTranslation("salutation.none"));

        Checkbox newsletter = new Checkbox(getTranslation("form.newsletter"));

        TextArea bemerkung = new TextArea(getTranslation("form.remark"));
        bemerkung.setMaxLength(500);

        binder.forField(vorname)
                .asRequired(getTranslation("valid.firstNameRequired"))
                .bind(Registration::getVorname, Registration::setVorname);
        binder.forField(nachname)
                .asRequired(getTranslation("valid.lastNameRequired"))
                .bind(Registration::getNachname, Registration::setNachname);
        binder.forField(email)
                .asRequired(getTranslation("valid.emailRequired"))
                .withValidator(new EmailValidator(getTranslation("valid.emailInvalid")))
                .bind(Registration::getEmail, Registration::setEmail);
        binder.forField(passwort)
                .bind(Registration::getPasswort, Registration::setPasswort);
        binder.forField(alter)
                .withValidator(new IntegerRangeValidator(getTranslation("valid.ageRange"), 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(getTranslation("form.save"), e -> {
            Registration ziel = new Registration();
            if (binder.writeBeanIfValid(ziel)) {
                Notification erfolg = Notification.show(
                        getTranslation("notify.saved", ziel.getVorname(), ziel.getNachname()));
                erfolg.addThemeVariants(NotificationVariant.LUMO_SUCCESS);
            } else {
                Notification fehler = Notification.show(getTranslation("notify.checkInput"));
                fehler.addThemeVariants(NotificationVariant.LUMO_ERROR);
            }
        });
        speichern.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        Button zuruecksetzen = new Button(getTranslation("form.reset"),
                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;
    }
}
