using System;
using OpenNest.Data;
using OpenNest.Undo;
namespace OpenNest.Desktop;
///
/// The Nest menu's whole-nest commands, toolkit-neutral: Edit (Nest Info) and Clear Nest, each
/// one undo step under its WinForms label opened with (the
/// window's BeginChange), and the defaults Edit's "Use these defaults for new nests" saves.
///
public static class NestCommands
{
/// The Clear Nest question, answered Yes to clear.
public const string ClearNestQuestion = "Remove all plates and parts from this nest?\nOne empty plate will remain.";
/// What a refused Clear Nest tells the user (WinForms also waits for a save).
public const string ClearNestBusyMessage = "Finish or cancel the current nesting, save or plate action first.";
///
/// Writes Nest Info's to as one "Nest Info"
/// step: a changed material or thickness takes 's spacing (the
/// nest's machine configuration, null when it has none on file), and an untouched trailing
/// sheet follows the new plate defaults. The name, status, author and creation date stay as
/// they are through undo, so an edit of only those records nothing.
/// runs inside the step, for the view's settings that follow
/// the material, thickness or units (its cut-off defaults).
///
/// Whether the step recorded a change.
public static bool EditNestInfo(Nest nest, PlateManager plates, NestInfoValues values, MachineConfig machine,
Func beginChange, Action afterApply = null)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(values);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Nest Info");
var before = NestInfoValues.UndoableKey(nest);
plates.ChangePlateDefaults(() => MachineCuttingDefaults.ApplySpacingAfter(nest, machine, () => values.ApplyTo(nest)));
var changed = NestInfoValues.UndoableKey(nest) != before;
if (changed)
change.MarkChanged();
afterApply?.Invoke();
change.Complete();
return changed;
}
///
/// The defaults new nests start from when Nest Info's "Use these defaults for new nests" is
/// checked: the nest's units, plate defaults and common-line usage, and its machine, or the
/// choice for a nest without one.
///
public static NestDefaults NewNestDefaults(Nest nest, NestDefaults saved)
{
ArgumentNullException.ThrowIfNull(nest);
var defaults = NestDefaults.FromNest(nest);
defaults.MachineId = nest.Machine?.Id ?? saved?.MachineId;
return defaults;
}
///
/// Nest > Clear Nest, as one "Clear Nest" step: every plate but the first is removed and
/// the first is emptied. A nest that is already one empty plate records nothing.
///
public static void ClearNest(PlateManager plates, Func beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Clear Nest");
if (plates.CanClearAll)
change.MarkChanged();
plates.ClearAll();
change.Complete();
}
}