using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using ClaudeDo.Ui.ViewModels.Modals;

namespace ClaudeDo.Ui.Views.Modals;

public partial class UsageMonitorModalView : Window
{
    public UsageMonitorModalView() => InitializeComponent();

    /// <summary>
    /// Persists a stage typed into a gauge's legend box. `NumericUpDown` has no commit command, so
    /// the box's <c>Tag</c> names the stage and the row's matching command does the clamp + save.
    /// </summary>
    private void OnStageBoxCommit(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control { Tag: string stage, DataContext: UsageGaugeRowViewModel row }) return;

        var command = stage switch
        {
            "soft" => row.CommitSoftCommand,
            "hard" => row.CommitHardCommand,
            _ => row.CommitGateCommand,
        };

        if (command.CanExecute(null)) command.Execute(null);
    }

    private void OnStageBoxKeyDown(object? sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter) return;
        OnStageBoxCommit(sender, e);
        e.Handled = true;
    }
}
