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), RMin = GetOptionalDecimal(values, "RMin"), RMax = GetOptionalDecimal(values, "RMax") }; } 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)}", $"RMin={(settings.RMin.HasValue ? Format(settings.RMin.Value) : string.Empty)}", $"RMax={(settings.RMax.HasValue ? Format(settings.RMax.Value) : string.Empty)}" }; 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); } private static decimal? GetOptionalDecimal(Dictionary values, string key) { return values.TryGetValue(key, out var text) && decimal.TryParse(text, NumberStyles.Number, CultureInfo.InvariantCulture, out var value) ? value : null; } }