package de.thpeetz.kontor.comics.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.combobox.ComboBox;
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.comics.data.Comic;
import de.thpeetz.kontor.comics.data.StoryArc;

import java.util.List;

public class StoryArcForm extends FormLayout {

    ComboBox<Comic> comic = new ComboBox<>("Comic");
    TextField name = new TextField("Story Arc Name");

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

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

    public StoryArcForm(List<Comic> comics) {
        addClassName("storyarc-form");
        binder.bindInstanceFields(this);

        comic.setItems(comics);
        comic.setItemLabelGenerator(Comic::getTitle);
        add(comic, name, 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 DeleteEvent(this, binder.getBean())));
        close.addClickListener(event -> fireEvent(new CloseEvent(this)));

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

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

    public void setStoryArc(StoryArc storyArc) {
        binder.setBean(storyArc);
    }

    public abstract static class StoryArcFormEvent extends ComponentEvent<StoryArcForm> {
        private StoryArc storyArc;

        protected StoryArcFormEvent(StoryArcForm source, StoryArc storyArc) {
            super(source, false);
            this.storyArc = storyArc;
        }

        public StoryArc getStoryArc() {
            return storyArc;
        }
    }

    public static class SaveEvent extends StoryArcFormEvent {
        SaveEvent(StoryArcForm source, StoryArc storyArc) {
            super(source, storyArc);
        }
    }

    public static class DeleteEvent extends StoryArcFormEvent {
        DeleteEvent(StoryArcForm source, StoryArc storyArc) {
            super(source, storyArc);
        }
    }

    public static class CloseEvent extends StoryArcFormEvent {
        CloseEvent(StoryArcForm source) {
            super(source, null);
        }
    }

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

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

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