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.IntegerField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;

import de.thpeetz.kontor.tysc.data.FieldPosition;
import de.thpeetz.kontor.tysc.data.Player;
import de.thpeetz.kontor.tysc.data.Rooster;
import de.thpeetz.kontor.tysc.data.Team;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RoosterForm extends FormLayout {
    IntegerField year = new IntegerField("Year");
    ComboBox<Team> team = new ComboBox<>("Team");
    ComboBox<Player> player = new ComboBox<>("Player");
    ComboBox<FieldPosition> position = new ComboBox<>("Position");

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

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

    public RoosterForm(List<Team> teams, List<Player> players, List<FieldPosition> positions) {
        addClassName("rooster-form");
        binder.bindInstanceFields(this);

        team.setItems(teams);
        team.setItemLabelGenerator(Team::getName);
        player.setItems(players);
        player.setItemLabelGenerator(Player::getFullName);
        position.setItems(positions);
        position.setItemLabelGenerator(FieldPosition::getName);
        add(year, team, player, position, 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 setRooster(Rooster rooster) {
        binder.setBean(rooster);
    }

    public abstract static class RoosterFormEvent extends ComponentEvent<RoosterForm> {
        private Rooster rooster;

        protected RoosterFormEvent(RoosterForm source, Rooster rooster) {
            super(source, false);
            this.rooster = rooster;
        }

        public Rooster getRooster() {
            return rooster;
        }
    }

    public static class SaveEvent extends RoosterFormEvent {
        SaveEvent(RoosterForm source, Rooster rooster) {
            super(source, rooster);
        }
    }

    public static class DeleteEvent extends RoosterFormEvent {
        DeleteEvent(RoosterForm source, Rooster rooster) {
            super(source, rooster);
        }
    }

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