package de.thpeetz.kontor.comics.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.comics.ComicConstants;
import de.thpeetz.kontor.comics.data.TradePaperback;
import de.thpeetz.kontor.comics.services.ComicService;
import de.thpeetz.kontor.common.views.MainLayout;
import jakarta.annotation.security.PermitAll;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = ComicConstants.TPB_ROUTE, layout = MainLayout.class)
@PageTitle("TradePaperBack | Comics | Kontor")
public class TradePaperbackView extends VerticalLayout {

    Grid<TradePaperback> grid = new Grid<>(TradePaperback.class);
    TextField filterText = new TextField();
    TradePaperBackForm form;
    ComicService service;

    public TradePaperbackView(ComicService service) {
        this.service = service;
        addClassName("tradepaperback-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

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

    private void configureGrid() {
        grid.addClassName("tradepaperback-grid");
        grid.setSizeFull();
        grid.setColumns("name", "comic.title", "issueStart", "issueEnd");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editTradePaperBack(event.getValue()));
    }

    public TradePaperBackForm getForm() {
        return form;
    }

    private void configureForm() {
        form = new TradePaperBackForm(service.findAllComics(null));
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveTradePaperBack);
        form.addDeleteListener(this::deleteTradePaperBack);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveTradePaperBack(TradePaperBackForm.SaveEvent event) {
        service.saveTradePaperBack(event.getTradePaperBack());
        updateList();
        closeEditor();
    }

    private void deleteTradePaperBack(TradePaperBackForm.DeleteEvent event) {
        service.deleteTradePaperBack(event.getTradePaperBack());
        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 addTradePaperBackButton = new Button("Add TradePaperBack");
        addTradePaperBackButton.addClickListener(click -> addTradePaperBack());

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

    public void editTradePaperBack(TradePaperback tradepaperback) {
        if (tradepaperback == null) {
            closeEditor();
        } else {
            form.setTradePaperBack(tradepaperback);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addTradePaperBack() {
        grid.asSingleSelect().clear();
        editTradePaperBack(new TradePaperback());
    }

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