using System.Collections.Generic;
using SabreTools.Library.Filtering;
using Newtonsoft.Json;
namespace SabreTools.Library.DatItems
{
///
/// Represents release information about a set
///
public class Release : DatItem
{
#region Fields
///
/// Release region(s)
///
[JsonProperty("region")]
public string Region { get; set; }
///
/// Release language(s)
///
[JsonProperty("language")]
public string Language { get; set; }
///
/// Date of release
///
[JsonProperty("date")]
public string Date { get; set; }
///
/// Default release, if applicable
///
[JsonProperty("default")]
public bool? Default { get; set; }
#endregion
#region Accessors
///
/// Get the value of that field as a string, if possible
///
public override string GetField(Field field, List excludeFields)
{
// If the field is to be excluded, return empty string
if (excludeFields.Contains(field))
return string.Empty;
// Handle Release-specific fields
string fieldValue;
switch (field)
{
case Field.Region:
fieldValue = Region;
break;
case Field.Language:
fieldValue = Language;
break;
case Field.Date:
fieldValue = Date;
break;
case Field.Default:
fieldValue = Default?.ToString();
break;
// For everything else, use the base method
default:
return base.GetField(field, excludeFields);
}
// Make sure we don't return null
if (string.IsNullOrEmpty(fieldValue))
fieldValue = string.Empty;
return fieldValue;
}
#endregion
#region Constructors
///
/// Create a default, empty Release object
///
public Release()
{
Name = string.Empty;
ItemType = ItemType.Release;
Region = string.Empty;
Language = string.Empty;
Date = string.Empty;
Default = null;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Release()
{
Name = this.Name,
ItemType = this.ItemType,
DupeType = this.DupeType,
AltName = this.AltName,
AltTitle = this.AltTitle,
PartName = this.PartName,
PartInterface = this.PartInterface,
Features = this.Features,
AreaName = this.AreaName,
AreaSize = this.AreaSize,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
Region = this.Region,
Language = this.Language,
Date = this.Date,
Default = this.Default,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a release return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Release
Release newOther = other as Release;
// If the archive information matches
return (Name == newOther.Name
&& Region == newOther.Region
&& Language == newOther.Language
&& Date == newOther.Date
&& Default == newOther.Default);
}
#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 region
if (filter.Region.MatchesPositiveSet(Region) == false)
return false;
if (filter.Region.MatchesNegativeSet(Region) == true)
return false;
// Filter on language
if (filter.Language.MatchesPositiveSet(Language) == false)
return false;
if (filter.Language.MatchesNegativeSet(Language) == true)
return false;
// Filter on date
if (filter.Date.MatchesPositiveSet(Date) == false)
return false;
if (filter.Date.MatchesNegativeSet(Date) == true)
return false;
// Filter on default
if (filter.Default.MatchesNeutral(null, Default) == false)
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.Region))
Region = null;
if (fields.Contains(Field.Language))
Language = null;
if (fields.Contains(Field.Date))
Date = null;
if (fields.Contains(Field.Default))
Default = 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 Release to replace from, ignore specific fields
if (item.ItemType != ItemType.Release)
return;
// Cast for easier access
Release newItem = item as Release;
// Replace the fields
if (fields.Contains(Field.Region))
Region = newItem.Region;
if (fields.Contains(Field.Language))
Language = newItem.Language;
if (fields.Contains(Field.Date))
Date = newItem.Date;
if (fields.Contains(Field.Default))
Default = newItem.Default;
}
#endregion
}
}