using System.Text.Json; using Titano.Core.Archiving; using Titano.Core.Model; namespace Titano.Data.Archiving; /// Sidecar records to index rows. Fields the sidecar does not carry are kept from the existing row. internal static class SequenceMapping { public static Frame ToFrame(SidecarFrame f, Guid sequenceId, SequencePaths sequence) => new() { Id = f.Id, SequenceId = sequenceId, Index = f.I, RelativePath = sequence.ArchiveRelative(f.File), FileName = Path.GetFileName(f.File), OriginalFileName = f.Original, Size = f.Size, Sha256 = f.Sha256, CapturedAt = f.CapturedAt, UtcOffsetMinutes = f.UtcOffset, TimestampSource = f.TimeSource, ExposureTime = f.Exposure, FNumber = f.FNumber, Iso = f.Iso, FocalLength = f.Focal, ExposureBias = f.Bias, WhiteBalance = f.Wb, Orientation = f.Orientation, Width = f.W, Height = f.H, Format = f.Format, PreviewOffset = f.PreviewOffset, PreviewLength = f.PreviewLength, Flags = f.Flags, }; public static List ToFrames(SequenceManifest manifest, SequencePaths sequence) => manifest.Frames.Select(f => ToFrame(f, manifest.Id, sequence)).ToList(); /// The current row; its render, analysis, sync and ownership fields are kept, and so are its /// title, tags, rating and notes when its settings version is ahead of the sidecar's (edited in the index only). public static Sequence ToSequence(SequenceManifest m, Guid archiveId, SequencePaths sequence, Sequence? existing) { var indexAhead = existing is not null && existing.SettingsVersion > m.Versions.Settings; var times = m.Frames.Where(f => f.CapturedAt is not null).Select(f => f.CapturedAt!.Value).Order().ToList(); var flagged = m.Frames.Any(f => (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0); return new Sequence { Id = m.Id, ArchiveId = archiveId, Title = indexAhead ? existing!.Title : m.Title, Folder = sequence.RelativeFolder, CameraMake = m.Camera?.Make, CameraModel = m.Camera?.Model, CameraSerial = m.Camera?.Serial, Lens = m.Lens, FirstCapture = times.Count > 0 ? times[0] : null, LastCapture = times.Count > 0 ? times[^1] : null, FrameCount = m.Frames.Count, MedianInterval = MedianInterval(times), TotalBytes = m.Frames.Sum(f => f.Size), Status = flagged ? m.Status | SequenceStatus.HasErrors : m.Status & ~SequenceStatus.HasErrors, VerifiedAt = m.VerifiedAt, Tags = indexAhead ? existing!.Tags : m.Tags, Rating = indexAhead ? existing!.Rating : m.Rating, Notes = indexAhead ? existing!.Notes : m.Notes, SettingsVersion = indexAhead ? existing!.SettingsVersion : m.Versions.Settings, FramesVersion = m.Versions.Frames, UpdatedAt = m.UpdatedAt, RenderedCount = existing?.RenderedCount ?? 0, HeroFrameId = existing?.HeroFrameId, SettingsJson = existing?.SettingsJson ?? ReadProject(sequence), AnalysisJson = existing?.AnalysisJson, Origin = existing?.Origin ?? SequenceOrigin.Local, SyncState = existing?.SyncState ?? SyncState.LocalOnly, OwnerUserId = existing?.OwnerUserId ?? (Guid.TryParse(m.CreatedBy, out var owner) ? owner : null), WarningCount = existing?.WarningCount ?? 0, }; } private static double? MedianInterval(List times) { if (times.Count < 2) return null; var gaps = new double[times.Count - 1]; for (var i = 1; i < times.Count; i++) gaps[i - 1] = (times[i] - times[i - 1]).TotalSeconds; Array.Sort(gaps); var mid = gaps.Length / 2; return gaps.Length % 2 == 1 ? gaps[mid] : (gaps[mid - 1] + gaps[mid]) / 2; } /// The project file is owned by the editor; a rebuilt index takes its text as-is when it is valid JSON. private static string? ReadProject(SequencePaths sequence) { if (!File.Exists(sequence.ProjectPath)) return null; var text = File.ReadAllText(sequence.ProjectPath); try { using (JsonDocument.Parse(text)) return text; } catch (JsonException) { return null; } } }