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.checkbox.Checkbox;
import com.vaadin.flow.component.formlayout.FormLayout;
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.MediaArticle;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MediaArticleForm extends FormLayout {

    TextField url = new TextField("URL");
    TextField title = new TextField("Title");
    Checkbox review = new Checkbox("Review");

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

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

    public MediaArticleForm() {
        addClassName("mediaarticle-form");
        binder.bindInstanceFields(this);
        add(url, 2);
        add(title, 2);
        add(review, 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 MediaArticleForm.DeleteEvent(this, binder.getBean())));
        close.addClickListener(event -> fireEvent(new MediaArticleForm.CloseEvent(this)));

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

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

    public void setMediaArticle(MediaArticle mediaArticle) {
        binder.setBean(mediaArticle);
    }

    public abstract static class MediaArticleFormEvent extends ComponentEvent<MediaArticleForm> {
        private MediaArticle mediaArticle;

        protected MediaArticleFormEvent(MediaArticleForm source, MediaArticle mediaArticle) {
            super(source, false);
            this.mediaArticle = mediaArticle;
        }

        public MediaArticle getMediaArticle() {
            return mediaArticle;
        }
    }

    public static class SaveEvent extends MediaArticleForm.MediaArticleFormEvent {
        SaveEvent(MediaArticleForm source, MediaArticle mediaArticle) {
            super(source, mediaArticle);
        }
    }

    public static class DeleteEvent extends MediaArticleForm.MediaArticleFormEvent {
        DeleteEvent(MediaArticleForm source, MediaArticle mediaArticle) {
            super(source, mediaArticle);
        }
    }

    public static class CloseEvent extends MediaArticleForm.MediaArticleFormEvent {
        CloseEvent(MediaArticleForm source) {
            super(source, null);
        }
    }

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

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

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