using System.Globalization; using System.IO; using Housing.Models; namespace Housing.Services; public static class InspectionSettingsStore { public static InspectionJudgementSettings Load(string filePath) { if (!File.Exists(filePath)) { var defaultSettings = new InspectionJudgementSettings(); Save(filePath, defaultSettings); return defaultSettings; } var values = IniFile.LoadSection(filePath, "Judgement"); return new InspectionJudgementSettings { VMin = GetDecimal(values, "VMin", 0.00m), VMax = GetDecimal(values, "VMax", 9.99m), AMin = GetDecimal(values, "AMin", 0.00m), AMax = GetDecimal(values, "AMax", 9.99m) }; } public static void Save(string filePath, InspectionJudgementSettings settings) { var lines = new[] { "[Judgement]", $"VMin={Format(settings.VMin)}", $"VMax={Format(settings.VMax)}", $"AMin={Format(settings.AMin)}", $"AMax={Format(settings.AMax)}" }; File.WriteAllLines(filePath, lines); } private static decimal GetDecimal(Dictionary values, string key, decimal defaultValue) { if (!values.TryGetValue(key, out var value)) { return defaultValue; } return decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : defaultValue; } private static string Format(decimal value) { return value.ToString("0.######", CultureInfo.InvariantCulture); } }