using System.IO;
using Titano.Core.Archiving;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Imaging;
using Titano.Core.Import;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Repositories;
namespace Titano.App.Import;
///
/// Capture and smoke-test data ONLY (--capture, never the user's data): a simulated Canon EOS R5 card in the
/// capture folder — a night, a dawn and a sunset session of small JPEGs with Exif — remembered as "EOS R5 · card A",
/// a Sony card seen for the first time, a second remembered card that is not inserted, a USB drive without DCIM, and
/// the import result shown afterwards.
///
public static class SampleCards
{
private const uint Serial = 0x3F2A9C11;
private static readonly DateTimeOffset LastImport = new(2026, 9, 21, 19, 40, 0, TimeSpan.Zero);
/// Night, dawn and sunset: start, photos, seconds between photos.
private static readonly (DateTime Start, int Photos, int Interval)[] Sessions =
[
(new DateTime(2026, 9, 26, 21, 10, 0), 10, 120),
(new DateTime(2026, 9, 27, 5, 41, 0), 16, 4),
(new DateTime(2026, 9, 28, 18, 2, 11), 24, 2),
];
/// The new card's one session: a morning harbour series.
private static readonly (DateTime Start, int Photos, int Interval)[] NewSessions = [(new DateTime(2026, 9, 30, 7, 15, 0), 12, 5)];
/// The inserted card: its root is a folder of the capture environment.
public static CardDevice Card(string dataDirectory) =>
new(Path.Combine(dataDirectory, "card") + Path.DirectorySeparatorChar, "EOS_DIGITAL", Serial, 127_865_454_592, 31_665_454_592, IsRemovable: true,
HasDcim: true);
/// A Sony card Titano has never seen (the nickname field and the "new card" toast).
public static CardDevice NewCard(string dataDirectory) =>
new(Path.Combine(dataDirectory, "newcard") + Path.DirectorySeparatorChar, "SONY", 0x2B7E1A09, 63_864_569_856, 60_120_000_000, IsRemovable: true,
HasDcim: true);
/// Writes the cards' photos, remembers two cards (the inserted one with its last import) and inserts both cards and a USB drive.
public static async Task InsertAsync(TitanoDatabase db, SampleDeviceMonitor devices, string dataDirectory)
{
var card = Card(dataDirectory);
WritePhotos(Path.Combine(card.Root, "DCIM", "100CANON"), "IMG_", Sessions, "Canon", "Canon EOS R5", "0431");
var newCard = NewCard(dataDirectory);
WritePhotos(Path.Combine(newCard.Root, "DCIM", "100MSDCF"), "DSC0", NewSessions, "SONY", "ILCE-7M4", "4410287");
var known = new MediaCard
{
Id = Guid.CreateVersion7(),
Identity = card.Identity,
VolumeSerial = card.SerialNumber,
Label = card.Label,
Capacity = card.TotalBytes,
Nickname = "EOS R5 · card A",
CameraMake = "Canon",
CameraModel = "Canon EOS R5",
CameraSerial = "0431",
FirstSeen = LastImport.AddMonths(-7),
LastSeen = DateTimeOffset.UtcNow,
};
await db.MediaCards.UpsertAsync(known);
await db.MediaCards.RecordImportsAsync([new ImportRecord(known.Id, new ImportKey("DCIM/100CANON/IMG_9001.CR2", 24_000_000, LastImport), null, null, null,
LastImport)]);
await db.MediaCards.UpsertAsync(new MediaCard
{
Id = Guid.CreateVersion7(),
Identity = "7C1D0E42-GOPRO-63864569856",
VolumeSerial = 0x7C1D0E42,
Label = "GOPRO",
Capacity = 63_864_569_856,
Nickname = "GoPro 12",
CameraMake = "GoPro",
CameraModel = "HERO12 Black",
FirstSeen = LastImport.AddMonths(-3),
LastSeen = LastImport.AddDays(-6),
});
devices.Insert(card);
devices.Insert(newCard);
devices.Insert(new CardDevice(@"F:\", "USB DRIVE", 0x51E02B44, 2_000_398_934_016, 1_800_000_000_000, IsRemovable: true, HasDcim: false));
}
/// What the import of reports: every planned copy committed and read back.
public static ImportOutcome Outcome(ImportPlan plan) => new(plan.CopyCount, 0, plan.FileCount - plan.CopyCount, [], Verified: true);
/// The dry run of "Delete imported" after that import: every copy matches except the last photo, touched since.
public static ErasePlan Erase(ImportPlan plan)
{
var copied = plan.Sessions.SelectMany(s => s.Items.Where(i => i.Action == ImportAction.Copy).Select(i => (Session: s, Item: i))).ToList();
return new ErasePlan(plan.SourceRoot,
[
.. copied.Select((c, n) => new EraseItem(
new EraseCandidate(c.Item.SourcePath, c.Item.Size, string.Empty,
Path.Combine(plan.DestinationRoot, c.Session.Folder, ArchiveLayout.OriginalsFolder, c.Item.FileName)),
n == copied.Count - 1 ? EraseVerdict.SourceChanged : EraseVerdict.Delete, c.Item.ModifiedUtc)),
]);
}
private static void WritePhotos(string folder, string prefix, (DateTime Start, int Photos, int Interval)[] sessions, string make, string model, string body)
{
if (Directory.Exists(folder)) return;
Directory.CreateDirectory(folder);
var n = 1;
foreach (var (start, photos, interval) in sessions)
{
for (var i = 0; i < photos; i++, n++)
{
var captured = start.AddSeconds(i * interval);
var path = Path.Combine(folder, $"{prefix}{n:0000}.JPG");
File.WriteAllBytes(path, Photo(captured, n, make, model, body));
File.SetLastWriteTime(path, captured);
}
}
}
/// A small sky gradient with grain (distinct bytes per photo) and the camera's Exif.
private static byte[] Photo(DateTime captured, int seed, string make, string model, string body)
{
const int Width = 96, Height = 64;
using var image = new ImageBuffer(Width, Height, 3, clear: false);
var random = new Random(seed);
var dusk = captured.Hour is >= 18 or < 6 ? 0.15f : 0.6f;
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
var sky = dusk * (1 - y / (float)Height) + 0.05f;
var grain = (float)random.NextDouble() * 0.04f;
image.Plane(0)[y * Width + x] = sky * 1.2f + grain;
image.Plane(1)[y * Width + x] = sky * 0.8f + grain;
image.Plane(2)[y * Width + x] = sky * 0.6f + grain;
}
}
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata
{
Make = make,
Model = model,
BodySerial = body,
CapturedAt = captured,
Iso = 100,
ExposureTime = 1.0 / 250,
FNumber = 8,
});
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
}
}
/// The capture's device monitor: lists the sample volumes; it raises no events and never ejects or formats.
public sealed class SampleDeviceMonitor : IDeviceMonitor
{
private readonly List _devices = [];
public event Action? Arrived
{
add { }
remove { }
}
public event Action? Removed
{
add { }
remove { }
}
public IReadOnlyList Devices => [.. _devices];
public void Insert(CardDevice device) => _devices.Add(device);
public void Start()
{
}
public EjectOutcome Eject(CardDevice device) => EjectOutcome.Failed;
public FormatOutcome Format(CardDevice device) => FormatOutcome.Cancelled;
public void Dispose()
{
}
}