using VisionBuilder.UI.Common.Services;
using VisionBuilder.UI.Avalonia.Views;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;

namespace VisionBuilder.UI.Avalonia.Services;

public class AvaloniaPasswordInputService : IPasswordInputService
{
    public async Task<string?> GetPasswordAsync()
    {
        var dialog = new PasswordDialog();
        var parent = GetMainWindow();
        if (parent != null)
            return await dialog.ShowDialog<string?>(parent);
        return null;
    }

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