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