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.combobox.ComboBox;
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.Comic;
import de.thpeetz.kontor.comics.data.StoryArc;
import de.thpeetz.kontor.comics.services.ComicService;
import de.thpeetz.kontor.common.views.MainLayout;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = ComicConstants.STORYARC_ROTE, layout = MainLayout.class)
@PageTitle("StoryArc | Comics | Kontor")
public class StoryArcView extends VerticalLayout {

    Grid<StoryArc> grid = new Grid<>(StoryArc.class);
    ComboBox<Comic> comicFilter = new ComboBox<>("Comic");
    StoryArcForm form;
    ComicService service;

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

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

    StoryArcForm getForm() {
        return form;
    }

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

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

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

    private void saveStoryArc(StoryArcForm.SaveEvent event) {
        service.saveStoryArc(event.getStoryArc());
        updateList();
        closeEditor();
    }

    private void deleteStoryArc(StoryArcForm.DeleteEvent event) {
        service.deleteStoryArc(event.getStoryArc());
        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() {
        comicFilter.setItems(service.findAllComics(null));
        comicFilter.setItemLabelGenerator(Comic::getTitle);
        comicFilter.addValueChangeListener(e -> updateList());
        comicFilter.setClearButtonVisible(true);

        Button addStoryArcButton = new Button("Add StoryArc", click -> addStoryArc());

        HorizontalLayout toolbar = new HorizontalLayout(comicFilter, addStoryArcButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editStoryArc(StoryArc storyArc) {
        if (storyArc == null) {
            closeEditor();
        } else {
            form.setStoryArc(storyArc);
            form.setVisible(true);
            addClassName("editing");
        }
    }

    public void closeEditor() {
        form.setStoryArc(null);
        form.setVisible(false);
        removeClassName("editing");
    }

    private void addStoryArc() {
        grid.asSingleSelect().clear();
        editStoryArc(new StoryArc());
    }

    private void updateList() {
        grid.setItems(service.findAllStoryArcsForComic(comicFilter.getValue()));
    }
}
