using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
{
///
///
/// Represents a generic information for a Corsair-.
///
public class CorsairRGBDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
///
/// Gets the corsair specific device type.
///
public CorsairDeviceType CorsairDeviceType { get; }
///
/// Gets the index of the .
///
public int CorsairDeviceIndex { get; }
///
public RGBDeviceType DeviceType { get; }
///
public string DeviceName { get; }
///
public string Manufacturer => "Corsair";
///
public string Model { get; }
///
public Uri Image { get; set; }
///
public bool SupportsSyncBack => true;
///
public RGBDeviceLighting Lighting => RGBDeviceLighting.Key;
///
/// Gets a flag that describes device capabilities. ()
///
public CorsairDeviceCaps CapsMask { get; }
#endregion
#region Constructors
///
/// Internal constructor of managed .
///
/// The index of the .
/// The type of the .
/// The native -struct
/// A dictionary containing counters to create unique names for equal devices models.
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, Dictionary modelCounter)
{
this.CorsairDeviceIndex = deviceIndex;
this.DeviceType = deviceType;
this.CorsairDeviceType = nativeInfo.type;
this.Model = nativeInfo.model == IntPtr.Zero ? null : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase);
this.CapsMask = (CorsairDeviceCaps)nativeInfo.capsMask;
DeviceName = GetUniqueModelName(modelCounter);
}
///
/// Internal constructor of managed .
///
/// The index of the .
/// The type of the .
/// The native -struct
/// The name of the device-model (overwrites the one provided with the device info).
/// A dictionary containing counters to create unique names for equal devices models.
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, string modelName, Dictionary modelCounter)
{
this.CorsairDeviceIndex = deviceIndex;
this.DeviceType = deviceType;
this.CorsairDeviceType = nativeInfo.type;
this.Model = modelName;
this.CapsMask = (CorsairDeviceCaps)nativeInfo.capsMask;
DeviceName = GetUniqueModelName(modelCounter);
}
#endregion
#region Methods
private string GetUniqueModelName(Dictionary modelCounter)
{
if (modelCounter.TryGetValue(Model, out int counter))
{
counter = ++modelCounter[Model];
return $"{Manufacturer} {Model} {counter}";
}
else
{
modelCounter.Add(Model, 1);
return $"{Manufacturer} {Model}";
}
}
#endregion
}
}