using VisionBuilder.UI.Common.ViewModel;
using VisionBuilder.UI.Common.ViewModel.Interfaces.UI;
using VisionBuilder.UI.Avalonia.Views;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;

namespace VisionBuilder.UI.Avalonia.Services;

public class AvaloniaImagePreviewService : IImagePreviewService
{
    public async Task ShowImagePreviewAsync(ErrorPreviewVM errorPreviewVm)
    {
        var dialog = new ImagePreviewDialog(errorPreviewVm);
        var parent = GetMainWindow();
        if (parent != null)
            await dialog.ShowDialog(parent);
    }

    private static Window? GetMainWindow()
    {
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            return desktop.MainWindow;
        return null;
    }
}
