package de.thpeetz.kontor.bookshelf.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.bookshelf.data.Article;
import de.thpeetz.kontor.bookshelf.data.Author;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ArticleForm extends FormLayout {

    TextField title = new TextField("Title");
    Grid<Author> author = new Grid<>(Author.class);

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

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

    public ArticleForm() {
        addClassName("article-form");
        binder.bindInstanceFields(this);

        add(title, author, 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 ArticleForm.DeleteEvent(this, binder.getBean())));
        close.addClickListener(event -> fireEvent(new ArticleForm.CloseEvent(this)));

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

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

    public void setArticle(Article article) {
        binder.setBean(article);
    }

    public abstract static class ArticleFormEvent extends ComponentEvent<ArticleForm> {
        private Article article;

        protected ArticleFormEvent(ArticleForm source, Article article) {
            super(source, false);
            this.article = article;
        }

        public Article getArticle() {
            return article;
        }
    }

    public static class SaveEvent extends ArticleForm.ArticleFormEvent {
        SaveEvent(ArticleForm source, Article article) {
            super(source, article);
        }
    }

    public static class DeleteEvent extends ArticleForm.ArticleFormEvent {
        DeleteEvent(ArticleForm source, Article article) {
            super(source, article);
        }
    }

    public static class CloseEvent extends ArticleForm.ArticleFormEvent {
        CloseEvent(ArticleForm source) {
            super(source, null);
        }
    }

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

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

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