using OpenNest.BestFit;
using OpenNest.Data;
namespace OpenNest.Desktop;
/// What the desktop app's start-up steps found.
/// Why the legacy nest template could not be converted, to show
/// the user; null when there was nothing to report.
/// The template was converted or is gone: clear its setting
/// () so it is never consulted again.
/// Open a new nest, as Tools > Options > Create a new nest on
/// startup asks; the window adds none beside nests already open.
public sealed record DesktopStartupResult(
string TemplateWarning, bool ClearTemplateSetting, bool CreateNewNest);
///
/// The desktop app's start-up steps, in the WinForms main window's order: first the one-time
/// conversion of the legacy nest template into the nest defaults file (a new nest reads that
/// file), then whether to open a new nest.
///
public static class DesktopStartup
{
///
/// Converts the legacy nest template and decides on the new nest. Reads the user settings
/// but never writes them, so it can run off the UI thread while the window's own settings
/// writers run on it; the caller clears the setting on the UI thread when asked to.
///
/// The shared user settings file.
/// The nest defaults file the template converts into.
/// Best-fit cache the template's drawings read from.
public static DesktopStartupResult Run(
string settingsPath, string defaultsPath, IBestFitStore bestFitStore)
{
var settings = UserSettingsFile.Read(settingsPath);
settings.TryGetValue(NestTemplateMigration.SettingName, out var templatePath);
var migration = NestTemplateMigration.Run(templatePath, defaultsPath, bestFitStore);
return new DesktopStartupResult(migration.Warning, migration.ClearSetting,
DesktopOptions.Read(settings).CreateNewNestOnOpen);
}
///
/// Clears the template setting to the empty text WinForms stores, keeping every other
/// setting. Returns false when the file could not be written; the next start then finds
/// the defaults file already there and clears it again.
///
public static bool ClearTemplateSetting(string settingsPath) =>
UserSettingsFile.Update(settingsPath, s => s[NestTemplateMigration.SettingName] = "");
}