using System.Collections.Generic;
using System.Globalization;
namespace OpenNest.Desktop;
///
/// Setting values in the text form the WinForms app stores in user-settings.json: what
/// System.Configuration's invariant type converters write and read ("True"/"False", a decimal
/// in "G" form, a double in round-trip "R" form). A value they cannot read stands for the
/// setting's default, as it does in WinForms.
///
public static class UserSettingText
{
/// The stored decimal under , or .
public static decimal ReadDecimal(IReadOnlyDictionary settings, string name, decimal fallback) =>
settings.TryGetValue(name, out var text)
&& decimal.TryParse(text?.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var value)
? value
: fallback;
/// The stored double under , or .
public static double ReadDouble(IReadOnlyDictionary settings, string name, double fallback) =>
settings.TryGetValue(name, out var text)
&& double.TryParse(text?.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var value)
? value
: fallback;
/// The stored "True"/"False" under , or .
public static bool ReadBool(IReadOnlyDictionary settings, string name, bool fallback) =>
settings.TryGetValue(name, out var text) && bool.TryParse(text?.Trim(), out var value)
? value
: fallback;
/// The stored text under , or .
public static string ReadString(IReadOnlyDictionary settings, string name, string fallback) =>
settings.TryGetValue(name, out var text) && text is not null ? text : fallback;
public static string Format(decimal value) => value.ToString("G", CultureInfo.InvariantCulture);
public static string Format(double value) => value.ToString("R", CultureInfo.InvariantCulture);
public static string Format(bool value) => value ? "True" : "False";
}