package io.xpipe.app.util;

import io.xpipe.app.comp.base.ModalButton;
import io.xpipe.app.comp.base.ModalOverlay;
import io.xpipe.app.core.AppI18n;
import io.xpipe.app.core.AppLayoutModel;
import io.xpipe.app.ext.ShellStore;
import io.xpipe.app.platform.LabelGraphic;
import io.xpipe.app.platform.PlatformThread;
import io.xpipe.app.storage.DataStoreEntry;
import io.xpipe.app.storage.DataStoreEntryRef;

import javafx.beans.binding.Bindings;

import java.util.List;

public class ScanDialog {

    public static void showSingleAsync(DataStoreEntry entry) {
        var showForCon = entry == null || entry.getStore() instanceof ShellStore;
        if (showForCon) {
            showSingle(entry, ScanDialogAction.shellScanAction());
        }
    }

    private static void showSingle(DataStoreEntry initialStore, ScanDialogAction action) {
        var comp = new ScanSingleDialogComp(initialStore != null ? initialStore.ref() : null, action);
        var modal = ModalOverlay.of("scanAlertTitle", comp);
        var queueEntry = new AppLayoutModel.QueueEntry(
                AppI18n.observable("scanConnections"), new LabelGraphic.IconGraphic("mdi2l-layers-plus"), () -> false);
        var button = new ModalButton(
                "ok",
                () -> {
                    AppLayoutModel.get().getQueueEntries().add(queueEntry);
                    ThreadHelper.runAsync(() -> {
                        comp.finish();
                        AppLayoutModel.get().getQueueEntries().remove(queueEntry);
                    });
                },
                true,
                true);
        var list = comp.getBase().getSelected();
        button.augment(r -> r.disableProperty()
                .bind(PlatformThread.sync(Bindings.createBooleanBinding(
                        () -> {
                            return comp.getBusy().get() || list.isEmpty();
                        },
                        comp.getBusy(),
                        list))));
        modal.addButton(button);
        modal.show();
    }

    public static void showMulti(List<DataStoreEntryRef<ShellStore>> entries, ScanDialogAction action) {
        var comp = new ScanMultiDialogComp(entries, action);
        var modal = ModalOverlay.of("scanAlertTitle", comp);
        var queueEntry = new AppLayoutModel.QueueEntry(
                AppI18n.observable("scanConnections"), new LabelGraphic.IconGraphic("mdi2l-layers-plus"), () -> false);
        var button = new ModalButton(
                "ok",
                () -> {
                    AppLayoutModel.get().getQueueEntries().add(queueEntry);
                    ThreadHelper.runAsync(() -> {
                        comp.finish();
                        AppLayoutModel.get().getQueueEntries().remove(queueEntry);
                    });
                },
                true,
                true);
        button.augment(r -> r.disableProperty().bind(PlatformThread.sync(comp.getBusy())));
        modal.addButton(button);
        modal.show();
    }
}
