using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RGB.NET.Layout;
namespace Artemis.Core;
///
/// Represents a LED layout decorated with extra Artemis-specific data
///
public class ArtemisLedLayout
{
internal ArtemisLedLayout(ArtemisLayout deviceLayout, ILedLayout led)
{
DeviceLayout = deviceLayout;
RgbLayout = led;
LayoutCustomLedData = (LayoutCustomLedData?) led.CustomData ?? new LayoutCustomLedData();
// Default to the first logical layout for images
LayoutCustomLedDataLogicalLayout? defaultLogicalLayout = LayoutCustomLedData.LogicalLayouts?.FirstOrDefault();
if (defaultLogicalLayout != null)
ApplyLogicalLayout(defaultLogicalLayout);
}
///
/// Gets the device layout of this LED layout
///
public ArtemisLayout DeviceLayout { get; }
///
/// Gets the RGB.NET LED Layout of this LED layout
///
public ILedLayout RgbLayout { get; }
///
/// Gets the name of the logical layout this LED belongs to
///
public string? LogicalName { get; private set; }
///
/// Gets the image of the LED
///
public Uri? Image { get; private set; }
///
/// Gets the custom layout data embedded in the RGB.NET layout
///
public LayoutCustomLedData LayoutCustomLedData { get; }
///
/// Gets the logical layout names available for this LED
///
public IEnumerable GetLogicalLayoutNames()
{
return LayoutCustomLedData.LogicalLayouts?.Where(l => l.Name != null).Select(l => l.Name!) ?? [];
}
internal void ApplyCustomLedData(ArtemisDevice artemisDevice)
{
if (LayoutCustomLedData.LogicalLayouts == null || !LayoutCustomLedData.LogicalLayouts.Any())
return;
// Prefer a matching layout or else a default layout (that has no name)
LayoutCustomLedDataLogicalLayout logicalLayout = LayoutCustomLedData.LogicalLayouts
.OrderByDescending(l => l.Name == artemisDevice.LogicalLayout)
.ThenBy(l => l.Name == null)
.First();
ApplyLogicalLayout(logicalLayout);
}
private void ApplyLogicalLayout(LayoutCustomLedDataLogicalLayout logicalLayout)
{
string? layoutDirectory = Path.GetDirectoryName(DeviceLayout.FilePath);
LogicalName = logicalLayout.Name;
if (layoutDirectory != null && logicalLayout.Image != null)
Image = new Uri(Path.Combine(layoutDirectory, logicalLayout.Image!), UriKind.Absolute);
else
Image = null;
}
}