using System.Globalization; using System.Windows.Media; using Chronos.App.Archiving; using Chronos.App.Infrastructure; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Data.Videos; using Chronos.Protocol; using Chronos.Ui.Controls; using Chronos.Ui.Mvvm; using Chronos.Ui.Visuals; namespace Chronos.App.Videos; /// The words of the Videos section: kinds, states, details and the errors of the videos library. public static class VideoText { /// "Videos", "Stills", "Image sequences"; a newer kind as it is. public static string Kind(string kind) => kind switch { RenderKind.Video => Loc.L("Videos.Kind.Video"), RenderKind.Still => Loc.L("Videos.Kind.Still"), RenderKind.Sequence => Loc.L("Videos.Kind.Sequence"), _ => kind, }; /// "0:42" for a video; empty for a still, which has no duration. public static string Duration(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); return entry.Kind == RenderKind.Still || entry.DurationSeconds <= 0 ? string.Empty : Formatting.Elapsed(TimeSpan.FromSeconds(entry.DurationSeconds)); } /// "3840×2160 · HEVC" (the size and codec, left to right in every language). public static string Format(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); var size = entry.Width > 0 && entry.Height > 0 ? entry.Width.ToString(CultureInfo.InvariantCulture) + "×" + entry.Height.ToString(CultureInfo.InvariantCulture) : string.Empty; var codec = VideoSources.CodecText(entry.Codec); return size.Length == 0 ? codec : codec.Length == 0 ? size : size + Loc.Separator + codec; } /// "3840×2160 · HEVC · 0:42 · 1.2 GB": the parts a render has (a still has no duration), joined by the separator. public static string Details(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); return string.Join(Loc.Separator, new[] { Format(entry), Duration(entry), Formatting.Bytes(entry.Bytes) }.Where(p => p.Length > 0)); } /// The state chips of a render: where its file is, whether it was copied, and a missing file. public static IReadOnlyList States(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); var states = new List(); if (entry.Missing) states.Add(new CardStatus(StatusKind.Warning, Loc.L("Videos.State.Missing"), Loc.L("Tooltips.Videos.Missing"))); if (entry.IsUnsent) states.Add(new CardStatus(StatusKind.Queued, Loc.L("Videos.State.NotSent"), Loc.L("Tooltips.Videos.NotSent"))); else if (entry.IsLocal && entry.IsOnServer) states.Add(new CardStatus(StatusKind.Synced, Loc.L("Videos.State.HereAndServer"), Loc.L("Tooltips.Videos.HereAndServer"))); else if (entry.IsLocal) states.Add(new CardStatus(StatusKind.Done, Loc.L("Videos.State.Here"), Loc.L("Tooltips.Videos.Here"))); else if (entry.IsOnServer) states.Add(new CardStatus(StatusKind.Remote, Loc.L("Videos.State.OnServer"), Loc.L("Tooltips.Videos.OnServer"))); if (entry.Copies.Count > 0) { states.Add(new CardStatus(StatusKind.Rendered, Loc.L("Videos.State.Copied"), Loc.L("Tooltips.Videos.Copied", string.Join(Environment.NewLine, entry.Copies.Select(Formatting.LeftToRight))))); } return states; } /// The sentence of a videos library error (); is the file or sequence it is about. public static string Error(VideoLibraryException ex, string? name = null) { ArgumentNullException.ThrowIfNull(ex); return ex.Code switch { VideoErrors.FolderUnset => Loc.L("Errors.Videos.FolderUnset"), VideoErrors.FolderInvalid => Loc.L("Errors.Videos.FolderInsideArchive"), VideoErrors.PlanChanged => Loc.L("Errors.Videos.Fingerprint"), VideoErrors.HashMismatch => Loc.L("Errors.Videos.TrashMismatch"), VideoErrors.NoSpace => Loc.L("Errors.Videos.NoSpace", name ?? string.Empty), VideoErrors.SequenceGone => Loc.L("Errors.Videos.SequenceGone", name ?? string.Empty), VideoErrors.Busy => Loc.L("Errors.Videos.Busy", name ?? string.Empty), _ => ex.Message, }; } /// Why a copy into 's folder is refused (a tooltip), from . public static string FolderReason(string problem) => problem == VideoErrors.FolderUnset ? Loc.L("Tooltips.Videos.FolderUnset") : Loc.L("Errors.Videos.FolderInsideArchive"); /// /// Play's tooltip for : what Play does for a file on this PC; for a video only the server holds, /// that it plays here once copied to this PC; for a file that is gone, that it is missing. /// public static string PlayToolTip(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); if (entry.HasLocalFile) return Loc.L("Tooltips.Videos.Play"); if (entry.Missing) return Loc.L("Tooltips.Videos.Missing"); return entry.IsOnServer ? Loc.L("Tooltips.Videos.PlayServer") : Loc.L("Tooltips.Videos.Play"); } /// "In use by a job", "Not found". public static string BlockReason(string reason) => reason == VideoBlockReasons.Busy ? Loc.L("Videos.Blocked.Busy") : Loc.L("Videos.Blocked.NotFound"); } /// One render of the Videos section, as a card of the grid or a row of the list. public sealed class VideoRowViewModel(VideoEntry entry) : ObservableObject { private bool _isSelected; private ImageSource? _poster; private bool _posterRequested; public VideoEntry Entry { get; } = entry; public string Title => Entry.Title; /// The preset name (and the project's, when a project was rendered). public string Subtitle => Entry.ProjectName is { Length: > 0 } project ? Entry.PresetName + Loc.Separator + project : Entry.PresetName; public string DetailsText => VideoText.Details(Entry); public string FormatText => VideoText.Format(Entry); public string DurationText => VideoText.Duration(Entry); public string SizeText => Formatting.Bytes(Entry.Bytes); public string DateText => Formatting.Date(Entry.Created) + " " + Formatting.TimeOfDay(Entry.Created); public string KindText => VideoText.Kind(Entry.Kind); public IReadOnlyList States { get; } = VideoText.States(entry); /// The card's and the row's tooltip: what it is, then how to select it. public string ToolTip => Entry.IsUnsent ? Loc.L("Tooltips.Videos.WorkspaceRow") : Loc.L("Tooltips.Videos.Select"); /// /// Play's tooltip: what it does, or why it waits (a server's video plays here once it is copied to this PC, a file /// that is gone plays nowhere). /// public string PlayToolTip => VideoText.PlayToolTip(Entry); /// The section listing the row: its menu's words are those of the commands it runs, on what they act on. internal Sections.VideosViewModel? Section { get; set; } private IReadOnlyList MenuTargets => Section?.TargetsOf(this) ?? [this]; /// The menu's Play tooltip: why it waits, else what it does. public string MenuPlayToolTip => Sections.VideosViewModel.PlayToolTipOf(MenuTargets); public string MenuShowInFolderToolTip => Sections.VideosViewModel.ShowInFolderToolTipOf(MenuTargets); /// Copy to folder, or Download for the server's videos alone. public string MenuCopyText => Sections.VideosViewModel.CopyTextOf(MenuTargets); public string MenuCopyToolTip => Section?.CopyToolTipOf(MenuTargets) ?? Loc.L("Tooltips.Videos.Copy"); public string MenuCopyOnServerToolTip => Section?.CopyOnServerToolTipOf(MenuTargets) ?? Loc.L("Tooltips.Videos.CopyOnServer"); public string MenuOpenInEditToolTip => Sections.VideosViewModel.OpenInEditToolTipOf(MenuTargets); public string MenuDeleteToolTip => Section?.DeleteToolTipOf(MenuTargets) ?? Loc.L("Tooltips.Videos.Delete"); /// The selection or the section's state changed: the menu's words are read again. internal void RefreshMenu() { OnPropertyChanged(nameof(MenuPlayToolTip)); OnPropertyChanged(nameof(MenuShowInFolderToolTip)); OnPropertyChanged(nameof(MenuCopyText)); OnPropertyChanged(nameof(MenuCopyToolTip)); OnPropertyChanged(nameof(MenuCopyOnServerToolTip)); OnPropertyChanged(nameof(MenuOpenInEditToolTip)); OnPropertyChanged(nameof(MenuDeleteToolTip)); } /// The file name, left to right in every language. public string FileNameText => Formatting.LeftToRight(Entry.FileName); public bool IsSelected { get => _isSelected; set { if (Set(ref _isSelected, value)) SelectionChanged?.Invoke(this); } } /// The sequence's cover, or the still itself; null until loaded (). public ImageSource? Poster { get => _poster; internal set => Set(ref _poster, value); } /// The card came on screen: its poster is loaded once by . public void RequestPoster() { if (_posterRequested || _poster is not null) return; _posterRequested = true; PosterRequested?.Invoke(this); } internal event Action? SelectionChanged; internal event Action? PosterRequested; } /// /// The dry run of a delete of videos (decision 17) in the words of its confirmation: what goes into the trash (and how /// many bytes), the renders whose file is missing (only their entry goes), the renders left where they are and why, and /// that copies in the videos folder stay. This PC's renders and the server's are planned apart and confirmed together. /// /// What the section shows of each selected render ("title · file"), which names a render that stays /// (the server's plan gives only its id); a render not among them is named by its id. public sealed record VideoDeletePlans(VideoDeletePlan? Local, VideoDeletePlanDto? Server, IReadOnlyDictionary? Names = null) { /// "Harbour sunset · harbour-4k.mp4": how a render that stays is named in the confirmation. public static string NameOf(VideoEntry entry) { ArgumentNullException.ThrowIfNull(entry); return entry.Title + Loc.Separator + Formatting.LeftToRight(entry.FileName); } /// The renders that go. public int Count => (Local?.Items.Count ?? 0) + (Server?.Items.Count ?? 0); /// The bytes that move into the trash. public long Bytes => (Local?.Bytes ?? 0) + (Server?.Bytes ?? 0); /// The renders left where they are. public int Blocked => (Local?.Blocked.Count ?? 0) + (Server?.Blocked.Count ?? 0); /// The renders whose file is missing: only their entry goes. public int Missing => (Local?.Items.Count(i => i.Missing) ?? 0) + (Server?.Items.Count(i => i.Missing) ?? 0); /// The lines of what goes, then of what stays and why ( each, the rest counted). public IReadOnlyList Lines() { var goes = (Local?.Items.Select(i => (i.SequenceTitle, i.FileName, i.Render.Bytes, i.Missing)) ?? []) .Concat(Server?.Items.Select(i => (i.SequenceTitle, FileName: i.FileName ?? string.Empty, i.Bytes, i.Missing)) ?? []).ToList(); var lines = new List(); foreach (var (title, file, bytes, missing) in goes.Take(DeletePlans.ListedLines)) { lines.Add(Loc.L("Archive.Delete.Line", title, Formatting.LeftToRight(file), missing ? Loc.L("Videos.State.Missing") : Formatting.Bytes(bytes))); } if (goes.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", goes.Count - DeletePlans.ListedLines)); var stays = (Local?.Blocked.Select(b => (Named(b.RenderId, b.Video is { } video ? video.SequenceTitle + Loc.Separator + Formatting.LeftToRight(video.FileName) : null), b.Reason)) ?? []) .Concat(Server?.Blocked.Select(b => (Named(b.Id, null), b.Reason)) ?? []).ToList(); if (stays.Count > 0) lines.Add(Loc.L("Videos.Delete.Blocked", Formatting.Count(stays.Count))); foreach (var (name, reason) in stays.Take(DeletePlans.ListedLines)) lines.Add(name + Loc.Separator + VideoText.BlockReason(reason)); if (stays.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", stays.Count - DeletePlans.ListedLines)); return lines; } /// The render's name in the section, else , else its id. private string Named(Guid id, string? fallback) => Names?.GetValueOrDefault(id) ?? fallback ?? id.ToString("D"); /// The confirmation of moving the planned renders into the trash; null when nothing would go. public ConfirmRequest? Request() { if (Count == 0) return null; var message = Loc.L("Videos.Delete.Summary", Formatting.Count(Count), Formatting.Bytes(Bytes)) + Environment.NewLine + Loc.L("Videos.Delete.CopiesStay"); if (Missing > 0) message += Environment.NewLine + Loc.L("Videos.Delete.Missing"); return new ConfirmRequest { Heading = Loc.L("Videos.Delete.Title"), Message = message, Detail = string.Join(Environment.NewLine, Lines()), IsDetailMonospaced = false, DetailToolTip = Loc.L("Tooltips.Videos.Delete"), SafeText = Loc.L("Archive.Delete.Keep"), SafeToolTip = Loc.L("Tooltips.Archive.DeleteKeep"), ActionText = Loc.L("Videos.Delete.Confirm"), ActionToolTip = Loc.L("Tooltips.Videos.DeleteConfirm"), IsDestructive = true, }; } }