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

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

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

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

    VolumeForm getForm() {
        return form;
    }

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

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

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

    private void saveVolume(VolumeForm.SaveEvent event) {
        service.saveVolume(event.getVolume());
        updateList();
        closeEditor();
    }

    private void deleteVolume(VolumeForm.DeleteEvent event) {
        service.deleteVolume(event.getVolume());
        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 addVolumeButton = new Button("Add Volume", click -> addVolume());

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

    public void editVolume(Volume volume) {
        if (volume == null) {
            closeEditor();
        } else {
            form.setVolume(volume);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addVolume() {
        grid.asSingleSelect().clear();
        editVolume(new Volume());
    }

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