using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Markup.Xaml;

namespace VeloeMinecraftLauncher.Views;

public partial class SettingsView : UserControl
{
    public SettingsView()
    {
        InitializeComponent();
        this.FindControl<NumericUpDown>("GameWidthEdit").AddHandler(TextInputEvent, NumericUpDown_TextInput, Avalonia.Interactivity.RoutingStrategies.Tunnel);
        this.FindControl<NumericUpDown>("GameHeightEdit").AddHandler(TextInputEvent, NumericUpDown_TextInput, Avalonia.Interactivity.RoutingStrategies.Tunnel);
        this.FindControl<NumericUpDown>("MaxRamEdit").AddHandler(TextInputEvent, NumericUpDown_TextInput, Avalonia.Interactivity.RoutingStrategies.Tunnel);
        this.FindControl<NumericUpDown>("MaxLogEdit").AddHandler(TextInputEvent, NumericUpDown_TextInput, Avalonia.Interactivity.RoutingStrategies.Tunnel);
    }

    private int MaxGameWidth => (Avalonia.Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow?.Screens.All.Select(x => x.Bounds.Width).Max() ?? 1920;

    private int MaxGameHeight => (Avalonia.Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow?.Screens.All.Select(x => x.Bounds.Height).Max() ?? 1080;

    private void NumericUpDown_TextInput(object? sender, TextInputEventArgs e)
    {
        e.Handled = !e.Text?.All(x => char.IsDigit(x)) ?? false;
    }
}