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.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.ComicWork;
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.COMICWORK_ROUTE, layout = MainLayout.class)
@PageTitle("ComicWork | Comics | Kontor")
public class ComicWorkView extends VerticalLayout {

    Grid<ComicWork> grid = new Grid<>(ComicWork.class);
    ComicWorkForm form;
    ComicService service;

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

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

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

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

    public ComicWorkForm getForm() {
        return form;
    }

    private void configureForm() {
        form = new ComicWorkForm(service.findAllComics(null), service.findAllArtists(null),
                service.findAllWorktypes(null));
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveComicWork);
        form.addDeleteListener(this::deleteComicWork);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveComicWork(ComicWorkForm.SaveEvent event) {
        service.saveComicWork(event.getComicWork());
        updateList();
        closeEditor();
    }

    private void deleteComicWork(ComicWorkForm.DeleteEvent event) {
        service.deleteComicWork(event.getComicWork());
        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() {
        Button addComicButton = new Button("Add ComicWork");
        addComicButton.addClickListener(click -> addComicWork());

        HorizontalLayout toolbar = new HorizontalLayout(addComicButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editComicWork(ComicWork comicWork) {
        if (comicWork == null) {
            closeEditor();
        } else {
            form.setComicWork(comicWork);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addComicWork() {
        grid.asSingleSelect().clear();
        editComicWork(new ComicWork());
    }

    public void updateList() {
        grid.setItems(service.findAllComicWorks());
    }
}
