using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace SabreTools.Library.DatItems
{
///
/// Represents a condition on a machine or other item
///
[JsonObject("condition"), XmlRoot("condition")]
public class Condition : DatItem
{
#region Fields
///
/// Condition tag value
///
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("tag")]
public string Tag { get; set; }
///
/// Condition mask
///
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("mask")]
public string Mask { get; set; }
///
/// Condition relationship
///
[JsonProperty("relation", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))]
[XmlElement("relation")]
public Relation Relation { get; set; }
[JsonIgnore]
public bool RelationSpecified { get { return Relation != Relation.NULL; } }
///
/// Condition value
///
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("value")]
public string Value { get; set; }
#endregion
#region Accessors
///
/// Set fields with given values
///
/// Mappings dictionary
public override void SetFields(Dictionary mappings)
{
SetFields(mappings, false);
}
///
/// Set fields with given values
///
/// Mappings dictionary
/// True if this is a subitem, false otherwise
public void SetFields(Dictionary mappings, bool sub)
{
// Set base fields
base.SetFields(mappings);
// Handle Condition-specific fields
if (sub)
{
if (mappings.Keys.Contains(Field.DatItem_Condition_Tag))
Tag = mappings[Field.DatItem_Condition_Tag];
if (mappings.Keys.Contains(Field.DatItem_Condition_Mask))
Mask = mappings[Field.DatItem_Condition_Mask];
if (mappings.Keys.Contains(Field.DatItem_Condition_Relation))
Relation = mappings[Field.DatItem_Condition_Relation].AsRelation();
if (mappings.Keys.Contains(Field.DatItem_Condition_Value))
Value = mappings[Field.DatItem_Condition_Value];
}
else
{
if (mappings.Keys.Contains(Field.DatItem_Tag))
Tag = mappings[Field.DatItem_Tag];
if (mappings.Keys.Contains(Field.DatItem_Mask))
Mask = mappings[Field.DatItem_Mask];
if (mappings.Keys.Contains(Field.DatItem_Relation))
Relation = mappings[Field.DatItem_Relation].AsRelation();
if (mappings.Keys.Contains(Field.DatItem_Value))
Value = mappings[Field.DatItem_Value];
}
}
#endregion
#region Constructors
///
/// Create a default, empty Condition object
///
public Condition()
{
ItemType = ItemType.Condition;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Condition()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
Tag = this.Tag,
Mask = this.Mask,
Relation = this.Relation,
Value = this.Value,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a Condition, return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Condition
Condition newOther = other as Condition;
// If the Feature information matches
return (Tag == newOther.Tag
&& Mask == newOther.Mask
&& Relation == newOther.Relation
&& Value == newOther.Value);
}
#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)
{
return PassesFilter(filter, false);
}
///
/// Check to see if a DatItem passes the filter
///
/// Filter to check against
/// True if this is a subitem, false otherwise
/// True if the item passed the filter, false otherwise
public bool PassesFilter(Filter filter, bool sub)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
if (sub)
{
// Filter on tag
if (!PassStringFilter(filter.DatItem_Condition_Tag, Tag))
return false;
// Filter on mask
if (!PassStringFilter(filter.DatItem_Condition_Mask, Mask))
return false;
// Filter on relation
if (filter.DatItem_Condition_Relation.MatchesPositive(Relation.NULL, Relation) == false)
return false;
if (filter.DatItem_Condition_Relation.MatchesNegative(Relation.NULL, Relation) == true)
return false;
// Filter on value
if (!PassStringFilter(filter.DatItem_Condition_Value, Value))
return false;
}
else
{
// Filter on tag
if (!PassStringFilter(filter.DatItem_Tag, Tag))
return false;
// Filter on mask
if (!PassStringFilter(filter.DatItem_Mask, Mask))
return false;
// Filter on relation
if (filter.DatItem_Relation.MatchesPositive(Relation.NULL, Relation) == false)
return false;
if (filter.DatItem_Relation.MatchesNegative(Relation.NULL, Relation) == true)
return false;
// Filter on value
if (!PassStringFilter(filter.DatItem_Value, Value))
return false;
}
return true;
}
///
/// Remove fields from the DatItem
///
/// List of Fields to remove
public override void RemoveFields(List fields)
{
RemoveFields(fields, false);
}
///
/// Remove fields from the DatItem
///
/// List of Fields to remove
/// True if this is a subitem, false otherwise
public void RemoveFields(List fields, bool sub)
{
// Remove common fields first
base.RemoveFields(fields);
// Remove the fields
if (sub)
{
if (fields.Contains(Field.DatItem_Condition_Tag))
Tag = null;
if (fields.Contains(Field.DatItem_Condition_Mask))
Mask = null;
if (fields.Contains(Field.DatItem_Condition_Relation))
Relation = Relation.NULL;
if (fields.Contains(Field.DatItem_Condition_Value))
Value = null;
}
else
{
if (fields.Contains(Field.DatItem_Tag))
Tag = null;
if (fields.Contains(Field.DatItem_Mask))
Mask = null;
if (fields.Contains(Field.DatItem_Relation))
Relation = Relation.NULL;
if (fields.Contains(Field.DatItem_Value))
Value = null;
}
}
#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 Condition to replace from, ignore specific fields
if (item.ItemType != ItemType.Condition)
return;
// Cast for easier access
Condition newItem = item as Condition;
// Replace the fields
if (fields.Contains(Field.DatItem_Tag))
Tag = newItem.Tag;
else if (fields.Contains(Field.DatItem_Condition_Tag))
Tag = newItem.Tag;
if (fields.Contains(Field.DatItem_Mask))
Mask = newItem.Mask;
else if (fields.Contains(Field.DatItem_Condition_Mask))
Mask = newItem.Mask;
if (fields.Contains(Field.DatItem_Relation))
Relation = newItem.Relation;
else if (fields.Contains(Field.DatItem_Condition_Relation))
Relation = newItem.Relation;
if (fields.Contains(Field.DatItem_Value))
Value = newItem.Value;
else if (fields.Contains(Field.DatItem_Condition_Value))
Value = newItem.Value;
}
#endregion
}
}