using System;
using System.Threading.Tasks;
using CommunityToolkit.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using StrixMusic.Sdk.ViewModels;
using StrixMusic.Services.CoreManagement;
namespace StrixMusic.Shared.ViewModels
{
///
/// Used to display an item in the service section of .
///
public class LoadedServicesItemViewModel : ObservableObject
{
private bool _canDeleteCore = true;
///
/// Creates a new instance of .
///
/// Indicates that selecting this item will trigger adding a new core.
/// The core associated with this item, if applicable.
public LoadedServicesItemViewModel(bool isAddItem, CoreViewModel? core)
{
IsAddItem = isAddItem;
Core = core;
AddNewItemCommand = new RelayCommand(AddNewItem);
DeleteCoreCommand = new AsyncRelayCommand(DeleteCore);
ConfigureCoreCommand = new RelayCommand(ConfigureCore);
}
///
/// The core associated with this item, if applicable.
///
public CoreViewModel? Core { get; }
///
/// Indicates if selecting this item will trigger adding a new core.
///
public bool IsAddItem { get; }
///
/// Indicates if this core can be removed.
///
public bool CanDeleteCore
{
get => _canDeleteCore;
set => SetProperty(ref _canDeleteCore, value);
}
///
/// Executed when the user wants to add a new service.
///
public IRelayCommand AddNewItemCommand { get; }
///
/// Executed when the user wants to delete this .
///
public IAsyncRelayCommand DeleteCoreCommand { get; }
///
/// Executed when the user wants to set settings on the core.
///
public IRelayCommand ConfigureCoreCommand { get; }
///
/// Raised when the user requests to add a new service.
///
public event EventHandler? NewItemRequested;
///
/// Raised when the user requests configuration on the core.
///
public event EventHandler? ConfigRequested;
private void AddNewItem()
{
NewItemRequested?.Invoke(this, EventArgs.Empty);
}
private Task DeleteCore()
{
Guard.IsNotNull(Core, nameof(Core));
var coreManagementService = Ioc.Default.GetRequiredService();
return coreManagementService.UnregisterCoreInstanceAsync(Core.InstanceId);
}
private void ConfigureCore()
{
ConfigRequested?.Invoke(this, EventArgs.Empty);
}
}
}