using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RhythmBase.Components;
using RhythmBase.Extensions;
using RhythmBase.Settings;
using RhythmBase.Utils;
namespace RhythmBase.Events
{
///
/// Represents a custom event in the rhythm base system.
///
public class CustomEvent : BaseEvent
{
///
[JsonIgnore]
public override EventType Type => EventType.CustomEvent;
///
/// Gets or sets the actual type of the custom event.
///
[JsonIgnore]
public string ActureType
{
get => Data["Type".ToLowerCamelCase()]?.ToObject() ?? "";
init => Data["Type".ToLowerCamelCase()] = value;
}
///
public override Tabs Tab { get; }
///
public override int Y
{
get => (int)(Data["Y".ToLowerCamelCase()] ?? 0);
set => Data["Y".ToLowerCamelCase()] = value;
}
///
/// Initializes a new instance of the class.
///
public CustomEvent()
{
Data = [];
Tab = Tabs.Unknown;
}
///
/// Initializes a new instance of the class with the specified data.
///
/// The data for the custom event.
public CustomEvent(JObject data)
{
Data = data ?? [];
Tab = Tabs.Unknown;
Beat = new RDBeat(Data["bar"]?.ToObject() ?? 1, Data["beat"]?.ToObject() ?? 1f);
Tag = Data["tag"]?.ToObject() ?? "";
Condition = Data["condition"] == null
? null
: Condition.Load(Data["condition"]?.ToObject() ?? "");
Active = Data["active"]?.ToObject() ?? true;
}
///
public override string ToString() => $"{Beat} *{ActureType}";
///
/// Tries to convert the current custom event to a base event.
///
/// The base event to convert to.
/// The type of the event.
/// True if the conversion was successful; otherwise, false.
public virtual bool TryConvert(ref BaseEvent? value, ref EventType? type) => TryConvert(ref value, ref type, new LevelReadOrWriteSettings());
///
/// Tries to convert the current custom event to a base event with the specified settings.
///
/// The base event to convert to.
/// The type of the event.
/// The settings for reading or writing the level.
/// True if the conversion was successful; otherwise, false.
public virtual bool TryConvert(ref BaseEvent? value, ref EventType? type, LevelReadOrWriteSettings settings)
{
JsonSerializer serializer = JsonSerializer.Create(_beat.BaseLevel?.GetSerializer(settings));
Type eventType = Utils.EventTypeUtils.ToType(Data["type"]?.ToObject() ?? "");
bool TryConvert;
if (eventType == null)
{
if (Data["target"] != null)
value = Data.ToObject(serializer);
else if (Data["row"] != null)
value = Data.ToObject(serializer);
else
value = Data.ToObject(serializer);
type = null;
TryConvert = value is not null;
}
else
{
value = (BaseEvent?)Data.ToObject(eventType, serializer);
type = value?.Type;
TryConvert = true;
}
return TryConvert;
}
///
/// Gets or sets the data for the custom event.
///
[JsonIgnore]
public JObject Data { get; set; }
}
}