using System.Globalization;
namespace OpenNest.Data;
///
/// The shop's names for thicknesses, such as "10 ga" for 0.135, and the "10 ga (0.135)" text
/// that shows them. A nest stores its name (); one without is
/// named here: the machine's name for its material and thickness first, otherwise the nearest
/// standard gauge or plate fraction.
///
public static class ThicknessNames
{
// As machine thicknesses match: decimals typed to three places (0.135 for 10 ga, 0.188 for
// 3/16") still name their gauge, and gauges are far further apart.
private const double ToleranceInches = 0.001;
/// "10 ga (0.135)", or the plain decimal when is blank.
public static string Format(string? name, double thickness)
{
var number = thickness.ToString("0.####", CultureInfo.CurrentCulture);
return string.IsNullOrWhiteSpace(name) ? number : $"{name.Trim()} ({number})";
}
/// The thickness text for : its name, or one looked up, then the decimal.
public static string Format(Nest nest, IEnumerable machines)
{
ArgumentNullException.ThrowIfNull(nest);
var name = string.IsNullOrWhiteSpace(nest.ThicknessName) ? For(machines, nest) : nest.ThicknessName;
return Format(name, nest.Thickness);
}
/// The thickness text for a saved nest: its stored name, or one looked up, then the decimal.
public static string Format(NestRecord record, IEnumerable machines)
{
ArgumentNullException.ThrowIfNull(record);
var name = string.IsNullOrWhiteSpace(record.ThicknessName)
? For(machines, record.Material, record.MaterialGrade, record.Thickness, record.Units)
: record.ThicknessName;
return Format(name, record.Thickness);
}
/// The name for the thickness of ; blank when nothing names it.
public static string For(IEnumerable machines, Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
return For(machines, nest.Material?.Name, nest.Material?.Grade, nest.Thickness, nest.Units);
}
///
/// The name for (in ) of
/// : the first of that names it, otherwise
/// the nearest standard gauge or plate fraction; blank when nothing names it.
///
public static string For(IEnumerable machines, string? material, string? grade, double thickness, Units units)
{
if (!double.IsFinite(thickness) || thickness <= 0)
return "";
foreach (var machine in machines ?? Enumerable.Empty())
{
var name = MachineCuttingDefaults.Find(machine, material, grade, thickness, units)?.Name;
if (!string.IsNullOrWhiteSpace(name))
return name.Trim();
}
return StandardThicknesses.FindNearest(thickness, units, ToleranceInches)?.Name ?? "";
}
}