using System.Globalization;
using System.Windows.Input;
using Chronos.App.Infrastructure;
using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Core.Project;
using Chronos.Core.Video;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Editor;
///
/// A collapsible group of the Export step's Output panel (owner review 1, R3; DESIGN §4): its name with a chevron and,
/// while collapsed, a muted one-line summary of its values. Open or closed is this user's choice, kept by the app
/// (), not in the project.
///
public sealed class SettingGroup : ObservableObject
{
private readonly string _nameKey;
private readonly string _toolTipKey;
private readonly bool _expandedByDefault;
private string? _summary;
/// The group's key in ("export.encoding").
public SettingGroup(string key, string nameKey, string toolTipKey, bool expandedByDefault)
{
Key = key;
(_nameKey, _toolTipKey, _expandedByDefault) = (nameKey, toolTipKey, expandedByDefault);
ToggleCommand = new RelayCommand(() => IsExpanded = !IsExpanded);
}
public string Key { get; }
/// "Encoding".
public string Header => Loc.L(_nameKey);
/// What opening the group shows.
public string ToolTip => Loc.L(_toolTipKey);
public bool IsExpanded
{
get => EditorSettings.Groups.IsExpanded(Key, _expandedByDefault);
set
{
if (value == IsExpanded) return;
EditorSettings.Groups.Set(Key, value);
OnPropertyChanged();
}
}
/// "Variable · 40 Mb/s · GOP 1 s · B 2 · High": the values, shown while the group is collapsed.
public string? Summary { get => _summary; set => Set(ref _summary, value); }
public ICommand ToggleCommand { get; }
/// The UI language changed, or another editor toggled the group: everything is read again.
public void Refresh() => OnAllPropertiesChanged();
}
///
/// What the Export step's advanced encoding rows offer for a codec and colour depth (owner review 1, R8; decision 6), and
/// the keyframe interval in seconds as the project stores it in frames. Every value is an
/// member nodes of phase 4 already read, so none of them raises the settings format.
///
public static class ExportRules
{
/// Keyframe intervals offered, in seconds (0.5 to 10, step 0.5).
public static IReadOnlyList KeyframeSeconds { get; } = [.. Enumerable.Range(1, 20).Select(i => i * 0.5)];
/// B-frame counts offered besides Auto.
public static IReadOnlyList BFrameCounts { get; } = [0, 1, 2, 3, 4];
///
/// The standard resolutions of the Resolution choice (decision 7): the landscape box written into
/// /, which the chosen proportions are fitted in.
///
public static IReadOnlyList<(string Key, string LabelKey, int Width, int Height)> StandardResolutions { get; } =
[.. ExportPresets.Resolutions.Select(r => (r.Key, "Export.Resolution." + ResolutionLabel(r.Key), r.Width, r.Height))];
/// Only the hardware codecs have a rate control, a keyframe interval and B-frames to choose.
public static bool HasRateControl(VideoCodec codec) => !VideoCodecInfo.IsManaged(codec);
/// Why has no rate control or keyframe interval to choose; null when it has.
public static string? RateControlBlocked(VideoCodec codec) =>
HasRateControl(codec) ? null : Loc.L("Tooltips.Export.RateControlNA", Loc.L("Export.Codec." + codec));
///
/// Why with takes no B-frames (Baseline, MJPEG, GIF, APNG and the
/// image sequences); null when it does.
///
public static string? BFramesBlocked(VideoCodec codec, CodecProfile profile) => codec switch
{
VideoCodec.H264 when profile == CodecProfile.H264Baseline => Loc.L("Tooltips.Export.BFramesNA", ProfileLabel(profile)),
_ when VideoCodecInfo.IsManaged(codec) => Loc.L("Tooltips.Export.BFramesNA", Loc.L("Export.Codec." + codec)),
_ => null,
};
///
/// The profiles offers at : H.264 Baseline, Main and High; HEVC Main
/// at 8 bits or Main 10 above; AV1 Main; none for the managed codecs.
///
public static IReadOnlyList Profiles(VideoCodec codec, int bitDepth) => codec switch
{
VideoCodec.H264 => [CodecProfile.H264Baseline, CodecProfile.H264Main, CodecProfile.H264High],
VideoCodec.Hevc => [bitDepth > 8 ? CodecProfile.HevcMain10 : CodecProfile.HevcMain],
VideoCodec.Av1 => [CodecProfile.Av1Main],
_ => [],
};
/// Why the profile cannot be chosen (the codec has one, or none); null when it can.
public static string? ProfileBlocked(VideoCodec codec, int bitDepth) =>
Profiles(codec, bitDepth).Count > 1 ? null : Loc.L("Tooltips.Export.ProfileNA", Loc.L("Export.Codec." + codec));
/// "Baseline", "Main", "High", "Main 10".
public static string ProfileLabel(CodecProfile profile) => Loc.L(profile switch
{
CodecProfile.H264Baseline => "Export.Profile.Baseline",
CodecProfile.H264High => "Export.Profile.High",
CodecProfile.HevcMain10 => "Export.Profile.Main10",
_ => "Export.Profile.Main",
});
/// The interval of as stored: whole frames at , at least one.
public static int GopFrames(double seconds, Rational frameRate) => int.Max(1, (int)double.Round(seconds * frameRate.Value));
/// The interval of at , in seconds.
public static double GopSeconds(int gopFrames, Rational frameRate) => frameRate.Value > 0 ? gopFrames / frameRate.Value : 0;
/// The offered interval nearest at (what the choice shows).
public static double NearestKeyframeSeconds(int gopFrames, Rational frameRate) =>
double.Clamp(double.Round(GopSeconds(gopFrames, frameRate) * 2) / 2, KeyframeSeconds[0], KeyframeSeconds[^1]);
///
/// A keyframe interval chosen by hand keeps its seconds when the frame rate changes: at
/// as frames at .
///
public static int RescaleGop(int gopFrames, Rational from, Rational to) => GopFrames(GopSeconds(gopFrames, from), to);
/// "1.5 s".
public static string Seconds(double seconds) => Loc.L("Export.Seconds", seconds.ToString("0.##", Loc.Culture));
private static string ResolutionLabel(string key) => key switch
{
"4k" => "Uhd4k",
"dci4k" => "Dci4k",
_ => key,
};
}
/// The one-line summaries of the Export step (owner review 1, R3): a collapsed group's values and the footer's line.
public static class ExportSummaries
{
private static string Separator => Loc.Separator;
///
/// "Variable · 40 Mb/s · GOP 1 s · B 2 · High" for a video codec; the quality ("Quality 95") or n/a for the managed
/// codecs, which have no rate control.
///
public static string Encoding(ResolvedExport export, long bitRate)
{
ArgumentNullException.ThrowIfNull(export);
if (!ExportRules.HasRateControl(export.Codec))
{
var managed = EditorText.Encoding(export, 0, null);
return managed.Length > 0 ? managed : Loc.L("Common.NotApplicable");
}
var parts = new List { RateControlLabel(export.RateControl) };
parts.Add(export.RateControl == RateControl.Quality ? Loc.L("Editor.Export.QualityValue", export.Quality) : EditorText.BitRate(bitRate));
parts.Add(Loc.L("Export.Summary.Keyframes", ExportRules.Seconds(ExportRules.GopSeconds(export.GopFrames, export.FrameRate))));
if (ExportRules.BFramesBlocked(export.Codec, export.Profile) is null) parts.Add(Loc.L("Export.Summary.BFrames", export.BFrames));
if (export.Profile != CodecProfile.Auto) parts.Add(ExportRules.ProfileLabel(export.Profile));
return string.Join(Separator, parts);
}
/// "Forward · no fades", "Boomerang · ×2 · fades 1 s / 1.5 s · hold 2 s".
public static string Playback(PlaybackSettings? playback)
{
playback ??= new PlaybackSettings();
var parts = new List { DirectionLabel(playback.EffectiveDirection) };
if (playback.EffectiveLoops > 1) parts.Add(Loc.L("Export.Summary.Loops", playback.EffectiveLoops));
parts.Add(playback.EffectiveFadeIn > 0 || playback.EffectiveFadeOut > 0
? Loc.L("Export.Summary.Fades", ExportRules.Seconds(playback.EffectiveFadeIn), ExportRules.Seconds(playback.EffectiveFadeOut))
: Loc.L("Export.Summary.NoFades"));
if (playback.EffectiveHoldEnd > 0) parts.Add(Loc.L("Export.Summary.Hold", ExportRules.Seconds(playback.EffectiveHoldEnd)));
return string.Join(Separator, parts);
}
/// "2026-09-28 Harbour sunset - 4K archive.mp4 · Sync": the result's name, then Sync when it is on.
public static string File(string? resultName, string folder, bool sync)
{
var parts = new List { resultName is { Length: > 0 } name ? name : folder };
if (sync) parts.Add(Loc.L("Editor.Export.Sync"));
return string.Join(Separator, parts);
}
///
/// The footer's line (decision 5): "2026-09-28 Harbour sunset - 4K….mp4 · 1.2 GB · about 4 min · 42 s · 1,260 frames"; the
/// size and time are left out when there is no estimate (no encoder here).
///
public static string Footer(string resultName, ExportEstimate? estimate, double durationSeconds, int frames)
{
ArgumentNullException.ThrowIfNull(resultName);
var duration = Formatting.Duration(TimeSpan.FromSeconds(double.Max(0, durationSeconds)));
var count = Loc.Plural("Export.Footer.Frames", frames);
return estimate is null
? string.Join(Separator, resultName, duration, count)
: Loc.L("Export.Footer.Summary", resultName, Formatting.Bytes(estimate.Bytes), Loc.L("Editor.Export.About", Formatting.Duration(estimate.Time)), duration, count);
}
/// "Variable", "Constant", "Quality".
public static string RateControlLabel(RateControl rateControl) => Loc.L("Export.RateControl." + rateControl);
/// "Forward", "Reverse", "Boomerang" ().
public static string DirectionLabel(string direction) =>
Loc.L("Export.Direction." + CultureInfo.InvariantCulture.TextInfo.ToTitleCase(PlaybackDirection.IsKnown(direction) ? direction.ToLowerInvariant() : PlaybackDirection.Forward));
}