package de.thpeetz.kontor.comics.views;

import lombok.*;
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.ComicConstants;
import de.thpeetz.kontor.comics.data.Worktype;
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.WORKTYPE_ROUTE, layout = MainLayout.class)
@PageTitle("Worktype | Comics | Kontor")
public class WorktypeView extends VerticalLayout {

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

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

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

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

    private void configureForm() {
        form = new WorktypeForm();
        form.setWidth("45em");
        form.setVisible(false);
        form.addSaveListener(this::saveWorktype);
        form.addDeleteListener(this::deleteWorktype);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveWorktype(WorktypeForm.SaveEvent event) {
        service.saveWorktype(event.getWorktype());
        updateList();
        closeEditor();
    }

    private void deleteWorktype(WorktypeForm.DeleteEvent event) {
        service.deleteWorktype(event.getWorktype());
        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 addWorktypeButton = new Button("Add worktype");
        addWorktypeButton.addClickListener(click -> addWorktype());

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

    public void editWorktype(Worktype worktype) {
        if (worktype == null) {
            closeEditor();
        } else {
            form.setWorktype(worktype);
            form.setComicWorks(worktype.getComicWorks());
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addWorktype() {
        grid.asSingleSelect().clear();
        editWorktype(new Worktype());
    }

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