package io.xpipe.app.update;

import io.xpipe.app.comp.RegionBuilder;
import io.xpipe.app.comp.base.MarkdownComp;
import io.xpipe.app.comp.base.ModalOverlay;
import io.xpipe.app.core.AppProperties;
import io.xpipe.app.issue.TrackEvent;

public class UpdateAvailableDialog {

    public static boolean showIfNeeded(boolean wait) {
        if (AppProperties.get().isAotTrainMode()) {
            return false;
        }

        UpdateHandler uh = AppDistributionType.get().getUpdateHandler();
        if (uh.getPreparedUpdate().getValue() == null) {
            return false;
        }

        // Check whether we still have the latest version prepared
        uh.refreshUpdateCheckSilent(false, uh.getPreparedUpdate().getValue().isSecurityOnly());
        if (uh.getPreparedUpdate().getValue() == null) {
            return false;
        }

        TrackEvent.withInfo("Showing update alert ...")
                .tag("version", uh.getPreparedUpdate().getValue().getVersion())
                .handle();
        var u = uh.getPreparedUpdate().getValue();

        var comp = RegionBuilder.of(() -> {
            var markdown = new MarkdownComp(u.getBody() != null ? u.getBody() : "", s -> s, false).build();
            return markdown;
        });
        var modal = ModalOverlay.of("updateReadyAlertTitle", comp.prefWidth(600), null);
        for (var action : uh.createActions()) {
            modal.addButton(action);
        }

        if (wait) {
            modal.showAndWait();
        } else {
            modal.show();
        }
        return true;
    }
}
