package ru.vyatsu.qr_access_admin.unit.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.textfield.TextField;
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.partner.entity.PartnerEntity;
import ru.vyatsu.qr_access_admin.partner.entity.PartnerRepository;
import ru.vyatsu.qr_access_admin.unit.model.UnitModel;

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

public class UnitEditor extends Composite<VerticalLayout> {
    public interface SaveListener {
        void onSave(UnitModel unit);
    }

    public interface DeleteListener {
        void onDelete(UnitModel unit);
    }

    public interface CancelListener {
        void onCancel();
    }

    private volatile UnitModel currentUnit;

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

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

    public void setCurrentUnit(UnitModel unit) {
        this.currentUnit = unit;
        binder.setBean(unit);
    }

    public UnitEditor(PartnerRepository partnerRepository) {
        var clientId = new TextField("Идентификатор устройства");
        clientId.setRequired(true);
        var clientSecret = new TextField("Секрет устройства");
        clientSecret.setRequired(true);
        var clientSecretExpiresAt = new DateTimePicker("Актуален до");
        clientSecretExpiresAt.setRequiredIndicatorVisible(true);
        var clientName = new TextField("Человекочитаемое название устройства");
        clientName.setRequired(true);
        ComboBox<PartnerEntity> partnerField = new ComboBox<>("Партнер");
        partnerField.setPageSize(100);
        Map<String, PartnerEntity> partners = partnerRepository.findAll()
                .stream()
                .collect(Collectors.toMap(PartnerEntity::getId, p -> p));
        partnerField.setItemLabelGenerator(PartnerEntity::getName);
        partnerField.setItems(partners.values());

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

        binder.forField(clientId).bind("clientId");
        binder.forField(clientSecret).bind("clientSecret");
        binder.forField(clientSecretExpiresAt).bind("clientSecretExpiresAt");
        binder.forField(clientName).bind("clientName");
        binder.forField(partnerField)
                .withConverter(PartnerEntity::getId, partners::get, "Invalid value")
                .bind("partnerId");

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

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

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

        getContent().add(clientId, clientSecret, clientSecretExpiresAt, clientName, partnerField, new HorizontalLayout(save, cancel, delete));
    }
}
