using System;
using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Data.Extensions;
using SabreTools.Metadata.Filter;
using SabreTools.Text.Extensions;
namespace SabreTools.Metadata.DatItems
{
///
/// Represents the information specific to a set/game/machine
///
[JsonObject("machine"), XmlRoot("machine")]
public sealed class Machine : ModelBackedItem, ICloneable, IEquatable
{
#region Constructors
public Machine()
{
_internal = [];
}
public Machine(Data.Models.Metadata.Machine machine)
{
// Get all fields to automatically copy without processing
var nonItemFields = TypeHelper.GetConstants(typeof(Data.Models.Metadata.Machine));
if (nonItemFields is null)
return;
// Populate the internal machine from non-filter fields
_internal = [];
foreach (string fieldName in nonItemFields)
{
if (machine.TryGetValue(fieldName, out var value))
_internal[fieldName] = value;
}
// Process flag values
if (GetStringFieldValue(Data.Models.Metadata.Machine.Im1CRCKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.Im1CRCKey, TextHelper.NormalizeCRC32(GetStringFieldValue(Data.Models.Metadata.Machine.Im1CRCKey)));
if (GetStringFieldValue(Data.Models.Metadata.Machine.Im2CRCKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.Im2CRCKey, TextHelper.NormalizeCRC32(GetStringFieldValue(Data.Models.Metadata.Machine.Im2CRCKey)));
if (GetBoolFieldValue(Data.Models.Metadata.Machine.IsBiosKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.IsBiosKey, GetBoolFieldValue(Data.Models.Metadata.Machine.IsBiosKey).FromYesNo());
if (GetBoolFieldValue(Data.Models.Metadata.Machine.IsDeviceKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.IsDeviceKey, GetBoolFieldValue(Data.Models.Metadata.Machine.IsDeviceKey).FromYesNo());
if (GetBoolFieldValue(Data.Models.Metadata.Machine.IsMechanicalKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.IsMechanicalKey, GetBoolFieldValue(Data.Models.Metadata.Machine.IsMechanicalKey).FromYesNo());
if (GetStringFieldValue(Data.Models.Metadata.Machine.SupportedKey) is not null)
SetFieldValue(Data.Models.Metadata.Machine.SupportedKey, GetStringFieldValue(Data.Models.Metadata.Machine.SupportedKey).AsSupported().AsStringValue());
// Handle Trurip object, if it exists
if (machine.ContainsKey(Data.Models.Metadata.Machine.TruripKey))
{
var truripItem = machine.Read(Data.Models.Metadata.Machine.TruripKey);
if (truripItem is not null)
SetFieldValue(Data.Models.Metadata.Machine.TruripKey, new Trurip(truripItem));
}
}
#endregion
#region Accessors
///
/// Gets the name to use for a Machine
///
/// Name if available, null otherwise
public string? GetName() => _internal.GetName();
///
/// Sets the name to use for a Machine
///
/// Name to set for the item
public void SetName(string? name) => _internal.SetName(name);
#endregion
#region Cloning methods
///
/// Create a clone of the current machine
///
/// New machine with the same values as the current one
public object Clone()
{
return new Machine()
{
_internal = _internal.Clone() as Data.Models.Metadata.Machine ?? [],
};
}
///
/// Get a clone of the current internal model
///
public Data.Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Data.Models.Metadata.Machine)!;
#endregion
#region Comparision Methods
///
public override bool Equals(ModelBackedItem? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not Machine otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
///
public override bool Equals(ModelBackedItem? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not Machine otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
///
public bool Equals(Machine? other)
{
// If other is null
if (other is null)
return false;
// Compare internal models
return _internal.EqualTo(other._internal);
}
#endregion
#region Manipulation
///
/// Runs a filter and determines if it passes or not
///
/// Filter runner to use for checking
/// True if the Machine passes the filter, false otherwise
public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal);
#endregion
}
}