package ru.vyatsu.qr_access_admin.slot.component;

import com.vaadin.flow.component.Composite;
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.datetimepicker.DateTimePicker;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.timepicker.TimePicker;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import lombok.Getter;
import lombok.Setter;
import ru.vyatsu.qr_access_admin.door.entity.DoorEntity;
import ru.vyatsu.qr_access_admin.door.entity.DoorRepository;
import ru.vyatsu.qr_access_admin.slot.entity.SlotEntity;

import java.util.Map;
import java.util.stream.Collectors;

public class SlotEditor extends Composite<VerticalLayout> {

    public interface SaveListener {
        void onSave(SlotEntity qr);
    }

    public interface DeleteListener {
        void onDelete(SlotEntity qr);
    }

    public interface CancelListener {
        void onCancel();
    }

    private volatile SlotEntity currentSlot;

    @Getter
    @Setter
    private SaveListener saveListener;
    @Getter
    @Setter
    private DeleteListener deleteListener;
    @Getter
    @Setter
    private CancelListener cancelListener;

    private final Binder<SlotEntity> binder = new BeanValidationBinder<>(SlotEntity.class);

    public void setCurrentSlot(SlotEntity slot) {
        this.currentSlot = slot;
        binder.setBean(slot);
    }

    public SlotEditor() {
        var startTime = new TimePicker("Начало действия");
        var endTime = new TimePicker("Конец действия");

        var save = new Button("Сохранить", VaadinIcon.CHECK.create());
        var cancel = new Button("Отмена");
        var delete = new Button("Удалить", VaadinIcon.TRASH.create());

        binder.forField(startTime).bind("startTime");
        binder.forField(endTime).bind("endTime");

        save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        save.addClickListener(e -> saveListener.onSave(currentSlot));
        save.addClickShortcut(Key.ENTER);

        delete.addThemeVariants(ButtonVariant.LUMO_ERROR);
        delete.addClickListener(e -> deleteListener.onDelete(currentSlot));

        cancel.addClickListener(e -> cancelListener.onCancel());

        getContent().add(startTime, endTime, new HorizontalLayout(save, cancel, delete));
    }
}
