using RhythmBase.Components;
using RhythmBase.Components.Easing;
using RhythmBase.Events;
using System.Reflection;
namespace RhythmBase.Extensions
{
///
/// Provides extension methods for retrieving easing properties from events.
///
public static class EasePropertyExtensions
{
private static readonly Dictionary EaseProperties = [];
///
/// Retrieves the easing properties of the specified type of event.
///
/// The type of the event that implements .
/// The collection of events to retrieve the easing properties from.
/// A dictionary containing the names and corresponding easing properties of the specified event type.
/// Thrown when an unsupported property type is encountered.
public static Dictionary GetEaseProperties(this IEnumerable obj) where TEvent : IEaseEvent, new()
{
if (!EaseProperties.TryGetValue(typeof(TEvent), out PropertyInfo[]? properties))
{
EaseProperties[obj.GetType()] = properties = typeof(TEvent).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetCustomAttribute() != null)
.ToArray();
}
Dictionary values = [];
foreach (var property in properties)
{
if (property.PropertyType.IsAssignableFrom(typeof(RDExpression)))
{
values[property.Name] = EasePropertyFloat.CreateEaseProperty(0, [.. obj], property);
}
else if (property.PropertyType.IsAssignableFrom(typeof(RDPointE)))
{
values[property.Name] = EasePropertyPoint.CreateEaseProperty(default, [.. obj], property);
}
else if (property.PropertyType.IsAssignableFrom(typeof(RDSizeE)))
{
values[property.Name] = EasePropertySize.CreateEaseProperty(default, [.. obj], property);
}
else if (property.PropertyType.IsAssignableFrom(typeof(RDColor)))
{
values[property.Name] = EasePropertyColor.CreateEaseProperty(default, [.. obj], property);
}
else
{
throw new NotSupportedException($"Unsupported property type {property.PropertyType}");
}
}
return values;
}
}
}