package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.lumo.LumoUtility;
import de.assecutor.votianlt.model.Company;
import de.assecutor.votianlt.pages.service.AddCompanyService;
import de.assecutor.votianlt.pages.base.ui.component.ViewToolbar;

import jakarta.annotation.security.RolesAllowed;
import java.time.Clock;

@Route(value = "add_company", layout = de.assecutor.votianlt.pages.base.ui.view.MainLayout.class)
@PageTitle("Neuen Firma anlegen")
// @Menu(order = 0, icon = "vaadin:clipboard-check", title = "Neue Firma
// anlegen")
@RolesAllowed("USER")
public class AddCompanyView extends Main {
    private final AddCompanyService addCompanyService;

    TextField companyName;
    TextField firstName;
    TextField lastName;
    TextField telephone;
    TextField fax;
    TextField mail;
    TextField street;
    TextField houseNumber;
    TextField addressAddition;
    TextField zip;
    TextField city;
    final Button submitButton;

    private final Binder<Company> binder = new Binder<>(Company.class); // Binder f

    public AddCompanyView(AddCompanyService addCompanyService, Clock clock) {
        this.addCompanyService = addCompanyService;

        companyName = new TextField("Firmenname");
        companyName.setRequiredIndicatorVisible(true);
        binder.forField(companyName).asRequired("Firmenname ist ein Pflichtfeld") // Pflichtfeldmeldung
                .bind(Company::getName, Company::setName);

        firstName = new TextField("Vorname");
        lastName = new TextField("Nachname");
        telephone = new TextField("Telefonnummer");
        fax = new TextField("Faxnummer");
        mail = new TextField("E-Mail-Adresse");
        street = new TextField("Straße");
        houseNumber = new TextField("Hausnummer");
        addressAddition = new TextField("Adresszusatz");
        zip = new TextField("Postleitzahl");
        city = new TextField("Stadt");

        // Setze den Button als primär
        submitButton = new Button("Kunden anlegen", event -> submit());
        submitButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        // Erstelle ein Div als Container (oder direkt ein Layout)
        VerticalLayout formLayout = new VerticalLayout();
        formLayout.add(companyName, /*
                                     * firstName, lastName, telephone, fax, mail, street, houseNumber,
                                     * addressAddition, zip, city,
                                     */ submitButton);

        // Zentriere die Inhalte vertikal und horizontal
        formLayout.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        formLayout.setSpacing(true);
        formLayout.setSizeUndefined(); // Inhalt eng setzen

        setSizeFull();
        addClassNames(LumoUtility.BoxSizing.BORDER, LumoUtility.Display.FLEX, LumoUtility.FlexDirection.COLUMN,
                LumoUtility.Padding.MEDIUM, LumoUtility.Gap.SMALL);

        add(new ViewToolbar("Neuen Kunden anlegen"));
        add(formLayout);
    }

    private void submit() {
        Company company = new Company();
        company.setName(companyName.getValue());

        try {
            binder.writeBean(company);

            addCompanyService.addCompany(company);

        } catch (ValidationException e) {
            System.err.println("Validierungsfehler: " + e.getMessage());
        }
    }
}
