using System.Globalization; using Chronos.App.Infrastructure; using Chronos.Core.Conversion; using Chronos.Core.Localization; namespace Chronos.App.Archiving; /// /// One sequence of the conversion dry run (owner review 2, R7): its name, what converts (photos, size before and after), /// and the photos that stay as they are or why the whole sequence is left as it is, each line with why. /// /// The sequence has no title: is its folder, read left to right. /// Nothing of the sequence converts now (busy, offline, unreadable). public sealed record ConvertRow(Guid SequenceId, string Name, bool IsNamePath, string Note, IReadOnlyList Kept, bool IsBlocked) { public bool HasNote => Note.Length > 0; public bool HasKept => Kept.Count > 0; /// What a screen reader says after the name: what converts, then every line of what stays. public string Description => string.Join(Environment.NewLine, [.. HasNote ? [Note] : (string[])[], .. Kept]); } /// The free space line of a conversion dry run, and whether the volume lacks the space. public sealed record ConvertSpaceLine(string Text, bool IsShort); /// /// The words of a mass conversion (owner review 2, R7; BRIEF §12.5): the engine's dry run () as /// rows, a summary and the free space line, the typed confirmation a Replace asks for, and the one New sequence asks for, /// which says which photos the new sequences lack. /// public static class ConvertPlans { /// The targets in the order the dialog offers them. public static readonly IReadOnlyList Targets = [ConvertTarget.Jpeg, ConvertTarget.Tiff16, ConvertTarget.LinearDng]; /// The problem of a JPEG quality as typed, localized; null when it is 1–100. public static string? ValidateQuality(string? text) => int.TryParse(text?.Trim(), NumberStyles.Integer, Loc.Culture, out var quality) && quality is >= 1 and <= 100 ? null : Loc.L("Errors.Number.Range", 1.ToString("N0", Loc.Culture), 100.ToString("N0", Loc.Culture)); /// A valid quality as typed (see ). public static int ParseQuality(string text) => int.Parse(text.Trim(), NumberStyles.Integer, Loc.Culture); /// /// The rows of a plan in its order: every sequence that converts, or whose photos stay as they are, with why in the /// words of the plan's mode (). /// public static IReadOnlyList Rows(ConvertPlan plan) { ArgumentNullException.ThrowIfNull(plan); return [.. plan.Sequences.Where(s => s.Frames.Count > 0 || s.Skipped.Count > 0 || s.Blocked.Count > 0).Select(s => Row(s, plan.Mode))]; } /// /// One sequence of a plan: what converts, then what blocks it, then the photos skipped with why. Of a sequence /// copies, those are not copied into the new sequence, which holds the converted /// copies alone; otherwise (a Replace, or a sequence that converts nothing now, of which no new sequence is made) they /// stay as they are. /// public static ConvertRow Row(ConvertSequencePlan step, ConvertMode mode = ConvertMode.Replace) { ArgumentNullException.ThrowIfNull(step); var (name, named) = NameOf(step); var note = step.Converts ? Loc.Plural("Shell.Status.Photos", step.Frames.Count) + Loc.Separator + Sizes(step.BytesBefore, step.BytesAfter) : string.Empty; // What blocks the whole sequence comes first: the photos it would skip anyway do not matter until it can convert. List kept = [.. step.Blocked.Distinct().Select(b => Loc.L("Archive.Convert.Blocked." + b))]; var why = mode == ConvertMode.NewSequence && step.Converts ? "Archive.Convert.NotCopied." : "Archive.Convert.Skipped."; kept.AddRange(step.Skipped.Select(s => Loc.L(why + s.Reason) + Loc.Separator + Loc.Plural("Shell.Status.Photos", s.Frames))); return new ConvertRow(step.SequenceId, name, !named, note, kept, step.Blocked.Count > 0); } /// A sequence's title, else its folder (a path), else the start of its id; and whether it has a title. private static (string Name, bool Named) NameOf(ConvertSequencePlan step) { var named = !string.IsNullOrWhiteSpace(step.Title); return (named ? step.Title : step.Folder.Length > 0 ? step.Folder : step.SequenceId.ToString("N")[..8], named); } /// The photos a conversion into new sequences leaves out of them: those the sequences it copies skip. public static int LeftOut(ConvertPlan plan) { ArgumentNullException.ThrowIfNull(plan); return plan.Converted.Sum(s => s.SkippedFrames); } /// "12.4 GB → 9.8 GB". public static string Sizes(long before, long after) => Loc.L("Archive.Convert.Sizes", Formatting.Bytes(before), Formatting.Bytes(after)); /// /// "12 of 36 change · 3,600 photos · 41 GB → 32 GB · 20 skipped": the sequences converted of those planned, then the /// photos and their sizes when something converts, the photos skipped when there are any. With /// , those of the sequences copied first ("20 not copied": the new sequences lack /// them), then those of the sequences that convert nothing now ("4 skipped"). /// public static string Summary(ConvertPlan plan) { ArgumentNullException.ThrowIfNull(plan); var parts = new List { Loc.Plural("Archive.Rename.Summary", plan.Converted.Count(), plan.Sequences.Count) }; if (plan.Frames > 0) { parts.Add(Loc.Plural("Shell.Status.Photos", plan.Frames)); parts.Add(Sizes(plan.BytesBefore, plan.BytesAfter)); } var leftOut = plan.Mode == ConvertMode.NewSequence ? LeftOut(plan) : 0; if (leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.NotCopiedPhotos", leftOut)); if (plan.Skipped - leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.SkippedPhotos", plan.Skipped - leftOut)); return string.Join(Loc.Separator, parts); } /// "Needs 12 GB, free 340 GB", or that the volume lacks it, or that its free space cannot be measured. public static ConvertSpaceLine Space(ConvertSpace space) { ArgumentNullException.ThrowIfNull(space); var required = Formatting.Bytes(space.Required); if (space.Free is not { } free) return new(Loc.L("Archive.Convert.SpaceUnknown", required), false); return space.Enough ? new(Loc.L("Archive.Convert.Space", required, Formatting.Bytes(free)), false) : new(Loc.L("Archive.Convert.SpaceShort", required, Formatting.Bytes(free)), true); } /// "Linear DNG": the target as the dialog names it. public static string TargetText(ConvertTarget target) => Loc.L("Archive.Convert.Target." + target); /// /// The destructive confirmation of a Replace: the sequences whose photos are replaced, that the originals move to the /// archive's trash, and the number of photos to type; null when the plan converts nothing. /// public static ConfirmRequest? ReplaceRequest(ConvertPlan plan) { ArgumentNullException.ThrowIfNull(plan); var converted = plan.Converted.ToList(); if (converted.Count == 0) return null; var count = plan.Frames.ToString(CultureInfo.InvariantCulture); var lines = converted.Take(DeletePlans.ListedLines).Select(s => Loc.L("Archive.Delete.Line", Row(s).Name, Loc.Plural("Shell.Status.Photos", s.Frames.Count), Formatting.Bytes(s.BytesBefore))).ToList(); if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines)); return new ConfirmRequest { Heading = Loc.Plural("Archive.Convert.ReplaceHeading", converted.Count), Message = Loc.L("Archive.Convert.ReplaceMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore), TargetText(plan.Options.Target)), Detail = string.Join(Environment.NewLine, lines), IsDetailMonospaced = false, DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlan"), SafeText = Loc.L("Archive.Delete.Keep"), SafeToolTip = Loc.L("Tooltips.Archive.DeleteKeep"), ActionText = Loc.L("Archive.Convert.Replace"), ActionToolTip = Loc.Plural("Tooltips.Archive.ConvertReplace", plan.Frames), IsDestructive = true, TypedConfirmation = count, TypedPrompt = Loc.L("Archive.Delete.Type", count), }; } /// /// The confirmation of a conversion into new sequences: the photos converted into copies beside their sources, the /// originals untouched, how many photos the new sequences lack (those the plan skips there, for the reasons its rows /// give), and each sequence copied with its photos, their size and the photos its new sequence lacks; null when the plan /// converts nothing. /// public static ConfirmRequest? NewRequest(ConvertPlan plan) { ArgumentNullException.ThrowIfNull(plan); var converted = plan.Converted.ToList(); if (converted.Count == 0) return null; var lines = converted.Take(DeletePlans.ListedLines).Select(s => { var line = Loc.L("Archive.Delete.Line", NameOf(s).Name, Loc.Plural("Shell.Status.Photos", s.Frames.Count), Sizes(s.BytesBefore, s.BytesAfter)); return s.SkippedFrames > 0 ? line + Loc.Separator + Loc.Plural("Archive.Convert.NotCopiedPhotos", s.SkippedFrames) : line; }).ToList(); if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines)); var message = Loc.L("Archive.Convert.NewMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore), TargetText(plan.Options.Target)); if (LeftOut(plan) is > 0 and var leftOut) message += " " + Loc.Plural("Archive.Convert.NewLeftOut", leftOut); return new ConfirmRequest { Heading = Loc.Plural("Archive.Convert.NewHeading", converted.Count), Message = message, Detail = string.Join(Environment.NewLine, lines), IsDetailMonospaced = false, DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlanNew"), SafeText = Loc.L("Archive.Dialog.Cancel"), SafeToolTip = Loc.L("Tooltips.Archive.ConvertBack"), ActionText = Loc.L("Archive.Convert.Confirm"), ActionToolTip = Loc.L("Tooltips.Archive.ConvertNew"), }; } }