using System.Globalization; using Titano.Core.Analysis; using Titano.Core.Analysis.Deep; using Titano.Core.Localization; using Titano.Core.Project; namespace Titano.Core.Advisor; /// /// The analysis advisor (BRIEF §10.3, §10.4): turns the light and deep analyses into explained recommendations /// (), stores them as Auto values without touching manual ones (), and takes /// the heavier per-section decisions on request (). Deterministic; every reason is a resource /// key plus arguments. /// public static class Advisor { /// /// Recommendations for every parameter the rules cover (see ), in section order, plus /// every finding of both analyses. Without a deep report, pixel-based parameters get missing-data recommendations. /// public static AdvisorResult Recommend(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context) { ArgumentNullException.ThrowIfNull(light); ArgumentNullException.ThrowIfNull(context); var findings = new List(light.Findings); if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding())); return new AdvisorResult(new AdvisorRules(light, deep, context).Run(), findings); } /// /// Fills , and of every /// advised parameter. Manual values stay in force; missing data changes no value (its reason is stored only where no /// advice exists yet). Plain, non-Auto settings are left to . /// public static ProjectSettings Apply(ProjectSettings settings, AdvisorResult result) { ArgumentNullException.ThrowIfNull(settings); ArgumentNullException.ThrowIfNull(result); foreach (var recommendation in result.Recommendations) { if (ParameterBindings.Find(recommendation.Parameter)?.Advise is { } advise) settings = advise(settings, recommendation); } return settings; } /// /// "Optimise section" (BRIEF §10.4): applies the section's advice, returns module switches (deflicker, regions, holy /// grail, white balance, motion blur, stabilisation, timing mode, export preset) to Auto where the advice differs /// from the value in force, and takes the plain decisions (fit-to duration, hero frame when unset, exclusions added /// to the user's, crop placement when centred, output size cap). Missing data changes nothing. /// Returns every effective value that changed, with its reason. /// public static OptimiseResult Optimise(AdvisorSection section, ProjectSettings settings, AdvisorResult result) { ArgumentNullException.ThrowIfNull(settings); ArgumentNullException.ThrowIfNull(result); var optimised = settings; var changes = new List(); foreach (var binding in ParameterBindings.All) { if (binding.Section != section || result.Find(binding.Parameter) is not { } recommendation) continue; if (binding.Advise is { } advise) optimised = advise(optimised, recommendation); if (recommendation.Value is null) continue; if (binding.IsModuleSwitch && !ParameterBindings.SameValue(binding.Effective(optimised), recommendation.Value)) optimised = binding.ResetToAuto!(optimised); if (binding.Set is { } set) optimised = set(optimised, recommendation); var before = binding.Effective(settings); var after = binding.Effective(optimised); if (!ParameterBindings.SameValue(before, after)) changes.Add(new SettingChange(binding.Parameter, before, after, recommendation.ReasonKey, recommendation.ReasonArgs)); } return new OptimiseResult(optimised, changes); } /// Localised text of a recommendation in the current UI culture. public static string Explain(Recommendation recommendation) { ArgumentNullException.ThrowIfNull(recommendation); return Loc.L(recommendation.ReasonKey, [.. recommendation.ReasonArgs]); } /// /// Localised text of a stored reason (/): numeric /// arguments, stored culture-neutral, are parsed back so they format with the current culture. /// public static string Explain(string reasonKey, IReadOnlyList? reasonArgs) { ArgumentNullException.ThrowIfNull(reasonKey); var args = (reasonArgs ?? []).Select(a => double.TryParse(a, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) ? (object)number : a).ToArray(); return Loc.L(reasonKey, args); } }