package de.thpeetz.kontor.media.views;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.contextmenu.ContextMenu;
import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
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.common.views.MainLayout;
import de.thpeetz.kontor.common.views.ColumnToggleContextMenu;
import de.thpeetz.kontor.common.views.StatusIcon;
import de.thpeetz.kontor.media.MediaConstants;
import de.thpeetz.kontor.media.data.MediaArticle;
import de.thpeetz.kontor.media.services.MediaArticleService;
import jakarta.annotation.security.PermitAll;
import lombok.Getter;
import org.springframework.context.annotation.Scope;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = MediaConstants.MEDIAARTICLE_ROUTE, layout = MainLayout.class)
@PageTitle("MediaArticle | Media | Kontor")
public class MediaArticleView extends VerticalLayout {

    @Getter
    Grid<MediaArticle> grid = new Grid<>(MediaArticle.class, false);
    Grid.Column<MediaArticle> idColumn = grid.addColumn(MediaArticle::getId).setHeader("ID").setResizable(true);
    Grid.Column<MediaArticle> urlColumn = grid.addColumn(MediaArticle::getUrl).setHeader("URL").setResizable(true).setSortable(true);
    Grid.Column<MediaArticle> titleColumn = grid.addColumn(MediaArticle::getTitle).setHeader("Titel").setResizable(true).setSortable(true);
    Grid.Column<MediaArticle> reviewColumn = grid.addComponentColumn(mediaArticle -> StatusIcon.create(mediaArticle.isReview()))
            .setHeader("Überprüfung")
            .setWidth("6rem");
    TextField searchField = new TextField();
    @Getter
    MediaArticleForm form;
    MediaArticleService service;

    public MediaArticleView(MediaArticleService service) {
        this.service = service;
        addClassName("mediaarticle-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

    private void configureGrid() {
        grid.addClassName("mediaarticle-grid");
        grid.setSizeFull();
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        idColumn.setVisible(false);
        grid.asSingleSelect().addValueChangeListener(event -> editMediaArticle(event.getValue()));
    }

    private void configureForm() {
        form = new MediaArticleForm();
        form.setWidth("75em");
        form.setVisible(false);
        form.addSaveListener(this::saveMediaArticle);
        form.addDeleteListener(this::deleteMediaArticle);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveMediaArticle(MediaArticleForm.SaveEvent event) {
        service.saveMediaArticle(event.getMediaArticle());
        updateList();
        closeEditor();
    }

    private void deleteMediaArticle(MediaArticleForm.DeleteEvent event) {
        service.deleteMediaArticle(event.getMediaArticle());
        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() {
        searchField.setPlaceholder("Search");
        searchField.setClearButtonVisible(true);
        searchField.setPrefixComponent(new Icon(VaadinIcon.SEARCH));
        searchField.setValueChangeMode(ValueChangeMode.EAGER);
        searchField.addValueChangeListener(e -> updateList());

        Button addMediaArticleButton = new Button("Add MediaArticle");
        addMediaArticleButton.addClickListener(click -> addMediaArticle());

        Button menuButton = new Button("Show/Hide Columns");
        menuButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
        ColumnToggleContextMenu<MediaArticle> columnToggleContextMenu = new ColumnToggleContextMenu<>(menuButton);
        columnToggleContextMenu.addColumnToggleItem(idColumn);
        columnToggleContextMenu.addColumnToggleItem(urlColumn);
        columnToggleContextMenu.addColumnToggleItem(titleColumn);
        columnToggleContextMenu.addColumnToggleItem(reviewColumn);
        HorizontalLayout toolbar = new HorizontalLayout(searchField, addMediaArticleButton, menuButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editMediaArticle(MediaArticle mediaArticle) {
        if (mediaArticle == null) {
            closeEditor();
        } else {
            form.setMediaArticle(mediaArticle);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addMediaArticle() {
        grid.asSingleSelect().clear();
        editMediaArticle(new MediaArticle());
    }

    public void updateList() {
        grid.setItems(service.findAllMediaArticles(searchField.getValue()));
    }
}
