using System; using System.Collections.Generic; using System.Linq; using SabreTools.RedumpLib.Data; namespace MPF.Core.UI.ComboBoxItems { /// /// Represents a single item in the System combo box /// public class RedumpSystemComboBoxItem : IEquatable, IElement { private readonly object? Data; public RedumpSystemComboBoxItem(RedumpSystem? system) => Data = system; public RedumpSystemComboBoxItem(SystemCategory? category) => Data = category; public static implicit operator RedumpSystem?(RedumpSystemComboBoxItem item) => item.Data as RedumpSystem?; /// public string Name { get { if (IsHeader) return "---------- " + (Data as SystemCategory?).LongName() + " ----------"; else return (Data as RedumpSystem?).LongName() ?? "No system selected"; } } public override string ToString() => Name; /// /// Internal enum value /// public RedumpSystem? Value => Data as RedumpSystem?; /// /// Determines if the item is a header value /// public bool IsHeader => Data is SystemCategory?; /// /// Determines if the item is a standard system value /// public bool IsSystem => Data is RedumpSystem?; /// /// Generate all elements for the known system combo box /// /// public static IEnumerable GenerateElements() { var knownSystems = Enum.GetValues(typeof(RedumpSystem)) .OfType() .Where(s => !s.IsMarker() && s.GetCategory() != SystemCategory.NONE) .ToList(); Dictionary> mapping = knownSystems .GroupBy(s => s.GetCategory()) .ToDictionary( k => k.Key, v => v .OrderBy(s => s.LongName()) .ToList() ); var systemsValues = new List { new RedumpSystemComboBoxItem((RedumpSystem?)null), }; foreach (var group in mapping) { systemsValues.Add(new RedumpSystemComboBoxItem(group.Key)); group.Value.ForEach(system => systemsValues.Add(new RedumpSystemComboBoxItem(system))); } return systemsValues; } /// public override bool Equals(object? obj) { return Equals(obj as RedumpSystemComboBoxItem); } /// public bool Equals(RedumpSystemComboBoxItem? other) { if (other == null) return false; return Value == other.Value; } /// public override int GetHashCode() => base.GetHashCode(); } }