using System.Collections.ObjectModel;
using System.Windows.Input;
using Microsoft.Win32;
using SpdUp.Core;
using SpdUp.Models;
using SpdUp.Services;
namespace SpdUp.ViewModels;
///
/// Game Library page – manage detected and manually-added games.
///
public sealed class GameLibraryViewModel : ObservableObject
{
private readonly AppSettings _settings;
private readonly GameDetectionService _detectionService;
public ObservableCollection Games { get; }
private GameEntry? _selectedGame;
public GameEntry? SelectedGame
{
get => _selectedGame;
set => SetProperty(ref _selectedGame, value);
}
public ICommand AddGameCommand { get; }
public ICommand RemoveGameCommand { get; }
public GameLibraryViewModel(AppSettings settings, GameDetectionService detectionService)
{
_settings = settings;
_detectionService = detectionService;
Games = new ObservableCollection(_settings.Games);
AddGameCommand = new RelayCommand(AddGame);
RemoveGameCommand = new RelayCommand(RemoveGame, () => SelectedGame is not null);
}
private void AddGame()
{
var dlg = new OpenFileDialog
{
Filter = "Executables (*.exe)|*.exe",
Title = "Select Game Executable"
};
if (dlg.ShowDialog() == true)
{
var entry = new GameEntry
{
Name = System.IO.Path.GetFileNameWithoutExtension(dlg.FileName),
ExecutablePath = dlg.FileName,
ProcessName = GameEntry.ProcessNameFromPath(dlg.FileName),
LauncherSource = "Manual"
};
Games.Add(entry);
_settings.Games.Add(entry);
_detectionService.AddGameProcess(entry.ProcessName);
_settings.Save();
}
}
private void RemoveGame()
{
if (SelectedGame is null) return;
_settings.Games.Remove(SelectedGame);
Games.Remove(SelectedGame);
_settings.Save();
}
}