package de.thpeetz.kontor.comics.views;

import lombok.Getter;
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.data.Artist;
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 = "artist", layout = MainLayout.class)
@PageTitle("Artist | Comics | Kontor")
public class ArtistView extends VerticalLayout {

    @Getter
    Grid<Artist> grid = new Grid<>(Artist.class);
    TextField filterText = new TextField();
    @Getter
    ArtistForm form;
    ComicService service;

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

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

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

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

    private void saveArtist(ArtistForm.SaveEvent event) {
        service.saveArtist(event.getArtist());
        updateList();
        closeEditor();
    }

    private void deleteArtist(ArtistForm.DeleteEvent event) {
        service.deleteArtist(event.getArtist());
        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 addArtistButton = new Button("Add artist");
        addArtistButton.addClickListener(click -> addArtist());

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

    public void editArtist(Artist artist) {
        if (artist == null) {
            closeEditor();
        } else {
            form.setArtist(artist);
            form.setComicWorks(artist.getComicWorks());
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addArtist() {
        grid.asSingleSelect().clear();
        editArtist(new Artist());
    }

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