using Titano.App.Sections;
using Titano.App.Shell;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Data;
namespace Titano.App.Import;
///
/// The Import states that --capture renders (import-<state>-<theme>.png) and the screen smoke
/// tests check, on the cards of : the card just inserted (with its toast) and its plan and
/// sessions, "Show plan", the import copying, an import that stopped, the verification result with the after-import
/// actions, and a card seen for the first time (its nickname field and toast). The caller dismisses toasts before each
/// state, and after it unless .
///
public static class ImportScenes
{
public static readonly IReadOnlyList States = ["inserted", "plan", "showplan", "running", "failed", "done", "newcard"];
/// States that show a toast on purpose (the insertion toasts); the caller dismisses the others' toasts.
public static bool ShowsToast(string state) => state is "inserted" or "newcard";
public static async Task ShowAsync(ShellViewModel shell, string state)
{
var import = shell.Import;
var data = shell.Services.Environment.DataDirectory;
shell.Navigate(SectionKey.Import);
switch (state)
{
case "inserted":
await ShowPlanAsync(import, SampleCards.Card(data));
await import.CardInsertedAsync(SampleCards.Card(data));
break;
case "plan":
await ShowPlanAsync(import, SampleCards.Card(data));
break;
case "showplan":
import.ShowPlanCommand.Execute(null);
break;
case "running":
import.CloseDetailsCommand.Execute(null);
var plan = Plan(import);
import.ShowRunning(new ImportProgress(31, plan.FileCount, plan.CopyBytes * 31 / plan.FileCount, plan.CopyBytes, 31, 0, 0, null));
break;
case "failed":
import.ShowFailed(Loc.L("Archive.Error.SourceGone", SampleCards.Card(data).Root));
break;
case "done":
await ShowPlanAsync(import, SampleCards.Card(data));
import.DeleteImported = true;
var done = Plan(import);
var card = SampleCards.Card(data);
import.ShowResult(SampleCards.Outcome(done)).ShowErase(SampleCards.Erase(done), CardSnapshot.Take(card.Root, card.Identity));
break;
case "newcard":
// Inserted first (its toast says it is new), then chosen: its first plan registers it and asks for a nickname.
var fresh = SampleCards.NewCard(data);
await ForgetAsync(shell, fresh.Identity);
await import.CardInsertedAsync(fresh);
await ShowPlanAsync(import, fresh);
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, "Unknown Import state.");
}
}
/// The card selected, its plan ready, the sunset session titled.
private static async Task ShowPlanAsync(ImportViewModel import, CardDevice card)
{
if (import.Result is not null) import.NewImportCommand.Execute(null);
import.CloseDetailsCommand.Execute(null);
await import.RefreshSourcesAsync();
import.SelectSource(card.Identity);
import.KeepCard = true;
await import.WaitForPlanAsync();
if (import.Sessions.Count == 3)
{
var sunset = import.Sessions[^1].Title;
sunset.Text = "Harbour sunset";
sunset.Commit();
}
}
private static ImportPlan Plan(ImportViewModel import) => import.Plan ?? throw new InvalidOperationException("The sample card has no plan.");
/// The new card stays new in every theme: an earlier state registered it on its first plan.
private static async Task ForgetAsync(ShellViewModel shell, string identity)
{
if (shell.Services.Database is not { } db || await db.MediaCards.FindByIdentityAsync(identity) is not { } known) return;
await db.MediaCards.DeleteAsync(known.Id);
}
}