using System.Windows;
using NexusShell.Controls;
using NexusShell.ViewModels;

namespace NexusShell.Views;

public partial class NexusConsoleWindow : NexusWindow
{
    public NexusConsoleWindow()
    {
        InitializeComponent();
    }

    protected override void OnClosed(EventArgs e)
    {
        if (DataContext is NexusConsoleViewModel vm)
        {
            vm.ExitRequested -= OnExitRequested;
            vm.OpenBrowserRequested -= OnOpenBrowserRequested;
            vm.OpenFilesRequested -= OnOpenFilesRequested;
            vm.RestartShellRequested -= OnRestartShellRequested;
        }

        base.OnClosed(e);
    }

    public void WireEvents(NexusConsoleViewModel vm)
    {
        vm.ExitRequested += OnExitRequested;
        vm.OpenBrowserRequested += OnOpenBrowserRequested;
        vm.OpenFilesRequested += OnOpenFilesRequested;
        vm.RestartShellRequested += OnRestartShellRequested;
    }

    private void OnExitRequested(object? sender, EventArgs e) => Close();

    private void OnOpenBrowserRequested(object? sender, string? url)
    {
        if (Owner is MainWindow mainWindow)
        {
            mainWindow.OpenBrowserFromConsole(url);
        }
    }

    private void OnOpenFilesRequested(object? sender, string? path)
    {
        if (Owner is MainWindow mainWindow)
        {
            mainWindow.OpenFilesFromConsole(path);
        }
    }

    private void OnRestartShellRequested(object? sender, EventArgs e)
    {
        if (Owner is MainWindow mainWindow)
        {
            mainWindow.RestartShellFromConsole();
        }
    }
}
