using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.CommonLines;
using OpenNest.Data;
namespace OpenNest.Desktop;
///
/// The Tools > Nest Defaults dialog's lists and limits, toolkit-neutral: the machines by
/// name with the one new nests are assigned selected, the common-line choices, the boxes'
/// ranges and how a saved value is shown in them.
///
public static class NestDefaultsChoices
{
/// The smallest plate width or length the boxes take.
public const double SizeMinimum = 0.001;
/// The smallest spacing the boxes take.
public const double SpacingMinimum = 0;
/// The largest size or spacing the boxes take.
public const double Maximum = 1000000;
/// Decimals the boxes show.
public const int Decimals = 4;
public const string QuadrantHint = "Click a quadrant to select it.";
public const string CommonLineToolTip =
"The common-line usage new nests start with. Only drawings with \"Allow common line\" set in Drawing Properties take part.";
/// The common-line list, in the order the WinForms box shows it.
public static IReadOnlyList<(string Text, CommonLineUsage Usage)> CommonLineUsages { get; } =
[
("Off", CommonLineUsage.Off),
("When it adds parts", CommonLineUsage.WhenItAddsParts),
("Whenever available", CommonLineUsage.Whenever),
];
/// The list index of ; an unknown usage shows as Off.
public static int CommonLineIndex(CommonLineUsage usage)
{
for (var i = 0; i < CommonLineUsages.Count; i++)
{
if (CommonLineUsages[i].Usage == usage)
return i;
}
return 0;
}
/// The usage at list index ; none selected is Off.
public static CommonLineUsage CommonLineAt(int index) =>
index >= 0 && index < CommonLineUsages.Count ? CommonLineUsages[index].Usage : CommonLineUsage.Off;
/// The machines to choose from, by name.
public static IReadOnlyList SortedMachines(IEnumerable machines) =>
(machines ?? Enumerable.Empty())
.OrderBy(m => m.Name, StringComparer.CurrentCultureIgnoreCase)
.ToList();
///
/// The machine id to save: the chosen machine's, or with none to choose (no machines on
/// file) the kept as it was.
///
public static Guid? MachineIdToSave(MachineConfig chosen, Guid? savedMachineId) =>
chosen?.Id ?? savedMachineId;
///
/// as a box between and
/// shows it: clamped, with NaN or infinity as the minimum.
///
public static double Clamp(double value, double minimum, double maximum)
{
if (double.IsNaN(value) || double.IsInfinity(value))
return minimum;
return System.Math.Clamp(value, minimum, maximum);
}
}