using Chronos.App.Infrastructure;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Settings;
///
/// The log rows of Settings › Advanced (owner review 2, R11): level, retention, file size, total size and rotation of this
/// PC's log files, and how much of each the diagnostics bundle keeps. Each choice is stored and saved at once, and saving
/// applies it to the files (). A value set elsewhere (an imported settings file) is listed with
/// the presets, never shown as another one.
///
public sealed class LogSettingsRows(AppSettings settings, Action save) : ObservableObject
{
public static readonly int[] RetentionPresets = [1, 3, 7, 14, 30, 90, 365];
public static readonly int[] FileSizePresets = [10, 20, 50, 100, 200, 500];
public static readonly int[] TotalSizePresets = [256, 512, 1024, 2048, 5120, 10240];
public static readonly int[] DiagnosticsPresets = [1, 2, 4, 8, 16, 32];
public IReadOnlyList> Levels =>
[.. Enum.GetValues().Select(l => new SettingsOption(l, Loc.L("Settings.Advanced.Level." + l)))];
public SettingsOption SelectedLevel
{
get => Levels.FirstOrDefault(l => l.Value == settings.LogLevel) ?? Levels.First(l => l.Value == LogLevel.Info);
set => Store(value, settings.LogLevel, v => settings.LogLevel = v);
}
public IReadOnlyList> RetentionChoices =>
[.. With(RetentionPresets, settings.LogRetentionDays).Select(n => new SettingsOption(n, Loc.Plural("Settings.Advanced.Days", n)))];
public SettingsOption SelectedRetention
{
get => RetentionChoices.First(c => c.Value == settings.LogRetentionDays);
set => Store(value, settings.LogRetentionDays, v => settings.LogRetentionDays = v);
}
public IReadOnlyList> FileSizeChoices => Sizes(FileSizePresets, settings.LogMaxFileMb);
public SettingsOption SelectedFileSize
{
get => FileSizeChoices.First(c => c.Value == settings.LogMaxFileMb);
set => Store(value, settings.LogMaxFileMb, v => settings.LogMaxFileMb = v);
}
public IReadOnlyList> TotalSizeChoices => Sizes(TotalSizePresets, settings.LogMaxTotalMb);
public SettingsOption SelectedTotalSize
{
get => TotalSizeChoices.First(c => c.Value == settings.LogMaxTotalMb);
set => Store(value, settings.LogMaxTotalMb, v => settings.LogMaxTotalMb = v);
}
public IReadOnlyList> Rotations =>
[.. Enum.GetValues().Select(r => new SettingsOption(r, Loc.L("Settings.Advanced.Rotation." + r)))];
public SettingsOption SelectedRotation
{
get => Rotations.FirstOrDefault(r => r.Value == settings.LogRotation) ?? Rotations[0];
set => Store(value, settings.LogRotation, v => settings.LogRotation = v);
}
public IReadOnlyList> DiagnosticsChoices =>
[.. With(DiagnosticsPresets, settings.DiagnosticsLogMb).Select(n => new SettingsOption(n, Megabytes(n)))];
public SettingsOption SelectedDiagnostics
{
get => DiagnosticsChoices.First(c => c.Value == settings.DiagnosticsLogMb);
set => Store(value, settings.DiagnosticsLogMb, v => settings.DiagnosticsLogMb = v);
}
/// The defaults of every row (Reset section on Advanced); the caller saves.
public void Reset()
{
var defaults = new AppSettings();
settings.LogLevel = defaults.LogLevel;
settings.LogRetentionDays = defaults.LogRetentionDays;
settings.LogMaxFileMb = defaults.LogMaxFileMb;
settings.LogMaxTotalMb = defaults.LogMaxTotalMb;
settings.LogRotation = defaults.LogRotation;
settings.DiagnosticsLogMb = defaults.DiagnosticsLogMb;
Refresh();
}
/// Labels in the current language, values as the settings have them now.
internal void Refresh() => OnAllPropertiesChanged();
private void Store(SettingsOption? choice, T current, Action set, [System.Runtime.CompilerServices.CallerMemberName] string? property = null)
{
if (choice is null || EqualityComparer.Default.Equals(choice.Value, current)) return;
set(choice.Value);
save();
OnPropertyChanged(property);
}
/// The presets in order, then "No limit" (0); joins them when it is another size.
private static IReadOnlyList> Sizes(int[] presets, int current) =>
[
.. With(presets, current).Where(n => n > 0).Select(n => new SettingsOption(n, Megabytes(n))),
new(0, Loc.L("Settings.Advanced.NoLimit")),
];
private static IEnumerable With(int[] presets, int current) => (presets.Contains(current) ? presets : presets.Append(current)).Order();
private static string Megabytes(int n) => Loc.L("Common.Size.MB", Formatting.Count(n));
}