package de.thpeetz.kontor.media.views;

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.common.views.MainLayout;
import de.thpeetz.kontor.media.MediaConstants;
import de.thpeetz.kontor.media.data.MediaActorFile;
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.MEDIAACTORFILE_ROUTE, layout = MainLayout.class)
@PageTitle("MediaActorFile | Media | Kontor")
public class MediaActorFileView extends VerticalLayout {

    @Getter
    Grid<MediaActorFile> grid = new Grid<>(MediaActorFile.class);
    @Getter
    MediaActorFileForm form;
    MediaFileService service;

    public MediaActorFileView(MediaFileService service) {
        this.service = service;
        addClassName("mediaactorfile-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

    private void configureGrid() {
        grid.addClassName("mediaactorfile-grid");
        grid.setSizeFull();
        grid.setColumns("id", "media_actor.name", "media_file.title");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editMediaActorFile(event.getValue()));
    }

    private void configureForm() {
        form = new MediaActorFileForm(service.findAllMediaFiles(null), service.findAllMediaActors(null));
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveMediaActorFile);
        form.addDeleteListener(this::deleteMediaActorFile);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveMediaActorFile(MediaActorFileForm.SaveEvent event) {
        service.saveMediaActorFile(event.getMediaActorFile());
        updateList();
        closeEditor();
    }

    private void deleteMediaActorFile(MediaActorFileForm.DeleteEvent event) {
        service.deleteMediaActorFile(event.getMediaActorFile());
        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 addMediaActorFileButton = new Button("Add MediaActorFile");
        addMediaActorFileButton.addClickListener(click -> addMediaActorFile());

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

    public void editMediaActorFile(MediaActorFile mediaActorFile) {
        if (mediaActorFile == null) {
            closeEditor();
        } else {
            form.setMediaActorFile(mediaActorFile);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addMediaActorFile() {
        grid.asSingleSelect().clear();
        editMediaActorFile(new MediaActorFile());
    }

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