using System; using System.Collections.Generic; using System.Linq; namespace ChanSort.Api { public class ChannelList { private readonly SignalSource source; private readonly IList channels = new List(); private readonly Dictionary> channelByUid = new Dictionary>(); private readonly Dictionary channelByProgNr = new Dictionary(); private readonly Dictionary> channelByName = new Dictionary>(); private int insertProgramNr = 1; private int duplicateUidCount; private int duplicateProgNrCount; public ChannelList(SignalSource source, string caption) { this.source = source; this.ShortCaption = caption; this.FirstProgramNumber = (source & SignalSource.Digital) != 0 ? 1 : 0; } public string ShortCaption { get; private set; } public SignalSource SignalSource { get { return this.source; } } public IList Channels { get { return this.channels; } } public int Count { get { return channels.Count; } } public int DuplicateUidCount { get { return duplicateUidCount; } } public int DuplicateProgNrCount { get { return duplicateProgNrCount; } } public bool ReadOnly { get; set; } public int MaxChannelNameLength { get; set; } public int PresetProgramNrCount { get; private set; } #region Caption public string Caption { get { string cap = this.ShortCaption; int validChannelCount = this.Channels.Count(ch => ch.OldProgramNr != -1); return cap + " (" + validChannelCount + ")"; } } #endregion #region InsertProgramNumber public int InsertProgramNumber { get { return this.Count == 0 ? 1 : this.insertProgramNr; } set { this.insertProgramNr = Math.Max(this.FirstProgramNumber, value); } } #endregion public int FirstProgramNumber { get; set; } #region AddChannel() public string AddChannel(ChannelInfo ci) { IList others; if (this.channelByUid.TryGetValue(ci.Uid, out others)) ++duplicateUidCount; else { others = new List(); this.channelByUid.Add(ci.Uid, others); } others.Add(ci); string warning2 = null; bool isDupeProgNr = false; if (ci.OldProgramNr != -1) { ChannelInfo other; this.channelByProgNr.TryGetValue(ci.OldProgramNr, out other); if (other != null) { warning2 = string.Format(Resources.ChannelList_ProgramNrAssignedToMultipleChannels, this.ShortCaption, ci.OldProgramNr, other.RecordIndex, other.Name, ci.RecordIndex, ci.Name); ++duplicateProgNrCount; isDupeProgNr = true; } } if (!isDupeProgNr) this.channelByProgNr[ci.OldProgramNr] = ci; var lowerName = ci.Name.ToLower().Trim(); var byNameList = this.channelByName.TryGet(lowerName); if (byNameList == null) { byNameList = new List(); this.channelByName[lowerName] = byNameList; } byNameList.Add(ci); if (ci.ProgramNrPreset != 0) ++this.PresetProgramNrCount; this.channels.Add(ci); return warning2; } #endregion #region GetChannelByUid() public IList GetChannelByUid(string uid) { IList channel; this.channelByUid.TryGetValue(uid, out channel); return channel ?? new List(0); } #endregion #region ToString() public override string ToString() { return this.Caption; } #endregion #region GetChannelByName() public IEnumerable GetChannelByName(string name) { var hits = this.channelByName.TryGet(name.ToLower().Trim()); return hits ?? new List(); } #endregion #region GetChannelByNewProgNr() public IList GetChannelByNewProgNr(int newProgNr) { return this.channels.Where(c => c.NewProgramNr == newProgNr).ToList(); } #endregion #region RemoveChannel() public void RemoveChannel(ChannelInfo channel) { this.channels.Remove(channel); var list = this.channelByUid.TryGet(channel.Uid); if (list != null && list.Contains(channel)) list.Remove(channel); list = this.channelByName.TryGet(channel.Name); if (list != null && list.Contains(channel)) list.Remove(channel); var chan = this.channelByProgNr.TryGet(channel.OldProgramNr); if (ReferenceEquals(chan, channel)) this.channelByProgNr.Remove(channel.OldProgramNr); } #endregion } }