using Titano.App.Library; using Titano.App.Settings; using Titano.Core.Archiving; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Naming; using Titano.Data.Repositories; namespace Titano.App.Archiving; /// What a mass rename changes: the sequence titles (Rename) or the folder layout (Move). public enum RenameMode { Titles, Folders, } /// One line of the rename dry run: before → after. public sealed record RenameRow(Guid SequenceId, string From, string To); /// /// Mass rename and move of sequences (BRIEF §7, §12.5): the template is checked live, the engine's dry run /// () is listed line by line, and applying makes exactly those changes. /// public static class MassRename { /// The first problem of a template, localized; null when it can be used. public static string? Validate(RenameMode mode, string? template) { if (string.IsNullOrWhiteSpace(template)) return Loc.L("Errors.Template.Empty", string.Empty, 1); if (mode == RenameMode.Folders) return NamingPreview.Validate(template, isFolder: true); var parsed = PathTemplate.Parse(template); return parsed.Errors is [var error, ..] ? Loc.L("Errors.Template." + error.Code, error.Token ?? string.Empty, error.Position + 1) : null; } /// The changes of a plan as rows: titles or folders, whichever the mode changes. public static IReadOnlyList Rows(RenamePlan plan, RenameMode mode) => [ .. plan.Steps.Select(s => mode == RenameMode.Folders ? new RenameRow(s.SequenceId, s.FromFolder, s.ToFolder) : new RenameRow(s.SequenceId, s.FromTitle ?? string.Empty, s.ToTitle ?? string.Empty)), ]; /// "12 of 36 sequences change". public static string Summary(RenamePlan plan, int setSize) => Loc.Plural("Archive.Rename.Summary", plan.Steps.Count, setSize); } /// Adding and removing tags on a set of sequences (BRIEF §12.5 Retag). public static class Retag { /// "sunset, harbour; Holy grail" → normalized, distinct tags; entries that cannot be tags are left out. public static IReadOnlyList Parse(string? text) { var seen = new HashSet(StringComparer.OrdinalIgnoreCase); return [.. Split(text).Where(IsValid).Select(TagRepository.Normalize).Where(seen.Add)]; } /// The problem of a tag list as one sentence; null when every entry can be a tag. public static string? Validate(string? text) => Split(text).All(IsValid) ? null : Loc.L("Archive.Retag.Invalid", TagRepository.MaxLength); private static IEnumerable Split(string? text) => string.IsNullOrWhiteSpace(text) ? [] : text.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); private static bool IsValid(string tag) => tag.Length is > 0 and <= TagRepository.MaxLength && !tag.Any(char.IsControl); /// The new tag list of one sequence: removals first, then additions not already present. public static IReadOnlyList Apply(IReadOnlyList current, IReadOnlyList add, IReadOnlyList remove) { var removed = new HashSet(remove, StringComparer.OrdinalIgnoreCase); var result = current.Where(t => !removed.Contains(t)).ToList(); var present = new HashSet(result, StringComparer.OrdinalIgnoreCase); result.AddRange(add.Where(present.Add)); return result; } /// Only the sequences whose tags change, with their new tags (the dry run). public static IReadOnlyList<(Sequence Sequence, IReadOnlyList Tags)> Changes(IEnumerable sequences, IReadOnlyList add, IReadOnlyList remove) => [ .. sequences.Select(s => (Sequence: s, Tags: Apply(s.Tags, add, remove))) .Where(c => !c.Tags.SequenceEqual(c.Sequence.Tags, StringComparer.Ordinal)), ]; } /// /// Narrows the mass-operation set on top of the tree selection (Archive.html "Filtered set ▾"): camera, status and tag; /// years and single sequences are chosen in the tree. /// public sealed record ArchiveSetFilter(string? Camera = null, StatusFilter Status = StatusFilter.Any, string? Tag = null) { public bool IsEmpty => Camera is null && Status == StatusFilter.Any && Tag is null; public bool Matches(Sequence sequence) { if (Camera is not null && CameraOf(sequence) != Camera) return false; if (Tag is not null && !sequence.Tags.Contains(Tag, StringComparer.OrdinalIgnoreCase)) return false; var (with, without) = LibraryFilter.Flags(Status); return (sequence.Status & with) == with && (sequence.Status & without) == 0; } /// "Canon EOS R5 · Not analysed · sunset"; empty without a filter. public string Describe() => string.Join(" · ", new[] { Camera ?? string.Empty, Status == StatusFilter.Any ? string.Empty : StatusLabel(Status), Tag ?? string.Empty, }.Where(t => t.Length > 0)); /// The camera a sequence is filed under, as the camera chart names it. public static string CameraOf(Sequence sequence) => PathTemplate.CameraName(sequence.CameraMake, sequence.CameraModel) is { Length: > 0 } name ? name : Loc.L("Archive.Chart.UnknownCamera"); public static string StatusLabel(StatusFilter status) => Loc.L(status == StatusFilter.Any ? "Library.Filter.AnyStatus" : "Library.Filter.Status." + status); }