using System.Collections; using System.Globalization; using Titano.Core.Export; using Titano.Core.Project; namespace Titano.Core.Advice; /// How one advisor parameter reads and writes . internal sealed class ParameterBinding { public required string Parameter { get; init; } public required AdvisorSection Section { get; init; } /// A module on/off decision: returns it to Auto when the advice differs. public bool IsModuleSwitch { get; init; } /// The value in force (manual, else advised), null when neither exists. public required Func Effective { get; init; } /// Auto fields: stores the advice and its reason, never touching a manual value. public Func? Advise { get; init; } /// Auto fields: drops the manual value so the advice applies. public Func? ResetToAuto { get; init; } /// Plain fields: the heavier decision takes (each guards user choices). public Func? Set { get; init; } } /// The binding of every id, in section order. internal static class ParameterBindings { public static IReadOnlyList All { get; } = [ new() { Parameter = AdvisorParameters.Hero, Section = AdvisorSection.Frames, Effective = s => s.HeroFrame, // A hero waiting for its photo (taken from another copy) is the user's choice too. Set = (s, r) => s.HeroFrame is null && !s.HeroPending ? s with { HeroFrame = (int)r.Value! } : s, }, new() { Parameter = AdvisorParameters.Excluded, Section = AdvisorSection.Frames, Effective = s => s.ExcludedFrames, Set = (s, r) => s with { ExcludedFrames = [.. s.ExcludedFrames.Union((int[])r.Value!).Order()] }, }, AutoField(AdvisorParameters.TimingMode, AdvisorSection.Timing, true, s => s.Timing.Mode, (s, a) => s with { Timing = s.Timing with { Mode = a } }), AutoField(AdvisorParameters.FrameRate, AdvisorSection.Timing, false, s => s.Timing.FrameRate, (s, a) => s with { Timing = s.Timing with { FrameRate = a } }), new() { Parameter = AdvisorParameters.FitToSeconds, Section = AdvisorSection.Timing, Effective = s => s.Timing.FitToSeconds, Set = (s, r) => s with { Timing = s.Timing with { FitToSeconds = (double)r.Value! } }, }, AutoField(AdvisorParameters.Deflicker, AdvisorSection.Exposure, true, s => s.Exposure.Deflicker, (s, a) => s with { Exposure = s.Exposure with { Deflicker = a } }), AutoField(AdvisorParameters.DeflickerWindow, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerWindow, (s, a) => s with { Exposure = s.Exposure with { DeflickerWindow = a } }), AutoField(AdvisorParameters.DeflickerStrength, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerStrength, (s, a) => s with { Exposure = s.Exposure with { DeflickerStrength = a } }), AutoField(AdvisorParameters.Regions, AdvisorSection.Exposure, true, s => s.Exposure.Regions, (s, a) => s with { Exposure = s.Exposure with { Regions = a } }), AutoField(AdvisorParameters.HolyGrail, AdvisorSection.Exposure, true, s => s.Exposure.HolyGrail, (s, a) => s with { Exposure = s.Exposure with { HolyGrail = a } }), AutoField(AdvisorParameters.TransitionLength, AdvisorSection.Exposure, false, s => s.Exposure.TransitionLength, (s, a) => s with { Exposure = s.Exposure with { TransitionLength = a } }), AutoField(AdvisorParameters.WhiteBalanceSmoothing, AdvisorSection.Exposure, true, s => s.Exposure.WhiteBalanceSmoothing, (s, a) => s with { Exposure = s.Exposure with { WhiteBalanceSmoothing = a } }), AutoField(AdvisorParameters.MotionBlur, AdvisorSection.Motion, true, s => s.Motion.MotionBlur, (s, a) => s with { Motion = s.Motion with { MotionBlur = a } }), AutoField(AdvisorParameters.TargetShutterAngle, AdvisorSection.Motion, false, s => s.Motion.TargetShutterAngle, (s, a) => s with { Motion = s.Motion with { TargetShutterAngle = a } }), AutoField(AdvisorParameters.FlowQuality, AdvisorSection.Motion, false, s => s.Motion.FlowQuality, (s, a) => s with { Motion = s.Motion with { FlowQuality = a } }), AutoField(AdvisorParameters.Stabilize, AdvisorSection.Motion, true, s => s.Motion.Stabilize, (s, a) => s with { Motion = s.Motion with { Stabilize = a } }), AutoField(AdvisorParameters.StabilizeSmoothing, AdvisorSection.Motion, false, s => s.Motion.StabilizeSmoothing, (s, a) => s with { Motion = s.Motion with { StabilizeSmoothing = a } }), AutoField(AdvisorParameters.Aspect, AdvisorSection.Camera, false, s => s.Camera.Aspect, (s, a) => s with { Camera = s.Camera with { Aspect = a } }), new() { Parameter = AdvisorParameters.OffsetY, Section = AdvisorSection.Camera, Effective = s => s.Camera.OffsetY, Set = (s, r) => s.Camera.OffsetY == 0 ? s with { Camera = s.Camera with { OffsetY = (double)r.Value! } } : s, }, AutoField(AdvisorParameters.ExportPreset, AdvisorSection.Export, true, s => s.Export.Preset, (s, a) => s with { Export = s.Export with { Preset = a } }), new() { Parameter = AdvisorParameters.Resolution, Section = AdvisorSection.Export, Effective = s => s.Export.Overrides is { Width: { } w, Height: { } h } ? Size(w, h) : null, Set = CapResolution, }, ]; public static ParameterBinding? Find(string parameter) => All.FirstOrDefault(b => b.Parameter == parameter); /// Value equality that compares lists element by element. public static bool SameValue(object? a, object? b) { if (a is IEnumerable x && b is IEnumerable y && a is not string && b is not string) return x.Cast().SequenceEqual(y.Cast()); return Equals(a, b); } /// Recommendation arguments as stored in (culture-neutral text). public static string[] Strings(IReadOnlyList args) => [.. args.Select(a => Convert.ToString(a, CultureInfo.InvariantCulture) ?? string.Empty)]; public static string Size(int width, int height) => string.Create(CultureInfo.InvariantCulture, $"{width}x{height}"); private static ParameterBinding AutoField(string parameter, AdvisorSection section, bool module, Func> get, Func, ProjectSettings> set) => new() { Parameter = parameter, Section = section, IsModuleSwitch = module, Effective = s => EffectiveOf(get(s)), Advise = (s, r) => set(s, Advise(get(s), r)), ResetToAuto = s => set(s, get(s).Reset()), }; private static object? EffectiveOf(Auto auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : auto.HasAdvice ? auto.AutoValue : null; /// /// Stores a value with its reason. Missing data changes no value: the missing-data reason is stored only when the /// parameter has no advice yet, so the Auto tooltip can say why it is empty. /// private static Auto Advise(Auto auto, Recommendation recommendation) { var args = Strings(recommendation.ReasonArgs); if (recommendation.Value is null) return auto.HasAdvice ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args }; if (recommendation.Value is not T value) throw new InvalidOperationException($"{recommendation.Parameter} expects {typeof(T).Name}, got {recommendation.Value.GetType().Name}."); return auto.WithAdvice(value, recommendation.ReasonKey, args); } /// Overrides the output size when the preset in force is larger than the recommended cap. private static ProjectSettings CapResolution(ProjectSettings settings, Recommendation recommendation) { var parts = ((string)recommendation.Value!).Split('x'); var width = int.Parse(parts[0], CultureInfo.InvariantCulture); var height = int.Parse(parts[1], CultureInfo.InvariantCulture); var id = EffectiveOf(settings.Export.Preset) as string; if (id is null || ExportPresets.Find(id) is not { Width: > 0 } preset || (preset.Width <= width && preset.Height <= height)) return settings; return settings with { Export = settings.Export with { Overrides = settings.Export.Overrides with { Width = width, Height = height } } }; } }