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.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.common.views.ColumnToggleContextMenu;
import de.thpeetz.kontor.common.views.MainLayout;
import de.thpeetz.kontor.media.MediaConstants;
import de.thpeetz.kontor.media.data.MediaActor;
import de.thpeetz.kontor.media.data.MediaFile;
import de.thpeetz.kontor.media.services.MediaFileService;
import jakarta.annotation.security.PermitAll;
import lombok.Getter;
import org.springframework.context.annotation.Scope;

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

    @Getter
    Grid<MediaActor> grid = new Grid<>(MediaActor.class, false);
    Grid.Column<MediaActor> idColumn = grid.addColumn(MediaActor::getId)
            .setHeader("ID").setResizable(true).setSortable(true);
    Grid.Column<MediaActor> createdColumn = grid.addColumn(MediaActor::getCreatedDate)
            .setHeader("Erstellt").setResizable(true).setSortable(true);
    Grid.Column<MediaActor> modifiedColumn = grid.addColumn(MediaActor::getLastModifiedDate)
            .setHeader("Geändert").setResizable(true).setSortable(true);
    Grid.Column<MediaActor> nameColumn = grid.addColumn(MediaActor::getName)
            .setHeader("Name").setResizable(true).setSortable(true);
    Grid.Column<MediaActor> urlColumn = grid.addColumn(MediaActor::getUrl)
            .setHeader("URL").setResizable(true).setSortable(true);
    TextField filterText = new TextField();
    @Getter
    MediaActorForm form;
    MediaFileService service;

    public MediaActorView(MediaFileService service) {
        this.service = service;
        addClassName("media-actor-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

    private void configureGrid() {
        grid.addClassName("media-actor-grid");
        grid.setSizeFull();
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editMediaActor(event.getValue()));
    }

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

    private void saveMediaActor(MediaActorForm.SaveEvent event) {
        service.saveMediaActor(event.getMediaActor());
        updateList();
        closeEditor();
    }

    private void deleteMediaActor(MediaActorForm.DeleteEvent event) {
        service.deleteMediaActor(event.getMediaActor());
        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 addMediaActorButton = new Button("Add actor");
        addMediaActorButton.addClickListener(click -> addMediaActor());

        Button menuButton = new Button("Show/Hide Columns");
        menuButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
        ColumnToggleContextMenu<MediaActor> columnToggleContextMenu = new ColumnToggleContextMenu<>(menuButton);
        columnToggleContextMenu.addColumnToggleItem(idColumn);
        columnToggleContextMenu.addColumnToggleItem(createdColumn);
        columnToggleContextMenu.addColumnToggleItem(modifiedColumn);
        columnToggleContextMenu.addColumnToggleItem(nameColumn);
        columnToggleContextMenu.addColumnToggleItem(urlColumn);
        HorizontalLayout toolbar = new HorizontalLayout(filterText, addMediaActorButton, menuButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editMediaActor(MediaActor mediaActor) {
        if (mediaActor == null) {
            closeEditor();
        } else {
            form.setMediaActor(mediaActor);
            form.setMediaActorFiles(mediaActor.getMediaActorFiles());
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addMediaActor() {
        grid.asSingleSelect().clear();
        editMediaActor(new MediaActor());
    }

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