using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenNest.Data;
using OpenNest.Desktop;
namespace OpenNest.Avalonia
{
///
/// The machines, nest defaults and post processors this app works with, from the shared
/// desktop library: in Database mode (storage.json, shared with the WinForms app) the nest
/// server's copy once it holds one, otherwise this PC's files, with the user settings both
/// apps keep in user-settings.json.
///
internal static class DesktopSettings
{
/// The storage mode and nest server from storage.json; Tools > Options can change the server.
private static NestStorageSettings storage = NestStorageSettings.Load(NestStorageSettings.DefaultPath);
public static SharedSettingsSource Shared { get; } = new(() => storage);
public static MachineSettingsSource Machines { get; } =
new(Shared, new MachineUserSettingsFile(UserSettingsFile.DefaultPath));
private static RemoteNestRepository repository;
private static string repositoryUrl;
///
/// File > New nests, numbered by the nest server in Database mode and otherwise by the
/// counter both apps keep in user-settings.json.
///
public static NewNestSource NewNests { get; } = new(
Machines,
new NewNestUserSettingsFile(UserSettingsFile.DefaultPath),
() => storage.IsDatabaseMode ? Repository(storage.ServerUrl) : null);
/// The nest server URL saved in storage.json (blank when none).
public static string ServerUrl => storage.ServerUrl;
///
/// Saves as the nest server in storage.json, keeping the
/// storage mode, as the WinForms Options form does; the next request uses it.
///
public static void SaveServerUrl(string serverUrl) =>
storage = NestServerUrlOption.SaveToFile(NestStorageSettings.DefaultPath, serverUrl);
// One client per server, recreated when the URL changes.
private static RemoteNestRepository Repository(string url)
{
if (repository == null || repositoryUrl != url)
{
repository?.Dispose();
repository = new RemoteNestRepository(url);
repositoryUrl = url;
}
return repository;
}
/// The post processors loaded from Posts/ beside the app, by load order.
public static IReadOnlyList PostProcessors { get; private set; } = [];
/// The readable machine configurations, once has run.
public static IReadOnlyList MachineConfigs { get; private set; } = [];
///
/// Loads the post processors in (each DLL on its own, so one
/// that fails does not stop the others) and registers their materials. Returns the
/// failures to show, or null when every post loaded.
///
public static string LoadPosts(string directory)
{
var result = PostProcessorLoader.Load(directory);
PostProcessors = result.PostProcessors;
return result.Failures.Count == 0
? null
: "These post processors could not be loaded: " + string.Join("; ", result.Failures);
}
///
/// Reads the machines off the UI thread (the nest server can take seconds to answer), then
/// copies the legacy clamps onto the default or only machine, as the WinForms app does at
/// startup (with several machines and no default it waits for the WinForms app to ask).
/// Returns what to tell the user: the machines' source when the server could not be
/// reached, or why the clamps were not copied; null when there is nothing to say.
///
public static Task LoadMachinesAsync() => Task.Run(() =>
{
MachineConfigs = Machines.LoadMachines();
var clamps = Machines.MigrateLegacyClamps(askMachine: null);
var offline = Shared.TakeOfflineWarning();
var notices = new[] { offline, clamps }
.Where(text => !string.IsNullOrEmpty(text))
.Select(OneLine)
.ToList();
return notices.Count == 0 ? null : string.Join(" ", notices);
});
private static string OneLine(string text) =>
string.Join(" ", text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}
}