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.HasDynamicTitle;
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)
// @Menu(order = 0, icon = "vaadin:clipboard-check", title = "Neue Firma
// anlegen")
@RolesAllowed("USER")
public class AddCompanyView extends Main implements HasDynamicTitle {
    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(getTranslation("profile.company"));
        companyName.setRequiredIndicatorVisible(true);
        binder.forField(companyName).asRequired(getTranslation("profile.validation.company.required"))
                .bind(Company::getName, Company::setName);

        firstName = new TextField(getTranslation("profile.firstname"));
        lastName = new TextField(getTranslation("profile.lastname"));
        telephone = new TextField(getTranslation("profile.phone"));
        fax = new TextField(getTranslation("profile.fax"));
        mail = new TextField(getTranslation("profile.email"));
        street = new TextField(getTranslation("profile.street"));
        houseNumber = new TextField(getTranslation("profile.housenr"));
        addressAddition = new TextField(getTranslation("profile.addressadd"));
        zip = new TextField(getTranslation("profile.zip"));
        city = new TextField(getTranslation("profile.city"));

        // Setze den Button als primär
        submitButton = new Button(getTranslation("addcompany.button.submit"), 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.setWidthFull();
        formLayout.setMaxWidth("620px");
        formLayout.addClassNames("form-shell", "form-card");

        setSizeFull();
        addClassNames(LumoUtility.BoxSizing.BORDER, LumoUtility.Display.FLEX, LumoUtility.FlexDirection.COLUMN,
                LumoUtility.Padding.MEDIUM, LumoUtility.Gap.SMALL);
        addClassName("form-page");

        add(new ViewToolbar(getTranslation("addcompany.title")));
        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());
        }
    }

    @Override
    public String getPageTitle() {
        return getTranslation("page.title.company.create");
    }
}
