package de.thpeetz.kontor.media.views;

import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import de.thpeetz.kontor.media.data.MediaActor;
import de.thpeetz.kontor.media.data.MediaActorFile;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class MediaActorForm extends FormLayout {

    TextField name = new TextField("Name");
    TextField url = new TextField("URL");
    Grid<MediaActorFile> mediaActorFiles = new Grid<>(MediaActorFile.class);

    Button save = new Button("Save");
    Button delete = new Button("Delete");
    Button close = new Button("Cancel");

    Binder<MediaActor> binder = new BeanValidationBinder<>(MediaActor.class);

    public MediaActorForm() {
        addClassName("media-actor-form");
        binder.bindInstanceFields(this);

        mediaActorFiles.setColumns("media_file.title");
        mediaActorFiles.getColumnByKey("media_file.title").setHeader("File Title");
        add(name, 2);
        add(url, 2);
        add(mediaActorFiles, 2);
        add(createButtonsLayout());
    }

    private HorizontalLayout createButtonsLayout() {
        save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        delete.addThemeVariants(ButtonVariant.LUMO_ERROR);
        close.addThemeVariants(ButtonVariant.LUMO_TERTIARY);

        save.addClickShortcut(Key.ENTER);
        close.addClickShortcut(Key.ESCAPE);

        save.addClickListener(event -> validateAndSave());
        delete.addClickListener(event -> fireEvent(new MediaActorForm.DeleteEvent(this, binder.getBean())));
        close.addClickListener(event -> fireEvent(new MediaActorForm.CloseEvent(this)));

        binder.addStatusChangeListener(e -> save.setEnabled(binder.isValid()));
        return new HorizontalLayout(save, delete, close);
    }

    private void validateAndSave() {
        if (binder.isValid()) {
            fireEvent(new MediaActorForm.SaveEvent(this, binder.getBean()));
        }
    }

    public void setMediaActor(MediaActor mediaActor) {
        binder.setBean(mediaActor);
    }

    public void setMediaActorFiles(List<MediaActorFile> mediaActorFiles) {
        log.info("Setting mediaActorFiles: {}", mediaActorFiles);
        this.mediaActorFiles.setItems(mediaActorFiles);
    }

    public abstract static class MediaActorFormEvent extends ComponentEvent<MediaActorForm> {
        private MediaActor mediaActor;

        protected MediaActorFormEvent(MediaActorForm source, MediaActor mediaActor) {
            super(source, false);
            this.mediaActor = mediaActor;
        }

        public MediaActor getMediaActor() {
            return mediaActor;
        }
    }

    public static class SaveEvent extends MediaActorForm.MediaActorFormEvent {
        SaveEvent(MediaActorForm source, MediaActor mediaActor) {
            super(source, mediaActor);
        }
    }

    public static class DeleteEvent extends MediaActorForm.MediaActorFormEvent {
        DeleteEvent(MediaActorForm source, MediaActor mediaActor) {
            super(source, mediaActor);
        }
    }

    public static class CloseEvent extends MediaActorForm.MediaActorFormEvent {
        CloseEvent(MediaActorForm source) {
            super(source, null);
        }
    }

    public void addDeleteListener(ComponentEventListener<MediaActorForm.DeleteEvent> listener) {
        addListener(MediaActorForm.DeleteEvent.class, listener);
    }

    public void addSaveListener(ComponentEventListener<MediaActorForm.SaveEvent> listener) {
        addListener(MediaActorForm.SaveEvent.class, listener);
    }

    public void addCloseListener(ComponentEventListener<MediaActorForm.CloseEvent> listener) {
        addListener(MediaActorForm.CloseEvent.class, listener);
    }
}
