using System.Windows;
using NexusInstaller.Services;

namespace NexusInstaller.Views;

public partial class NexusLaunchWindow : Window
{
    private readonly string _installRoot;

    public NexusLaunchWindow(string installRoot)
    {
        InitializeComponent();
        _installRoot = installRoot;
        Loaded += async (_, _) => await LaunchAsync();
    }

    private async Task LaunchAsync()
    {
        try
        {
            StatusText.Text = "Starter Ninja Nexus...";
            using var launcher = new NexusLaunchService(_installRoot);
            launcher.ValidateInstall();
            launcher.StopExisting();

            var progress = new Progress<string>(msg => StatusText.Text = msg);
            await launcher.StartIdentityAsync(progress);

            StatusText.Text = "Starter Nexus Shell...";
            launcher.StartShell();
            Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(
                ex.Message,
                "Kunne ikke starte Ninja Nexus",
                MessageBoxButton.OK,
                MessageBoxImage.Error);
            Close();
        }
    }
}
