package de.thpeetz.kontor.tysc.views;

import com.vaadin.flow.component.button.*;
import de.thpeetz.kontor.comics.data.*;
import de.thpeetz.kontor.common.views.*;
import lombok.*;
import org.springframework.context.annotation.Scope;

import com.vaadin.flow.component.Component;
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.tysc.TyscConstants;
import de.thpeetz.kontor.tysc.data.CardSet;
import de.thpeetz.kontor.tysc.services.CardService;
import jakarta.annotation.security.PermitAll;

import java.util.*;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = TyscConstants.CARDSET_ROUTE, layout = MainLayout.class)
@PageTitle("CardSet | Tysc | Kontor")
public class CardSetView extends VerticalLayout {

    @Getter
    Grid<CardSet> grid = new Grid<>(CardSet.class, false);
    Grid.Column<CardSet> idColumn = grid.addColumn(CardSet::getId)
            .setHeader("ID").setResizable(true).setSortable(true);
    Grid.Column<CardSet> createdColumn = grid.addColumn(CardSet::getCreatedDate)
            .setHeader("Erstellt").setResizable(true).setSortable(true);
    Grid.Column<CardSet> modifiedColumn = grid.addColumn(CardSet::getLastModifiedDate)
            .setHeader("Geändert").setResizable(true).setSortable(true);
    Grid.Column<CardSet> versionColumn = grid.addColumn(CardSet::getVersion)
            .setHeader("Version").setResizable(true).setSortable(true);
    Grid.Column<CardSet> nameColumn = grid.addColumn(CardSet::getName)
            .setHeader("Comic").setResizable(true).setSortable(true);
    Grid.Column<CardSet> vendorNameColumn = grid.addColumn(CardSet::getVendorName)
            .setHeader("Hersteller").setResizable(true).setSortable(true);
    Grid.Column<CardSet> parallelSetColumn = grid.addComponentColumn(parallelSet -> StatusIcon.create(parallelSet.isParallelSet()))
            .setHeader("Parallelset?").setWidth("6rem").setSortable(true);
    Grid.Column<CardSet> insertSetColumn = grid.addComponentColumn(insertSet -> StatusIcon.create(insertSet.isInsertSet()))
            .setHeader("Parallelset?").setWidth("6rem").setSortable(true);
    TextField filterText = new TextField();
    @Getter
    CardSetForm form;
    CardService service;

    public CardSetView(CardService service) {
        this.service = service;
        addClassName("cardSet-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

    private void configureGrid() {
        grid.addClassName("cardSet-grid");
        grid.setSizeFull();
        //grid.setColumns("name", "vendor.name", "parallelSet", "insertSet");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        idColumn.setVisible(false);
        createdColumn.setVisible(false);
        modifiedColumn.setVisible(false);
        versionColumn.setVisible(false);
        grid.asSingleSelect().addValueChangeListener(event -> editCardSet(event.getValue()));
    }

    private void configureForm() {
        form = new CardSetForm();
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveCardSet);
        form.addDeleteListener(this::deleteCardSet);
        form.addCloseListener(e -> 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 addCardSetButton = new Button("Add cardSet");
        addCardSetButton.addClickListener(click -> addCardSet());

        Button menuButton = new Button("Show/Hide Columns");
        menuButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
        ColumnToggleContextMenu<CardSet> columnToggleContextMenu = new ColumnToggleContextMenu<>(menuButton);
        columnToggleContextMenu.addColumnToggleItem(idColumn);
        columnToggleContextMenu.addColumnToggleItem(createdColumn);
        columnToggleContextMenu.addColumnToggleItem(modifiedColumn);
        columnToggleContextMenu.addColumnToggleItem(versionColumn);
        columnToggleContextMenu.addColumnToggleItem(nameColumn);
        columnToggleContextMenu.addColumnToggleItem(vendorNameColumn);
        columnToggleContextMenu.addColumnToggleItem(parallelSetColumn);
        columnToggleContextMenu.addColumnToggleItem(insertSetColumn);
        HorizontalLayout toolbar = new HorizontalLayout(filterText, addCardSetButton, menuButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    private void saveCardSet(CardSetForm.SaveEvent event) {
        service.saveCardSet(event.getCardSet());
        updateList();
        closeEditor();
    }

    private void deleteCardSet(CardSetForm.DeleteEvent event) {
        service.deleteCardSet(event.getCardSet());
        updateList();
        closeEditor();
    }

    public void editCardSet(CardSet cardSet) {
        if (cardSet == null) {
            closeEditor();
        } else {
            form.setCardSet(cardSet);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addCardSet() {
        grid.asSingleSelect().clear();
        editCardSet(new CardSet());
    }

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