using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.Data;
namespace OpenNest.Desktop;
///
/// One entry of the cut-time Acceleration list: a machine whose acceleration limits time the
/// moves, or none for full speed everywhere. Named as the WinForms list names them: a machine
/// with no saved motion values is marked "(placeholder)".
///
public sealed record CutTimeMachineChoice(MachineConfig Machine)
{
public const string FullSpeedText = "None (full speed)";
public override string ToString() =>
Machine == null ? FullSpeedText
: Machine.Motion == null ? $"{Machine.Name} (placeholder)"
: Machine.Name;
/// Full speed first, then by name.
public static List For(IEnumerable machines)
{
ArgumentNullException.ThrowIfNull(machines);
var choices = new List { new((MachineConfig)null) };
choices.AddRange(SavedCutTimeSettings.SortedByName(machines).Select(m => new CutTimeMachineChoice(m)));
return choices;
}
///
/// The index in of the remembered choice (): the first machine until one is saved, full speed
/// when saved so or when there are no machines.
///
public static int SavedIndex(IReadOnlyList choices, string saved)
{
ArgumentNullException.ThrowIfNull(choices);
var machine = SavedCutTimeSettings.ChooseMachine(
choices.Where(c => c.Machine != null).Select(c => c.Machine), saved);
for (var i = 0; i < choices.Count; i++)
{
if (ReferenceEquals(choices[i].Machine, machine))
return i;
}
return 0;
}
/// The machine's limits in , or null for full speed.
public MotionProfile GetMotion(Units units) => Motion(Machine, units);
///
/// 's limits (its placeholder without saved ones) in
/// , or null for full speed.
///
public static MotionProfile Motion(MachineConfig machine, Units units) =>
machine?.GetMotionOrPlaceholder().ToProfile(machine.Units, units);
}