using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving.Jobs;
namespace Titano.App.Editor;
///
/// Where the sequence open in the editor lives: the local archive () or a server the
/// client is connected to (: proxies from the cache, project and jobs through the
/// API). The editor reads frames, saves the project, queues jobs and follows them only through this interface.
///
public interface ISequenceSource : IAsyncDisposable
{
///
/// The sequence lives on a server (BRIEF §14.5 "edit with proxies, render where the originals are"): its project is
/// saved there and its renders and analyses run there, one job per preset.
///
bool IsRemote { get; }
///
/// The project is shown, not changed: a server sequence for an account without the Edit right, or one whose frames
/// changed on the server (). The editor disables every change and the source refuses to
/// store one. It can turn true while the sequence is open ().
///
bool IsReadOnly { get; }
///
/// The frames this PC shows are no longer the sequence's (frames added or removed on the server since its proxies
/// were fetched): frame positions in the project would point at other frames, so nothing is saved or rendered until
/// the sequence is opened again with fresh proxies.
///
bool IsOutdated { get; }
/// changed (any thread).
event Action? ReadOnlyChanged;
/// A job that may concern the sequence changed (any thread): this PC's engine, or the server's events.
event Action? JobChanged;
/// Everything the editor shows of the sequence; read off the UI thread.
/// The sequence is gone, or its project cannot be read (corrupt, or written by a newer Titano).
Task LoadAsync(CancellationToken cancellationToken);
///
/// Whether the frames read last are still the sequence's, in the same order: frames inserted
/// or removed since (a sync download, an import of earlier photos, a scan) moved the stored project's positions. A
/// server sequence whose frames changed becomes .
///
/// The sequence is gone.
Task FramesCurrentAsync(CancellationToken cancellationToken);
///
/// Frame in linear light, oriented, long edge at most ; null
/// when the frame cannot be read. The caller owns the buffer.
///
ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken);
///
/// Stores , edited from (the version last loaded or saved). A
/// change made meanwhile by someone else is kept where this edit left the value alone ().
/// Returns the stored project.
///
/// The editor's own reason the project was not stored, worded for the user.
Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken);
/// Queues an analysis or render job for the sequence; returns the job id.
Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken);
/// Recent jobs that may concern the sequence (queued, running, finished), for the busy state and the cost of Analyse.
Task> ListJobsAsync(CancellationToken cancellationToken);
/// Cancels a job of the sequence that waits or runs; its new state arrives through .
Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken);
/// Queues a failed or cancelled job of the sequence again from its checkpoint.
Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken);
}
/// A sequence as the editor opens it.
/// Frame rows in index order.
/// Light analysis of the frame metadata (BRIEF §10.1).
/// The saved deep analysis when it describes the current frames, else null ( says why).
/// Dominant oriented full-resolution width (0 when unknown).
/// Default output folder of renders; null when the sequence is not on this PC.
/// Finished renders, newest first (an empty output path when the file is not on this PC).
/// This PC's projects kept by "Save both" when a sync conflict was settled, newest first.
public sealed record SequenceContent(
Sequence Sequence,
IReadOnlyList Frames,
ProjectSettings Project,
LightAnalysisReport Light,
DeepAnalysisReport? Deep,
int SourceWidth,
int SourceHeight,
string? ExportsFolder,
IReadOnlyList Renders,
IReadOnlyList SavedCopies)
{
/// Flags of frames that never reach a render (as the render job skips them).
public const FrameFlags NotRendered = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded | FrameFlags.Undecodable;
public int Count => Frames.Count;
///
/// A saved deep analysis exists and reads, but describes another number of frames (frames were added or removed since);
/// false when there is none or it cannot be read now (archive offline, file moved or damaged, a server that sends none).
///
public bool DeepOfOtherFrames { get; init; }
/// Frames the render skips besides the project's exclusions.
public IEnumerable NotRenderable()
{
for (var i = 0; i < Frames.Count; i++)
{
if ((Frames[i].Flags & NotRendered) != 0) yield return i;
}
}
}