using System.Globalization;
using Febe.Core.Storage;
namespace Febe.Core.Export;
///
/// Settings of one export job (like ImportRunSettings): a row of the setting table each, so the schema
/// stays as E1 left it. Cleared when the job completes, is cancelled or deleted.
///
internal static class ExportRunSettings
{
public static string CopyAgainKey(long runId) => string.Create(CultureInfo.InvariantCulture, $"esportazione.lavoro.{runId}.copia-di-nuovo");
///
/// How the frames of a frames job were asked (tappa E3): Istante, Ogni or Numero:N; Fermo
/// for «Salva il fotogramma» of a still video (tappa E4).
///
public static string FramesKey(long runId) => string.Create(CultureInfo.InvariantCulture, $"esportazione.lavoro.{runId}.fotogrammi");
/// «Copia di nuovo» of one copy row: only its exact identity is looked up (decision D11 of tappa E3).
public static string ExactKey(long runId) => string.Create(CultureInfo.InvariantCulture, $"esportazione.lavoro.{runId}.esatta");
/// The frames request of a job, or null for a job of whole or converted files.
public static async Task<(FrameMode Mode, int Count)?> GetFramesAsync(ISettingsStore store, long runId, CancellationToken ct)
{
var text = (await store.GetAsync(FramesKey(runId), ct).ConfigureAwait(false))?.Trim();
if (text is null)
{
return null;
}
if (string.Equals(text, "Istante", StringComparison.Ordinal))
{
return (FrameMode.At, 1);
}
if (string.Equals(text, "Ogni", StringComparison.Ordinal))
{
return (FrameMode.Every, 0);
}
if (string.Equals(text, "Fermo", StringComparison.Ordinal))
{
return (FrameMode.Still, 1);
}
return text.StartsWith("Numero:", StringComparison.Ordinal)
&& int.TryParse(text.AsSpan("Numero:".Length), NumberStyles.None, CultureInfo.InvariantCulture, out var count) && count > 0
? (FrameMode.Count, count)
: null;
}
public static Task SetFramesAsync(ISettingsStore store, long runId, FrameMode mode, int count, CancellationToken ct) =>
store.SetAsync(
FramesKey(runId),
mode switch
{
FrameMode.At => "Istante",
FrameMode.Every => "Ogni",
FrameMode.Still => "Fermo",
_ => string.Create(CultureInfo.InvariantCulture, $"Numero:{count}"),
},
ct);
public static async Task GetExactAsync(ISettingsStore store, long runId, CancellationToken ct) =>
string.Equals((await store.GetAsync(ExactKey(runId), ct).ConfigureAwait(false))?.Trim(), "1", StringComparison.Ordinal);
public static Task SetExactAsync(ISettingsStore store, long runId, CancellationToken ct) => store.SetAsync(ExactKey(runId), "1", ct);
/// «Copia di nuovo anche i tolti o superati» was chosen for this job (decision D9).
public static async Task GetCopyAgainAsync(ISettingsStore store, long runId, CancellationToken ct)
{
var text = await store.GetAsync(CopyAgainKey(runId), ct).ConfigureAwait(false);
return string.Equals(text?.Trim(), "1", StringComparison.Ordinal);
}
public static Task SetCopyAgainAsync(ISettingsStore store, long runId, bool value, CancellationToken ct) =>
store.SetAsync(CopyAgainKey(runId), value ? "1" : null, ct);
/// Every key of the job, in one transaction (a run id can be used again after DeleteRunAsync).
public static Task ClearAsync(ISettingsStore store, long runId, CancellationToken ct) =>
store.SetManyAsync(
new Dictionary
{
[CopyAgainKey(runId)] = null,
[FramesKey(runId)] = null,
[ExactKey(runId)] = null,
},
ct);
}