using System.Collections.Specialized;
using System.Windows.Controls;
using System.Windows.Input;
using NexusShell.ViewModels;

namespace NexusShell.Views;

public partial class NexusConsoleView : UserControl
{
    public NexusConsoleView()
    {
        InitializeComponent();
        Loaded += (_, _) =>
        {
            if (DataContext is NexusConsoleViewModel vm)
            {
                vm.OutputLines.CollectionChanged += OutputLines_OnCollectionChanged;
            }

            CommandBox.Focus();
        };
    }

    private void OutputLines_OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        OutputScroll.ScrollToEnd();
    }

    private void CommandBox_OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter || DataContext is not NexusConsoleViewModel vm)
        {
            return;
        }

        vm.SubmitCommand();
        e.Handled = true;
    }
}
