using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Metin2Launcher.UI.ViewModels;

namespace Metin2Launcher.UI;

public partial class App : Application
{
    public static MainWindowViewModel? MainViewModel { get; set; }

    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var vm = MainViewModel ?? new MainWindowViewModel();
            desktop.MainWindow = new MainWindow { DataContext = vm };
            vm.AttachWindow(desktop.MainWindow);
            desktop.MainWindow.Opened += async (_, _) => await vm.StartUpdateAsync();
        }
        base.OnFrameworkInitializationCompleted();
    }
}
