package de.thpeetz.kontor.tysc.views;

import java.util.List;

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.tysc.data.Card;
import de.thpeetz.kontor.tysc.data.Rooster;
import de.thpeetz.kontor.tysc.data.Vendor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CardForm extends FormLayout {

    TextField cardNumber = new TextField("CardNumber");
    TextField year = new TextField("Year");
    ComboBox<Vendor> vendor = new ComboBox<>("Vendor");

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

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

    public CardForm(List<Vendor> vendors) {
        addClassName("card-form");
        binder.bindInstanceFields(this);

        vendor.setItems(vendors);
        vendor.setItemLabelGenerator(Vendor::getName);
        add(cardNumber, year, vendor, 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 setCard(Card card) {
        binder.setBean(card);
    }

    public void setRoosters(List<Rooster> roosters) {
        log.info("Setting roosters: {}", roosters);
    }

    public abstract static class CardFormEvent extends ComponentEvent<CardForm> {
        private Card card;

        protected CardFormEvent(CardForm source, Card card) {
            super(source, false);
            this.card = card;
        }

        public Card getCard() {
            return card;
        }
    }

    public static class SaveEvent extends CardFormEvent {
        SaveEvent(CardForm source, Card card) {
            super(source, card);
        }
    }

    public static class DeleteEvent extends CardFormEvent {
        DeleteEvent(CardForm source, Card card) {
            super(source, card);
        }
    }

    public static class CloseEvent extends CardFormEvent {
        CloseEvent(CardForm 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);
    }
}
