package de.thpeetz.kontor.comics.views;

import de.thpeetz.kontor.comics.data.Issue;
import de.thpeetz.kontor.comics.data.IssueWork;
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.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.services.ComicService;
import de.thpeetz.kontor.common.views.MainLayout;
import jakarta.annotation.security.PermitAll;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = ComicConstants.ISSUEWORK_ROUTE, layout = MainLayout.class)
@PageTitle("IssueWork | Comics | Kontor")
public class IssueWorkView extends VerticalLayout {

    @Getter
    Grid<IssueWork> grid = new Grid<>(IssueWork.class);
    @Getter
    IssueWorkForm form;
    ComicService service;

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

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

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

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

    private void saveIssueWork(IssueWorkForm.SaveEvent event) {
        service.saveIssueWork(event.getIssueWork());
        updateList();
        closeEditor();
    }

    private void deleteIssueWork(IssueWorkForm.DeleteEvent event) {
        service.deleteIssueWork(event.getIssueWork());
        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 addIssueButton = new Button("Add IssueWork");
        addIssueButton.addClickListener(click -> addIssueWork());

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

    public void editIssueWork(IssueWork issueWork) {
        if (issueWork == null) {
            closeEditor();
        } else {
            form.setIssueWork(issueWork);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addIssueWork() {
        grid.asSingleSelect().clear();
        editIssueWork(new IssueWork());
    }

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