using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.Data;
using OpenNest.Geometry;
namespace OpenNest.Desktop;
///
/// What Nest > Edit (the Edit Nest dialog) edits: the nest's name, dates, thickness and its name,
/// material and grade, customer and job number, status, author, units, notes and its plate defaults (size,
/// part and edge spacing, quadrant). reads a nest,
/// is when the dialog's Done button is enabled and writes the nest as
/// Done does.
///
public sealed record NestInfoValues
{
/// The range of the dialog's spacing boxes (the WinForms box limits).
public const double SpacingMinimum = 0;
public const double SpacingMaximum = 999999;
/// Decimals the spacing boxes show.
public const int SpacingDecimals = 4;
/// The step of the spacing boxes, in either unit (the WinForms boxes' 0.25).
public const double SpacingIncrement = 0.25;
public string Name { get; init; } = "";
public DateTime DateCreated { get; init; }
public DateTime DateLastModified { get; init; }
/// The thickness in .
public double Thickness { get; init; }
/// The shop's name for the thickness, such as "10 ga"; blank when it has none.
public string ThicknessName { get; init; } = "";
public string MaterialName { get; init; } = "";
public string MaterialGrade { get; init; } = "";
public string Customer { get; init; } = "";
/// The job number, or several separated by commas; blank when the nest has none.
public string JobNumber { get; init; } = "";
public NestStatus Status { get; init; } = NestStatus.Quote;
public string MadeBy { get; init; } = "";
public Units Units { get; init; } = Units.Inches;
public string Notes { get; init; } = "";
/// The default plate size as typed ("width x length").
public string SizeText { get; init; } = "";
public double PartSpacing { get; init; }
public double LeftSpacing { get; init; }
public double TopSpacing { get; init; }
public double RightSpacing { get; init; }
public double BottomSpacing { get; init; }
/// The default plate origin's quadrant, 1 to 4.
public int Quadrant { get; init; } = 1;
/// The nest's current info, as the dialog opens on it.
public static NestInfoValues FromNest(Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
var defaults = nest.PlateDefaults;
return new NestInfoValues
{
Name = nest.Name,
Notes = nest.Notes,
Customer = nest.Customer,
JobNumber = nest.JobNumber ?? "",
DateCreated = nest.DateCreated,
DateLastModified = nest.DateLastModified,
Thickness = nest.Thickness,
ThicknessName = nest.ThicknessName ?? "",
MaterialName = nest.Material?.Name ?? "",
MaterialGrade = nest.Material?.Grade ?? "",
Status = nest.Status,
// Shown as stored: a new nest got its author when it was made, and viewing an
// existing nest's info must not sign it.
MadeBy = nest.MadeBy ?? "",
SizeText = defaults.Size.ToString(),
PartSpacing = defaults.PartSpacing,
LeftSpacing = defaults.EdgeSpacing.Left,
TopSpacing = defaults.EdgeSpacing.Top,
RightSpacing = defaults.EdgeSpacing.Right,
BottomSpacing = defaults.EdgeSpacing.Bottom,
Quadrant = defaults.Quadrant,
Units = nest.Units,
};
}
///
/// Whether Done applies: a name, a thickness that reads (),
/// a size that parses and edge spacings that leave room on the plate both ways.
///
public bool CanApply(bool thicknessValid)
{
if (Name.Trim().Length == 0 || !thicknessValid)
return false;
if (!Size.TryParse(SizeText, out var size))
return false;
return LeftSpacing + RightSpacing < size.Length && TopSpacing + BottomSpacing < size.Width;
}
///
/// Writes the values to , as the dialog's Done does. The material
/// keeps its catalog details when its name and grade are unchanged
/// (). The size must parse ().
///
public void ApplyTo(Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
nest.Name = Name.Trim();
nest.Units = Units;
nest.Notes = Notes;
nest.Customer = Customer;
nest.JobNumber = JobNumber;
nest.DateCreated = DateCreated;
nest.DateLastModified = DateLastModified;
nest.Thickness = Thickness;
nest.ThicknessName = ThicknessName;
nest.Material = NestMaterialSelection.Apply(nest.Material, MaterialName, MaterialGrade);
nest.Status = Status;
nest.MadeBy = MadeBy;
nest.PlateDefaults.Size = Size.Parse(SizeText);
nest.PlateDefaults.PartSpacing = PartSpacing;
nest.PlateDefaults.EdgeSpacing = new Spacing(LeftSpacing, BottomSpacing, RightSpacing, TopSpacing);
nest.PlateDefaults.Quadrant = Quadrant;
}
///
/// The Made By to remember for later new nests (see )
/// when the user changed the shown to , trimmed
/// (blank falls back to the user name again); null when it was left as shown.
///
public static string MadeByToRemember(string shown, string typed)
{
var trimmed = (typed ?? "").Trim();
return trimmed == (shown ?? "").Trim() ? null : trimmed;
}
///
/// What Nest Info edits that undo puts back: everything but the identity fields (name,
/// status, author and dates), so an edit of only those records nothing.
///
public static string UndoableKey(Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
var defaults = nest.PlateDefaults;
return string.Join("|", nest.Units, nest.Notes, nest.Customer, nest.JobNumber, nest.Thickness, nest.ThicknessName, nest.Material?.Name,
nest.Material?.Grade, defaults.Size, defaults.PartSpacing, defaults.EdgeSpacing.Left,
defaults.EdgeSpacing.Bottom, defaults.EdgeSpacing.Right, defaults.EdgeSpacing.Top, defaults.Quadrant);
}
///
/// The material list: (the posts' materials and any already
/// listed) and the catalog's names, one per name ignoring case and spacing, sorted.
///
public static IReadOnlyList MaterialNames(IEnumerable listed, IEnumerable catalog) =>
(listed ?? Enumerable.Empty())
.Concat((catalog ?? Enumerable.Empty()).Select(m => m.Name))
.Where(name => !string.IsNullOrWhiteSpace(name))
.GroupBy(SharedListNames.Key)
.Select(group => group.First())
.OrderBy(name => name, StringComparer.OrdinalIgnoreCase)
.ToList();
/// The grades on file in for , sorted.
public static IReadOnlyList Grades(IEnumerable catalog, string material)
{
var key = SharedListNames.Key(material);
return (catalog ?? Enumerable.Empty())
.Where(m => SharedListNames.Key(m.Name) == key && !string.IsNullOrWhiteSpace(m.Grade))
.Select(m => m.Grade)
.GroupBy(SharedListNames.Key)
.Select(group => group.First())
.OrderBy(grade => grade, StringComparer.OrdinalIgnoreCase)
.ToList();
}
///
/// The grade kept when the material's key changes from to
/// that of : a grade belongs to its material, so a new material
/// clears a grade it does not have.
///
public static string GradeAfterMaterialChange(string previousKey, string material, string grade,
IReadOnlyList grades)
{
var key = SharedListNames.Key(material);
if (key != previousKey && !grades.Any(g => SharedListNames.Key(g) == SharedListNames.Key(grade)))
return "";
return grade ?? "";
}
}