package ru.vyatsu.qr_access_admin.qr.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.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.qr.entity.QrEntity;

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

public class QrEditor extends Composite<VerticalLayout> {

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

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

    public interface CancelListener {
        void onCancel();
    }

    private volatile QrEntity currentQr;

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

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

    public void setCurrentQr(QrEntity qr) {
        this.currentQr = qr;
        binder.setBean(qr);
    }

    public QrEditor(DoorRepository doorRepository) {
        ComboBox<DoorEntity> doorIdField = new ComboBox<>("Идентификатор двери");
        doorIdField.setRequired(true);
        doorIdField.setItemLabelGenerator(DoorEntity::getId);
        Map<String, DoorEntity> doors = doorRepository.findAll()
                .stream()
                .collect(Collectors.toMap(DoorEntity::getId, e -> e));

        doorIdField.setItems(doors.values());
        var startDateTime = new DateTimePicker("Начало действия");
        var endDateTime = new DateTimePicker("Актуален до");

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

        binder.forField(doorIdField)
                .withConverter(DoorEntity::getId, doors::get, "Invalid value")
                .bind("doorId");
        binder.forField(startDateTime).bind("startDateTime");
        binder.forField(endDateTime).bind("endDateTime");

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

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

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

        getContent().add(doorIdField, startDateTime, endDateTime, new HorizontalLayout(save, cancel, delete));
    }
}
