using System.Collections;
using System.Globalization;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Core.Advice;
using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Video;
namespace Chronos.App.Editor;
/// The editor's composed texts (header summary, shot data, preset card lines), in the current UI language.
public static class EditorText
{
private static string Separator => Loc.Separator;
/// "3,600 frames · Canon EOS R5 · 2 s interval · 2 h 00 m · 3 warnings".
public static string Summary(SequenceContent content, int warnings)
{
ArgumentNullException.ThrowIfNull(content);
var s = content.Sequence;
var parts = new List { Loc.Plural("Editor.Summary.Frames", content.Count) };
if (s.CameraModel is { Length: > 0 } camera) parts.Add(camera);
if (content.Light.MedianIntervalSeconds is > 0 and var interval) parts.Add(Loc.L("Editor.Summary.Interval", Number(interval)));
if (content.Light.CaptureSpan > TimeSpan.Zero) parts.Add(Formatting.Duration(content.Light.CaptureSpan));
if (warnings > 0) parts.Add(Loc.Plural("Editor.Summary.Warnings", warnings));
return string.Join(Separator, parts);
}
///
/// "Render · 46 %", "Render · queued", "Deep analysis · paused": a job the server runs for a server sequence (the job
/// bar shows this PC's jobs only).
///
public static string ServerJob(Job job)
{
ArgumentNullException.ThrowIfNull(job);
return job switch
{
{ State: JobState.Running, ProgressTotal: > 0 } => JobText.Kind(job.Kind) + Separator + Formatting.Percent((double)job.ProgressDone / job.ProgressTotal),
{ State: JobState.Running } => JobText.Kind(job.Kind),
_ => JobText.Kind(job.Kind) + Separator + Loc.L("Jobs.State." + job.State),
};
}
/// "Render running on Studio NAS · 1,240 / 3,600 · 11.3 fps · 3 min left"; "Render waiting on Studio NAS".
public static string ServerJobDetail(Job job, string server)
{
ArgumentNullException.ThrowIfNull(job);
var key = job.State switch
{
JobState.Queued => "Tooltips.Editor.ServerJob.Queued",
JobState.Paused => "Tooltips.Editor.ServerJob.Paused",
_ => "Tooltips.Editor.ServerJob",
};
var parts = new List { Loc.L(key, JobText.Kind(job.Kind), server) };
if (JobText.Counter(job.ProgressDone, job.ProgressTotal) is { Length: > 0 } counter) parts.Add(counter);
if (JobText.Speed(job.Kind, job.ItemsPerSecond) is { Length: > 0 } speed) parts.Add(speed);
if (job.Eta is { } eta) parts.Add(Loc.L("Editor.ServerJob.Left", Formatting.Duration(eta)));
return string.Join(Separator, parts);
}
/// "3,600 frames · 24 fps · 2 min 30 s · 3840×2160 · source 8192×5464": what the export makes of the sequence (Export.html header).
public static string OutputSummary(SequenceContent content, ExportPlan plan)
{
ArgumentNullException.ThrowIfNull(content);
ArgumentNullException.ThrowIfNull(plan);
var render = plan.Setup.Plan;
var settings = plan.Setup.Encoder.Settings;
var parts = new List
{
Loc.Plural("Editor.Summary.Frames", content.Count),
Loc.L("Editor.Value.Fps", FrameRate(render.FrameRate)),
Formatting.Duration(TimeSpan.FromSeconds(render.DurationSeconds)),
};
if (settings.Width > 0 && settings.Height > 0) parts.Add(Size(settings.Width, settings.Height));
if (content.SourceWidth > 0 && content.SourceHeight > 0) parts.Add(Loc.L("Editor.Summary.Source", Size(content.SourceWidth, content.SourceHeight)));
return string.Join(Separator, parts);
}
/// "0413 / 3,600 · 18:02:11 · 1/8 s · f/8 · ISO 400 · 24 mm".
public static string Shot(Frame frame, int index, int count)
{
ArgumentNullException.ThrowIfNull(frame);
var parts = new List { Position(index, count) };
if (frame.CapturedAt is { } at) parts.Add(at.ToString("T", Loc.Culture));
if (frame.ExposureTime is > 0 and var t) parts.Add(Shutter(t));
if (frame.FNumber is > 0 and var f) parts.Add(Loc.L("Editor.Shot.Aperture", Number(f)));
if (frame.Iso is > 0 and var iso) parts.Add(Loc.L("Editor.Shot.Iso", iso));
if (frame.FocalLength is > 0 and var mm) parts.Add(Loc.L("Editor.Shot.Focal", Number(mm)));
// Shown over the photo, a left-to-right island.
return string.Join(Loc.IslandSeparator, parts);
}
/// "0413 / 3,600".
public static string Position(int index, int count) =>
(index + 1).ToString("D4", Loc.Culture) + " / " + count.ToString("N0", Loc.Culture);
/// "1/250 s", "1/8 s", "2 s".
public static string Shutter(double seconds) => seconds >= 0.3
? Loc.L("Editor.Shot.Shutter", Number(seconds))
: Loc.L("Editor.Shot.Shutter", "1/" + double.Round(1 / seconds).ToString("0", Loc.Culture));
/// "MP4 · HEVC Main10 · 3840×2160 · 24 fps".
public static string Format(ResolvedExport export, int width, int height)
{
ArgumentNullException.ThrowIfNull(export);
var parts = new List { Loc.L("Export.Container." + VideoCodecInfo.DefaultContainer(export.Codec)), CodecName(export.Codec, export.Profile, export.BitDepth) };
if (width > 0 && height > 0) parts.Add(Size(width, height));
parts.Add(Loc.L("Editor.Export.Fps", FrameRate(export.FrameRate)));
return string.Join(Separator, parts);
}
/// "120 Mb/s · 0.07 bpp · NVENC" for video, "Quality 95 · Chronos" for frames.
public static string Encoding(ResolvedExport export, long bitRate, string? encoder)
{
ArgumentNullException.ThrowIfNull(export);
var parts = new List();
if (!VideoCodecInfo.IsManaged(export.Codec))
{
parts.Add(BitRate(bitRate));
var bpp = export.BitsPerPixel > 0 ? export.BitsPerPixel : ExportPreset.DefaultBitsPerPixel(export.Codec);
if (export.BitRate == 0 && bpp > 0) parts.Add(Loc.L("Editor.Export.Bpp", bpp.ToString("0.00", Loc.Culture)));
}
else if (export.Codec is VideoCodec.Mjpeg or VideoCodec.JpegSequence or VideoCodec.Gif)
{
parts.Add(Loc.L("Editor.Export.QualityValue", export.Quality));
}
if (encoder is { Length: > 0 }) parts.Add(encoder);
return string.Join(Separator, parts);
}
/// "2.2 GB · about 5 min".
public static string Estimate(ExportEstimate estimate)
{
ArgumentNullException.ThrowIfNull(estimate);
return Formatting.Bytes(estimate.Bytes) + Separator + Loc.L("Editor.Export.About", Formatting.Duration(estimate.Time));
}
/// "120 Mb/s".
public static string BitRate(long bitsPerSecond) => Loc.L("Editor.Export.Mbps", (bitsPerSecond / 1e6).ToString(bitsPerSecond >= 10_000_000 ? "0" : "0.#", Loc.Culture));
/// "3840×2160".
public static string Size(int width, int height) => width.ToString(Loc.Culture) + "×" + height.ToString(Loc.Culture);
/// "23.976", "24".
public static string FrameRate(Rational rate) => rate.Denominator == 1 ? rate.Numerator.ToString(Loc.Culture) : rate.Value.ToString("0.###", Loc.Culture);
/// Technical codec name ("H.264 High", "HEVC Main10", "PNG"…).
public static string CodecName(VideoCodec codec, CodecProfile profile, int bitDepth) => codec switch
{
VideoCodec.Hevc when profile == CodecProfile.HevcMain10 || bitDepth > 8 => Loc.L("Export.Codec.HevcMain10"),
VideoCodec.Hevc => Loc.L("Export.Codec.Hevc"),
VideoCodec.H264 => Loc.L("Export.Codec.H264"),
_ => Loc.L("Export.Codec." + codec),
};
/// "+1.0", "−0.7" (EV with a sign).
public static string Ev(double ev) => ev.ToString("+0.0;−0.0;0.0", Loc.Culture);
/// "01:17.2" (minutes, seconds and tenths of a video position).
public static string Clock(double seconds)
{
var t = TimeSpan.FromSeconds(double.Max(0, seconds));
return ((int)t.TotalMinutes).ToString("00", Loc.Culture) + ":" + t.Seconds.ToString("00", Loc.Culture) + Loc.Culture.NumberFormat.NumberDecimalSeparator
+ (t.Milliseconds / 100).ToString(Loc.Culture);
}
/// "Centred", "+0.25" (crop placement −1…1).
public static string Placement(double value) => value == 0 ? Loc.L("Editor.Value.Centred") : value.ToString("+0.00;−0.00", Loc.Culture);
/// The label of an advisor parameter as its control shows it.
public static string ParameterLabel(string parameter) => parameter switch
{
AdvisorParameters.Hero => Loc.L("Editor.Frames.HeroLabel"),
AdvisorParameters.Excluded => Loc.L("Editor.Frames.ExcludedLabel"),
AdvisorParameters.TimingMode => Loc.L("Editor.Time.Mode"),
AdvisorParameters.FrameRate => Loc.L("Editor.Time.Rate"),
AdvisorParameters.FitToSeconds => Loc.L("Editor.Time.Fit"),
AdvisorParameters.Deflicker => Loc.L("Editor.Exposure.Deflicker"),
AdvisorParameters.DeflickerWindow => Loc.L("Editor.Exposure.Window"),
AdvisorParameters.DeflickerStrength => Loc.L("Editor.Exposure.Strength"),
AdvisorParameters.Regions => Loc.L("Editor.Exposure.Regions"),
AdvisorParameters.HolyGrail => Loc.L("Editor.Exposure.HolyGrail"),
AdvisorParameters.TransitionLength => Loc.L("Editor.Exposure.Transitions"),
AdvisorParameters.WhiteBalanceSmoothing => Loc.L("Editor.Exposure.WhiteBalance"),
AdvisorParameters.MotionBlur => Loc.L("Editor.Motion.Blur"),
AdvisorParameters.TargetShutterAngle => Loc.L("Editor.Motion.Shutter"),
AdvisorParameters.FlowQuality => Loc.L("Editor.Motion.Flow"),
AdvisorParameters.Stabilize => Loc.L("Editor.Motion.Stabilize"),
AdvisorParameters.StabilizeSmoothing => Loc.L("Editor.Motion.Smoothing"),
AdvisorParameters.Aspect => Loc.L("Editor.Camera.Aspect"),
AdvisorParameters.OffsetY => Loc.L("Editor.Camera.OffsetY"),
AdvisorParameters.ExportPreset => Loc.L("Editor.Export.Preset"),
AdvisorParameters.Resolution => Loc.L("Editor.Export.Resolution"),
_ => parameter,
};
///
/// "Deflicker window: 21 frames → 41 frames", one line of the Optimise toast. Bidi never mirrors an arrow, so a
/// right-to-left language reads it from its own resource, the arrow pointing from the old value to the new one.
///
public static string Change(SettingChange change) =>
Loc.L(Loc.IsRightToLeft ? "Editor.Optimise.ChangeRtl" : "Editor.Optimise.Change", ParameterLabel(change.Parameter), Value(change.Parameter, change.Before),
Value(change.Parameter, change.After));
/// A value of an advisor parameter as its control shows it (the "what changed" toast of Optimise).
public static string Value(string parameter, object? value) => (parameter, value) switch
{
(_, null) => Loc.L("Editor.Value.None"),
(AdvisorParameters.Hero, int frame) => Loc.L("Editor.Frames.HeroValue", (frame + 1).ToString("D4", Loc.Culture)),
(AdvisorParameters.Aspect, "source") => Loc.L("Editor.Aspect.Source"),
(AdvisorParameters.FrameRate, string rate) when FrameRateText.TryParse(rate, out var parsed) => Loc.L("Editor.Value.Fps", FrameRate(parsed)),
(AdvisorParameters.FitToSeconds, double seconds) => Formatting.Duration(TimeSpan.FromSeconds(seconds)),
(AdvisorParameters.DeflickerWindow or AdvisorParameters.TransitionLength, int frames) => Loc.Plural("Editor.Value.Frames", frames),
(AdvisorParameters.DeflickerStrength, double strength) => strength.ToString("0.00", Loc.Culture),
(AdvisorParameters.TargetShutterAngle, double degrees) => Loc.L("Editor.Value.Degrees", degrees.ToString("0", Loc.Culture)),
(AdvisorParameters.StabilizeSmoothing, double sigma) => Loc.Plural("Editor.Value.Frames", (long)double.Round(sigma)),
(AdvisorParameters.OffsetY, double offset) => Placement(offset),
(AdvisorParameters.Resolution, string size) => size.Replace('x', '×'),
(_, bool on) => Loc.L(on ? "Editor.Value.On" : "Editor.Value.Off"),
(_, TimingMode mode) => Loc.L("Editor.Timing." + mode),
(_, RegionMode mode) => mode == RegionMode.Off ? Loc.L("Editor.Value.Off") : Loc.L("Editor.Regions." + mode),
(_, FlowQualitySetting quality) => Loc.L("Editor.Flow." + quality),
(_, string id) when ExportPresets.Find(id) is { } preset => Loc.L(preset.NameKey),
(_, string text) => text,
(_, double number) => number.ToString("0.##", Loc.Culture),
(_, IEnumerable list) => Loc.Plural("Editor.Value.Frames", list.Cast