using System.Text.Json; using ClawdDotNet.Models; namespace ClawdDotNet.Services; public sealed class SettingsManager { private const string SettingsFileName = "Settings.json"; private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true, ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true, PropertyNameCaseInsensitive = true }; private readonly string _settingsPath; public AppSettings AppSettings { get; private set; } = new(); public SettingsManager(string? basePath = null) { var dir = basePath ?? AppDomain.CurrentDomain.BaseDirectory; _settingsPath = Path.Combine(dir, SettingsFileName); } public void Load() { if (!File.Exists(_settingsPath)) { AppSettings = new AppSettings(); Save(); // Defaults schreiben return; } try { var json = File.ReadAllText(_settingsPath); AppSettings = JsonSerializer.Deserialize(json, JsonOptions) ?? new AppSettings(); } catch { AppSettings = new AppSettings(); } } public void Save() { try { var dir = Path.GetDirectoryName(_settingsPath); if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); var json = JsonSerializer.Serialize(AppSettings, JsonOptions); File.WriteAllText(_settingsPath, json); } catch (Exception ex) { // Logging ist hier ggf. noch nicht verfügbar – Fallback auf MessageBox MessageBox.Show( $"Settings konnten nicht gespeichert werden:\n{ex.Message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }