using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools;
using Newtonsoft.Json;
namespace SabreTools.Library.DatItems
{
///
/// Represents one ListXML input
///
[JsonObject("input"), XmlRoot("input")]
public class Input : DatItem
{
#region Fields
///
/// Input service ID
///
[JsonProperty("service", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("service")]
public bool? Service { get; set; }
[JsonIgnore]
public bool ServiceSpecified { get { return Service != null; } }
///
/// Determins if this has a tilt sensor
///
[JsonProperty("tilt", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("tilt")]
public bool? Tilt { get; set; }
[JsonIgnore]
public bool TiltSpecified { get { return Tilt != null; } }
///
/// Number of players on the input
///
[JsonProperty("players", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("players")]
public long? Players { get; set; }
[JsonIgnore]
public bool PlayersSpecified { get { return Players != null; } }
///
/// Number of coins required
///
[JsonProperty("coins", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("coins")]
public long? Coins { get; set; }
[JsonIgnore]
public bool CoinsSpecified { get { return Coins != null; } }
///
/// Set of controls for the input
///
[JsonProperty("controls", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("controls")]
public List Controls { get; set; }
[JsonIgnore]
public bool ControlsSpecified { get { return Controls != null && Controls.Count > 0; } }
#endregion
#region Accessors
///
/// Set fields with given values
///
/// Mappings dictionary
public override void SetFields(Dictionary mappings)
{
// Set base fields
base.SetFields(mappings);
// Handle Input-specific fields
if (mappings.Keys.Contains(Field.DatItem_Service))
Service = mappings[Field.DatItem_Service].AsYesNo();
if (mappings.Keys.Contains(Field.DatItem_Tilt))
Tilt = mappings[Field.DatItem_Tilt].AsYesNo();
if (mappings.Keys.Contains(Field.DatItem_Players))
Players = Sanitizer.CleanLong(mappings[Field.DatItem_Players]);
if (mappings.Keys.Contains(Field.DatItem_Coins))
Coins = Sanitizer.CleanLong(mappings[Field.DatItem_Coins]);
if (Controls != null)
{
foreach (Control control in Controls)
{
control.SetFields(mappings);
}
}
}
#endregion
#region Constructors
///
/// Create a default, empty Input object
///
public Input()
{
ItemType = ItemType.Input;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Input()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
Service = this.Service,
Tilt = this.Tilt,
Players = this.Players,
Coins = this.Coins,
Controls = this.Controls,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a Input, return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Input
Input newOther = other as Input;
// If the Input information matches
bool match = (Service == newOther.Service
&& Tilt == newOther.Tilt
&& Players == newOther.Players
&& Coins == newOther.Coins);
if (!match)
return match;
// If the controls match
if (Controls != null)
{
foreach (Control control in Controls)
{
match &= newOther.Controls.Contains(control);
}
}
return match;
}
#endregion
#region Filtering
///
/// Check to see if a DatItem passes the filter
///
/// Filter to check against
/// True if the item passed the filter, false otherwise
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on service
if (!filter.PassBoolFilter(filter.DatItem_Service, Service))
return false;
// Filter on tilt
if (!filter.PassBoolFilter(filter.DatItem_Tilt, Tilt))
return false;
// Filter on players
if (!filter.PassLongFilter(filter.DatItem_Players, Players))
return false;
// Filter on coins
if (!filter.PassLongFilter(filter.DatItem_Coins, Coins))
return false;
// Filter on individual controls
if (Controls != null)
{
foreach (Control control in Controls)
{
if (!control.PassesFilter(filter))
return false;
}
}
return true;
}
///
/// Remove fields from the DatItem
///
/// List of Fields to remove
public override void RemoveFields(List fields)
{
// Remove common fields first
base.RemoveFields(fields);
// Remove the fields
if (fields.Contains(Field.DatItem_Service))
Service = null;
if (fields.Contains(Field.DatItem_Tilt))
Tilt = null;
if (fields.Contains(Field.DatItem_Players))
Players = 0;
if (fields.Contains(Field.DatItem_Coins))
Coins = null;
if (Controls != null)
{
foreach (Control control in Controls)
{
control.RemoveFields(fields);
}
}
}
#endregion
#region Sorting and Merging
///
/// Replace fields from another item
///
/// DatItem to pull new information from
/// List of Fields representing what should be updated
public override void ReplaceFields(DatItem item, List fields)
{
// Replace common fields first
base.ReplaceFields(item, fields);
// If we don't have a Input to replace from, ignore specific fields
if (item.ItemType != ItemType.Input)
return;
// Cast for easier access
Input newItem = item as Input;
// Replace the fields
if (fields.Contains(Field.DatItem_Service))
Service = newItem.Service;
if (fields.Contains(Field.DatItem_Tilt))
Tilt = newItem.Tilt;
if (fields.Contains(Field.DatItem_Players))
Players = newItem.Players;
if (fields.Contains(Field.DatItem_Coins))
Coins = newItem.Coins;
// DatItem_Control_* doesn't make sense here
// since not every control under the other item
// can replace every control under this item
}
#endregion
}
}