// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Layout;
namespace RGB.NET.Devices.Debug
{
///
///
/// Represents a device provider responsible for debug devices.
///
public class DebugDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static DebugDeviceProvider? _instance;
///
/// Gets the singleton instance.
///
public static DebugDeviceProvider Instance => _instance ?? new DebugDeviceProvider();
///
public bool IsInitialized { get; private set; }
///
public IEnumerable Devices { get; private set; } = Enumerable.Empty();
private List<(IDeviceLayout layout, string imageLayout, Action>? updateLedsAction)> _fakeDeviceDefinitions = new();
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// Thrown if this constructor is called even if there is already an instance of this class.
public DebugDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(DebugDeviceProvider)}");
_instance = this;
}
#endregion
#region Methods
///
/// Adds a new fake device definition.
///
/// The path of the layout file to be used.
/// The image-layout to load.
/// A action emulating led-updates.
public void AddFakeDeviceDefinition(IDeviceLayout layout, string imageLayout, Action>? updateLedsAction = null)
=> _fakeDeviceDefinitions.Add((layout, imageLayout, updateLedsAction));
///
/// Removes all previously added fake device definitions.
///
public void ClearFakeDeviceDefinitions() => _fakeDeviceDefinitions.Clear();
///
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.Unknown, bool throwExceptions = false)
{
IsInitialized = false;
try
{
List devices = new();
foreach ((IDeviceLayout layout, string imageLayout, Action>? updateLedsAction) in _fakeDeviceDefinitions)
{
DebugRGBDevice device = new(layout, updateLedsAction);
devices.Add(device);
}
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
return true;
}
catch
{
if (throwExceptions) throw;
return false;
}
}
///
public void ResetDevices()
{ }
///
public void Dispose()
{
_fakeDeviceDefinitions.Clear();
}
#endregion
}
}