package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Menu;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.lumo.LumoUtility;
import de.assecutor.votianlt.model.Customer;
import de.assecutor.votianlt.pages.base.ui.component.ViewToolbar;
import de.assecutor.votianlt.pages.service.CustomerService;

import java.time.Clock;

import static com.vaadin.flow.spring.data.VaadinSpringDataHelpers.toSpringPageRequest;

@Route(value = "customer", layout = de.assecutor.votianlt.pages.base.ui.view.MainLayout.class)
@PageTitle("Kunden")
@Menu(order = 0, icon = "vaadin:clipboard-check", title = "Kunden")
public class CustomersView extends Main {
    final TextField description;
    final Button createBtn;
    final Grid<Customer> todoGrid;

    public CustomersView(CustomerService todoService, Clock clock) {
        description = new TextField();
        description.setPlaceholder("Suche");
        description.setMaxLength(255);
        description.setMinWidth("20em");

        createBtn = new Button("Kunde anlegen", event -> addCustomer());
        createBtn.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        todoGrid = new Grid<>();
        todoGrid.setItems(query -> todoService.list(toSpringPageRequest(query)).stream());
        todoGrid.addColumn(Customer::getCompanyName).setHeader("Firmenname");
        todoGrid.setSizeFull();

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

        add(new ViewToolbar("Kunden", ViewToolbar.group(description, createBtn)));
        add(todoGrid);
    }

    private void addCustomer() {
        UI.getCurrent().navigate("add_customer");
    }
}
