using Chronos.Core.Model;
using Chronos.Data.Repositories;
namespace Chronos.App.Library;
///
/// What the Library's Year and Camera choices and its timeline bands count (BRIEF ยง4.4 mixed mode): the index's
/// figures, the server's sequences without a copy here, or both, as the origin filter says.
///
public static class LibraryFacets
{
///
/// with its years, months and cameras replaced by those of the origins in view: the index's
/// own unless only the server's sequences show, plus the remote cards' unless only this PC's show.
///
public static ArchiveStats Of(ArchiveStats local, IReadOnlyList remote, OriginFilter origin)
{
var keepLocal = origin != OriginFilter.Server;
var years = keepLocal ? local.ByYear.ToDictionary(y => y.Year) : [];
var months = keepLocal ? local.ByMonth.ToDictionary(m => (m.Year, m.Month)) : [];
var cameras = keepLocal ? local.ByCamera.ToDictionary(c => (c.Make, c.Model)) : [];
foreach (var s in origin == OriginFilter.Local ? [] : remote)
{
if (s.FirstCapture is { } at)
{
years[at.Year] = years.TryGetValue(at.Year, out var y)
? y with { Sequences = y.Sequences + 1, Photos = y.Photos + s.FrameCount, Bytes = y.Bytes + s.TotalBytes }
: new YearBucket(at.Year, 1, s.FrameCount, s.TotalBytes);
months[(at.Year, at.Month)] = months.TryGetValue((at.Year, at.Month), out var m)
? m with { Sequences = m.Sequences + 1, Photos = m.Photos + s.FrameCount }
: new MonthBucket(at.Year, at.Month, 1, s.FrameCount);
}
cameras[(s.CameraMake, s.CameraModel)] = cameras.TryGetValue((s.CameraMake, s.CameraModel), out var c)
? c with { Sequences = c.Sequences + 1, Photos = c.Photos + s.FrameCount, Bytes = c.Bytes + s.TotalBytes }
: new CameraBucket(s.CameraMake, s.CameraModel, 1, s.FrameCount, s.TotalBytes);
}
return local with
{
ByYear = [.. years.Values.OrderBy(y => y.Year)],
ByMonth = [.. months.Values.OrderBy(m => m.Year).ThenBy(m => m.Month)],
ByCamera = [.. cameras.Values],
};
}
}