using Chronos.App.Library;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Data.Repositories;
namespace Chronos.App.Archiving;
/// 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;
return LibraryFilter.MatchesStatus(sequence, Status);
}
/// "Canon EOS R5 · Not analysed · sunset"; empty without a filter.
public string Describe() => string.Join(Loc.Separator, 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);
}