package de.thpeetz.kontor.tysc.views;

import org.springframework.context.annotation.Scope;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent;

import de.thpeetz.kontor.common.views.MainLayout;
import de.thpeetz.kontor.tysc.TyscConstants;
import de.thpeetz.kontor.tysc.data.Vendor;
import de.thpeetz.kontor.tysc.services.CardService;
import jakarta.annotation.security.PermitAll;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = TyscConstants.VENDOR_ROUTE, layout = MainLayout.class)
@PageTitle("Vendor | Tysc | Kontor")
public class VendorView extends VerticalLayout {

    Grid<Vendor> grid = new Grid<>(Vendor.class);
    TextField filterText = new TextField();
    VendorForm form;
    CardService service;

    public VendorView(CardService service) {
        this.service = service;
        addClassName("vendor-view");
        setSizeFull();
        configureGrid();
        configureForm();

        add(getToolbar(), getContent());
        updateList();
    }

    public Grid<Vendor> getGrid() {
        return grid;
    }

    private void configureGrid() {
        grid.addClassName("vendor-grid");
        grid.setSizeFull();
        grid.setColumns("name");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editVendor(event.getValue()));
    }

    public VendorForm getForm() {
        return form;
    }

    private void configureForm() {
        form = new VendorForm();
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveVendor);
        form.addDeleteListener(this::deleteVendor);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveVendor(VendorForm.SaveEvent event) {
        service.saveVendor(event.getVendor());
        updateList();
        closeEditor();
    }

    private void deleteVendor(VendorForm.DeleteEvent event) {
        service.deleteVendor(event.getVendor());
        updateList();
        closeEditor();
    }

    private Component getContent() {
        HorizontalLayout content = new HorizontalLayout(grid, form);
        content.setFlexGrow(2, grid);
        content.setFlexGrow(1, form);
        content.addClassName("content");
        content.setSizeFull();
        return content;
    }

    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("Filter by name...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList());

        Button addVendorButton = new Button("Add vendor");
        addVendorButton.addClickListener(click -> addVendor());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addVendorButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editVendor(Vendor vendor) {
        if (vendor == null) {
            closeEditor();
        } else {
            form.setVendor(vendor);
            form.setCardSets(vendor.getCardSets());
            form.setVisible(true);
            addClassName("editing");
        }
    }

    private void closeEditor() {
        form.setVendor(null);
        form.setVisible(false);
        removeClassName("editing");
    }

    private void addVendor() {
        grid.asSingleSelect().clear();
        editVendor(new Vendor());
    }

    public void updateList() {
        grid.setItems(service.findAllVendors(filterText.getValue()));
    }
}
