package de.thpeetz.kontor.bookshelf.views;

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.bookshelf.BookshelfConstants;
import de.thpeetz.kontor.bookshelf.data.Article;
import de.thpeetz.kontor.bookshelf.services.BookshelfService;
import de.thpeetz.kontor.common.views.MainLayout;
import jakarta.annotation.security.PermitAll;
import lombok.Getter;
import org.springframework.context.annotation.Scope;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = BookshelfConstants.ARTICLE_ROUTE, layout = MainLayout.class)
@PageTitle("Article | Bookshelf | Kontor")
public class ArticleView extends VerticalLayout {

    @Getter
    Grid<Article> grid = new Grid<>(Article.class);
    TextField filterText = new TextField();
    @Getter
    ArticleForm form;
    BookshelfService service;

    public ArticleView(BookshelfService service) {
        this.service = service;
        addClassName("article-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

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

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

    private void saveArticle(ArticleForm.SaveEvent event) {
        service.saveArticle(event.getArticle());
        updateList();
        closeEditor();
    }

    private void deleteArticle(ArticleForm.DeleteEvent event) {
        service.deleteArticle(event.getArticle());
        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 title or isbn...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList());

        Button addArticleButton = new Button("Add article");
        addArticleButton.addClickListener(click -> addArticle());

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

    public void editArticle(Article article) {
        if (article == null) {
            closeEditor();
        } else {
            form.setArticle(article);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addArticle() {
        grid.asSingleSelect().clear();
        editArticle(new Article());
    }

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