using Chronos.App.Archiving;
using Chronos.Core.Model;
using Chronos.Data.Repositories;
namespace Chronos.App.Library;
///
/// The Library's timeline layout (BRIEF ยง12.2): one band per capture year, newest first, with the density of photos
/// per month (the Archive heatmap's figures), and the way between a band's month and the sequences of the grid.
///
public sealed class LibraryTimeline
{
private LibraryTimeline(IReadOnlyList bands, IReadOnlyList years)
{
Bands = bands;
Years = years;
}
public static LibraryTimeline Empty { get; } = new([], []);
/// One row per year, newest first; months to come are NaN.
public IReadOnlyList Bands { get; }
/// The year of each band.
public IReadOnlyList Years { get; }
public static LibraryTimeline From(ArchiveStats stats, DateTime today)
{
var rows = ArchiveFigures.PhotosPerMonth(stats, today).Reverse().ToList();
var first = stats.ByMonth.Count == 0 ? 0 : stats.ByMonth.Max(m => m.Year);
return new LibraryTimeline(rows, [.. Enumerable.Range(0, rows.Count).Select(i => first - i)]);
}
/// Year and month of a band's slot; null outside the bands.
public (int Year, int Month)? MonthAt(int band, int slot) =>
band >= 0 && band < Years.Count && slot is >= 0 and < 12 ? (Years[band], slot + 1) : null;
/// Band and slot of a month; (โ1, โ1) when no band shows that year.
public (int Band, int Slot) SlotOf(int year, int month)
{
var band = IndexOf(Years, year);
return band < 0 || month is < 1 or > 12 ? (-1, -1) : (band, month - 1);
}
/// Position of the first sequence first captured in that month, in list order; โ1 when none of is.
public static int FirstOfMonth(IReadOnlyList sequences, int year, int month)
{
for (var i = 0; i < sequences.Count; i++)
{
if (sequences[i].FirstCapture is { } at && at.Year == year && at.Month == month) return i;
}
return -1;
}
private static int IndexOf(IReadOnlyList values, int value)
{
for (var i = 0; i < values.Count; i++)
{
if (values[i] == value) return i;
}
return -1;
}
}