using System.Windows.Input;
using Titano.Core.Advice;
using Titano.Core.Analysis;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Ui.Controls;
using Titano.Ui.Mvvm;
namespace Titano.App.Editor;
/// Columns of the Frames table, each sortable (DESIGN §4 tables).
public enum FrameSort
{
Number,
Time,
Exposure,
Iso,
Aperture,
Size,
Tier,
Flags,
}
/// A sortable column header of the Frames table: its sort state and the command that sorts on it.
public sealed class FrameColumn(FramesTab owner, FrameSort sort) : ObservableObject
{
public FrameSort Sort { get; } = sort;
public bool IsSorted => owner.SortBy == Sort;
public bool IsDescending => IsSorted && owner.SortDescending;
public ICommand SortCommand { get; } = new RelayCommand(() => owner.SortOn(sort));
public void Refresh()
{
OnPropertyChanged(nameof(IsSorted));
OnPropertyChanged(nameof(IsDescending));
}
}
/// One frame of the Frames table; texts are formatted when shown (the table is virtualised).
public sealed class FrameRow(FramesTab owner, Frame frame, int index) : ObservableObject
{
/// Archive flags named in the table (the others are internal or shown elsewhere).
public const FrameFlags Shown = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Duplicate | FrameFlags.Undecodable | FrameFlags.Black | FrameFlags.White;
private static readonly FrameFlags[] ShownFlags =
[FrameFlags.Missing, FrameFlags.Corrupt, FrameFlags.Duplicate, FrameFlags.Undecodable, FrameFlags.Black, FrameFlags.White];
public int Index { get; } = index;
public Frame Frame => frame;
public string Number => (Index + 1).ToString("D4", Loc.Culture);
public string Time => frame.CapturedAt?.ToString("T", Loc.Culture) ?? string.Empty;
public string Exposure => frame.ExposureTime is > 0 and var t ? EditorText.Shutter(t) : string.Empty;
public string Iso => frame.Iso?.ToString(Loc.Culture) ?? string.Empty;
public string Aperture => frame.FNumber is > 0 and var f ? Loc.L("Editor.Shot.Aperture", f.ToString("0.#", Loc.Culture)) : string.Empty;
public string Size => frame.Width > 0 ? EditorText.Size(frame.Width, frame.Height) : string.Empty;
/// Decode tier: the format, and "preview" when only the embedded preview decodes.
public string Tier => (frame.Flags & FrameFlags.PreviewOnly) != 0 ? Loc.L("Editor.Frames.PreviewOnly", frame.Format) : frame.Format.ToString();
/// "Excluded · black", the project's exclusion first, then the archive's flags.
public string Flags
{
get
{
var parts = new List();
if (IsExcluded) parts.Add(Loc.L("Editor.Frames.Excluded"));
if (IsHero) parts.Add(Loc.L("Editor.Frames.Hero"));
foreach (var flag in ShownFlags)
{
if ((frame.Flags & flag) != 0) parts.Add(Loc.L("Editor.Frames.Flag." + flag));
}
return string.Join(Loc.Separator, parts);
}
}
public bool IsExcluded => owner.IsExcluded(Index);
public bool IsHero => owner.HeroIndex == Index;
public string ExcludeText => Loc.L(IsExcluded ? "Editor.Frames.Include" : "Editor.Frames.Exclude");
public string ExcludeToolTip => owner.Editor.LockedToolTip ?? Loc.L(IsExcluded ? "Tooltips.Editor.Frames.Include" : "Tooltips.Editor.Frames.Exclude");
/// A job of the sequence started or ended: the action tooltips say why they wait.
public void RefreshBusy() => OnPropertyChanged(nameof(ExcludeToolTip));
/// The project's exclusions or hero changed.
public void Refresh()
{
OnPropertyChanged(nameof(IsExcluded));
OnPropertyChanged(nameof(IsHero));
OnPropertyChanged(nameof(Flags));
OnPropertyChanged(nameof(ExcludeText));
OnPropertyChanged(nameof(ExcludeToolTip));
}
}
/// A finding of the analyses with the frame to jump to.
public sealed record FindingItem(StatusKind Kind, string Text, int? Frame);
///
/// Frames (BRIEF §12.3): the virtualised frame table (index, time, exposure, ISO, aperture, size, tier, flags) with
/// exclude/include and hero, and the warnings of the analyses; Optimise adds the advised exclusions and hero.
///
public sealed class FramesTab : EditorTab
{
private HashSet _excluded = [];
private int? _hero;
private FrameRow[] _rows = [];
private IReadOnlyList _frames = [];
private IReadOnlyList _findings = [];
private FrameRow? _selected;
public FramesTab(EditorViewModel editor)
: base(editor, "Frames", AdvisorSection.Frames)
{
ToggleExcludeCommand = new RelayCommand(p => ToggleExcluded(p is FrameRow row ? row.Index : Editor.CurrentIndex), _ => !Editor.IsLocked);
SetHeroCommand = new RelayCommand(p => Editor.Session.SetHero(p is FrameRow row ? row.Index : Editor.CurrentIndex), _ => !Editor.IsLocked);
SeekCommand = new RelayCommand(p =>
{
if (p is FindingItem { Frame: { } frame }) Editor.CurrentIndex = frame;
});
Columns = [.. Enum.GetValues().Select(c => new FrameColumn(this, c))];
Load();
}
/// Rows in the table's order ().
public IReadOnlyList Frames { get => _frames; private set => Set(ref _frames, value); }
/// Column headers in order.
public IReadOnlyList Columns { get; }
public FrameSort SortBy { get; private set; } = FrameSort.Number;
public bool SortDescending { get; private set; }
public IReadOnlyList Findings
{
get => _findings;
private set
{
if (Set(ref _findings, value)) OnPropertyChanged(nameof(HasFindings));
}
}
public bool HasFindings => _findings.Count > 0;
/// The row of the current frame (selecting a row seeks the preview).
public FrameRow? Selected
{
get => _selected;
set
{
if (!Set(ref _selected, value) || value is null) return;
Editor.CurrentIndex = value.Index;
}
}
public int? HeroIndex => _hero;
/// "Frame 0413", or "Not set".
public string HeroText => _hero is { } h ? Loc.L("Editor.Frames.HeroValue", (h + 1).ToString("D4", Loc.Culture)) : Loc.L("Editor.Frames.HeroNone");
/// "12 frames", "None".
public string ExcludedText => _excluded.Count > 0 ? Loc.Plural("Editor.Value.Frames", _excluded.Count) : Loc.L("Editor.Frames.ExcludedNone");
public bool IsCurrentExcluded => IsExcluded(Editor.CurrentIndex);
public string CurrentExcludeText => Loc.L(IsCurrentExcluded ? "Editor.Frames.IncludeFrame" : "Editor.Frames.ExcludeFrame");
public string CurrentExcludeToolTip => Editor.LockedToolTip ?? Loc.L(IsCurrentExcluded ? "Tooltips.Editor.Frames.Include" : "Tooltips.Editor.Frames.Exclude");
/// What Set hero does, or why it waits.
public string HeroToolTip => Editor.LockedToolTip ?? Loc.L("Tooltips.Editor.Frames.Hero");
public override bool CanReset => false;
public ICommand ToggleExcludeCommand { get; }
public ICommand SetHeroCommand { get; }
public ICommand SeekCommand { get; }
public bool IsExcluded(int index) => _excluded.Contains(index);
/// Sorts the table on ; the same column again reverses the order.
public void SortOn(FrameSort column)
{
SortDescending = column == SortBy && !SortDescending;
SortBy = column;
Frames = Sorted();
foreach (var header in Columns) header.Refresh();
}
public override void Refresh()
{
base.Refresh();
var project = Editor.Session.Project;
var excluded = project.ExcludedFrames.ToHashSet();
if (excluded.SetEquals(_excluded) && project.HeroFrame == _hero) return;
var changed = new HashSet(excluded);
changed.SymmetricExceptWith(_excluded);
if (_hero is { } oldHero) changed.Add(oldHero);
if (project.HeroFrame is { } newHero) changed.Add(newHero);
_excluded = excluded;
_hero = project.HeroFrame;
foreach (var index in changed)
{
if ((uint)index < (uint)_rows.Length) _rows[index].Refresh();
}
OnPropertyChanged(nameof(HeroText));
OnPropertyChanged(nameof(ExcludedText));
OnCurrentFrameChanged();
}
public override void OnContentChanged()
{
Load();
base.OnContentChanged();
}
public override void OnBusyChanged()
{
base.OnBusyChanged();
OnPropertyChanged(nameof(HeroToolTip));
OnPropertyChanged(nameof(CurrentExcludeToolTip));
foreach (var row in _rows) row.RefreshBusy();
}
public override void OnCurrentFrameChanged()
{
var index = Editor.CurrentIndex;
if ((uint)index < (uint)_rows.Length && !ReferenceEquals(_selected, _rows[index]))
{
_selected = _rows[index];
OnPropertyChanged(nameof(Selected));
}
OnPropertyChanged(nameof(IsCurrentExcluded));
OnPropertyChanged(nameof(CurrentExcludeText));
OnPropertyChanged(nameof(CurrentExcludeToolTip));
}
private void Load()
{
var content = Editor.Session.Content;
_excluded = Editor.Session.Project.ExcludedFrames.ToHashSet();
_hero = Editor.Session.Project.HeroFrame;
_rows = [.. content.Frames.Select((f, i) => new FrameRow(this, f, i))];
Frames = Sorted();
Findings = [.. Editor.Session.Advice.Findings.OrderByDescending(f => f.Severity).Select(ToItem)];
_selected = null;
OnCurrentFrameChanged();
}
private IReadOnlyList Sorted()
{
IEnumerable rows = SortBy switch
{
FrameSort.Time => _rows.OrderBy(r => r.Frame.CapturedAt),
FrameSort.Exposure => _rows.OrderBy(r => r.Frame.ExposureTime),
FrameSort.Iso => _rows.OrderBy(r => r.Frame.Iso),
FrameSort.Aperture => _rows.OrderBy(r => r.Frame.FNumber),
FrameSort.Size => _rows.OrderBy(r => (long)r.Frame.Width * r.Frame.Height),
FrameSort.Tier => _rows.OrderBy(r => r.Tier, StringComparer.CurrentCulture),
// Flagged frames together: excluded first, then by how many archive flags they carry.
FrameSort.Flags => _rows.OrderBy(r => (r.IsExcluded ? 0 : 1) * 64 - BitCount(r.Frame.Flags & FrameRow.Shown)),
_ => _rows,
};
if (SortBy != FrameSort.Number && rows is IOrderedEnumerable ordered) rows = ordered.ThenBy(r => r.Index);
return SortDescending ? [.. rows.Reverse()] : [.. rows];
}
private static int BitCount(FrameFlags flags) => System.Numerics.BitOperations.PopCount((uint)flags);
private static FindingItem ToItem(Finding finding) => new(
finding.Severity switch
{
FindingSeverity.Error => StatusKind.Error,
FindingSeverity.Warning => StatusKind.Warning,
_ => StatusKind.NotAnalysed,
},
Loc.Text(finding.Key, finding.Arguments),
finding.Frames.Count > 0 ? finding.Frames[0] : null);
private void ToggleExcluded(int index) => Editor.Session.SetExcluded([index], !IsExcluded(index));
}