using System; using System.Collections.Generic; namespace FasdDesktopUi.Basics.Services.Models { internal static class TicketOverviewCountProcessor { internal static ScopeCountProcessingResult Calculate( IReadOnlyDictionary currentCounts, IEnumerable overviewKeys, TileScope scope, IDictionary incomingCounts, bool hasInitializedScope) { if (overviewKeys == null) throw new ArgumentNullException(nameof(overviewKeys)); var updatedCounts = new Dictionary(StringComparer.OrdinalIgnoreCase); if (currentCounts != null) { foreach (var kvp in currentCounts) { updatedCounts[kvp.Key] = kvp.Value; } } var changes = new List(); foreach (var key in overviewKeys) { var previous = currentCounts != null && currentCounts.TryGetValue(key, out var counts) ? counts : TileCounts.Empty; var incoming = incomingCounts != null && incomingCounts.TryGetValue(key, out var value) ? value : 0; TileCounts updated; int oldValue; if (scope == TileScope.Role) { updated = new TileCounts(previous.Personal, incoming); oldValue = previous.Role; } else { updated = new TileCounts(incoming, previous.Role); oldValue = previous.Personal; } updatedCounts[key] = updated; if (hasInitializedScope && oldValue != incoming) changes.Add(new TileCountChange(key, scope, oldValue, incoming)); } return new ScopeCountProcessingResult(updatedCounts, changes, !hasInitializedScope); } } internal sealed class ScopeCountProcessingResult { internal ScopeCountProcessingResult( IReadOnlyDictionary updatedCounts, IReadOnlyList changes, bool isInitialization) { UpdatedCounts = updatedCounts ?? throw new ArgumentNullException(nameof(updatedCounts)); Changes = changes ?? Array.Empty(); IsInitialization = isInitialization; } internal IReadOnlyDictionary UpdatedCounts { get; } internal IReadOnlyList Changes { get; } internal bool IsInitialization { get; } } }