using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Naming;
namespace Titano.App.Import;
public enum SourceKind
{
/// A card with a DCIM tree, or a remembered card.
Card,
/// A removable drive without DCIM: imported as a plain folder tree.
Drive,
/// A folder picked with "Folder…".
Folder,
}
///
/// One entry of the Devices list (BRIEF §12.4, §13 card pool): an inserted volume joined with its remembered card, a
/// remembered card that is not inserted, or a picked folder. is the card identity or the folder path.
///
public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Device, MediaCard? Card, string? FolderPath)
{
public bool IsInserted => Device is not null || FolderPath is not null;
public bool IsKnown => Card is not null;
/// Where the photos are read from; null for a remembered card that is not inserted.
public string? Root => Device?.Root ?? FolderPath;
/// "EOS R5 · card A", the volume label, or the drive.
public string Title => Card?.Nickname is { Length: > 0 } nickname ? nickname
: Device is { Label.Length: > 0 } labelled ? labelled.Label
: Device?.Root ?? FolderPath ?? Card?.Label ?? string.Empty;
/// "128 GB · 96.2 GB used", "no DCIM · 1.8 TB free", "Canon EOS R5 · not inserted".
public string Subtitle => Kind switch
{
SourceKind.Folder => FolderPath ?? string.Empty,
SourceKind.Drive => Loc.L("Import.Device.NoDcim", Formatting.Bytes(Device!.FreeBytes)),
_ when Device is not null => Loc.L("Import.Device.Card", Formatting.Bytes(Device.TotalBytes), Formatting.Bytes(Device.UsedBytes)),
_ => Loc.L("Import.Device.Away", CameraText(Card) is { Length: > 0 } camera ? camera : Formatting.Bytes(Card!.Capacity)),
};
/// "Known card", "New card", … for the state dot's tooltip.
public string StateText => Loc.L(Kind switch
{
SourceKind.Folder => "Import.Device.State.Folder",
SourceKind.Drive => "Import.Device.State.Drive",
_ when !IsInserted => "Import.Device.State.Away",
_ when IsKnown => "Import.Device.State.Known",
_ => "Import.Device.State.New",
});
/// "128 GB · serial 3F2A-9C11 · Canon EOS R5 #0431": the identity line of the profile header.
public string IdentityText
{
get
{
var parts = new List();
var capacity = Device?.TotalBytes ?? Card?.Capacity ?? 0;
if (capacity > 0) parts.Add(Formatting.Bytes(capacity));
var serial = Device?.SerialNumber ?? Card?.VolumeSerial;
if (serial is { } s && Kind != SourceKind.Folder) parts.Add(Loc.L("Import.Device.Serial", SerialText(s)));
if (CameraText(Card) is { Length: > 0 } camera) parts.Add(Card?.CameraSerial is { Length: > 0 } body ? Loc.L("Import.Device.Body", camera, body) : camera);
return string.Join(" · ", parts);
}
}
/// "3F2A-9C11", as Windows shows a volume serial.
public static string SerialText(uint serial)
{
var hex = serial.ToString("X8", CultureInfo.InvariantCulture);
return hex[..4] + "-" + hex[4..];
}
private static string CameraText(MediaCard? card) => card is null ? string.Empty : PathTemplate.CameraName(card.CameraMake, card.CameraModel);
}
/// Builds the Devices list: inserted volumes first (in drive order), then remembered cards, then a picked folder.
public static class CardPool
{
public static IReadOnlyList Build(IEnumerable devices, IEnumerable cards, string? folder)
{
var known = cards.ToDictionary(c => c.Identity, StringComparer.Ordinal);
var list = new List();
foreach (var device in devices.OrderBy(d => d.Root, StringComparer.OrdinalIgnoreCase))
{
known.Remove(device.Identity, out var card);
var kind = device.HasDcim || card is not null ? SourceKind.Card : SourceKind.Drive;
list.Add(new ImportSource(device.Identity, kind, device, card, null));
}
foreach (var card in known.Values.OrderByDescending(c => c.LastSeen))
list.Add(new ImportSource(card.Identity, SourceKind.Card, null, card, null));
if (folder is not null) list.Add(new ImportSource(folder, SourceKind.Folder, null, null, folder));
return list;
}
}