using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenNest.Desktop;
/// The bar down a Drawings tab row's left edge, for a drawing with a required quantity.
public enum DrawingQuantityBar
{
/// No quantity is required (or quantities are hidden): no bar.
None,
/// All of the required quantity is nested: a green bar.
Complete,
/// Some of the required quantity is still to nest: an orange bar.
Short,
}
///
/// One row of the Drawings tab list, as the WinForms list draws it beside the drawing's
/// thumbnail: its name, "[on this plate] nested of required nested", its size and its area in
/// square units. A drawing whose nested parts left none remaining is highlighted, and a drawing
/// with a required quantity carries a bar saying whether all of it is nested.
///
public sealed record DrawingListRow(
Drawing Drawing,
string Name,
string Quantity,
string Size,
string Area,
bool IsHighlighted,
DrawingQuantityBar Bar)
{
/// The note an emptied list shows when Hide Fully Nested Drawings hid every drawing.
public const string AllHiddenText = "All drawings are fully nested and hidden.\n\nClick here to show them.";
///
/// The row of . Its count on (the plate
/// shown beside the list; null omits it) prefixes the quantity when there is one. With
/// the row has no quantity, highlight or bar.
///
public static DrawingListRow For(Drawing drawing, Plate plate, Units units, bool hideQuantity = false)
{
ArgumentNullException.ThrowIfNull(drawing);
var quantity = drawing.Quantity;
string quantityText = null;
var bar = DrawingQuantityBar.None;
if (!hideQuantity)
{
quantityText = $"{quantity.Nested} of {quantity.Required} nested";
var onPlate = plate?.Parts.Count(part => part.BaseDrawing == drawing) ?? 0;
if (onPlate > 0)
quantityText = $"[{onPlate}] {quantityText}";
if (quantity.Required > 0)
bar = quantity.Nested >= quantity.Required ? DrawingQuantityBar.Complete : DrawingQuantityBar.Short;
}
return new DrawingListRow(
drawing,
drawing.Name,
quantityText,
drawing.Program.BoundingBox().Size.ToString(4),
$"{System.Math.Round(drawing.Area, 4)} sq/{UnitsHelper.GetShortString(units)}",
!hideQuantity && quantity.Nested > 0 && quantity.Remaining == 0,
bar);
}
/// The rows of , in their order.
public static IReadOnlyList For(IEnumerable drawings, Plate plate, Units units)
{
ArgumentNullException.ThrowIfNull(drawings);
return drawings.Select(drawing => For(drawing, plate, units)).ToList();
}
///
/// The note the list shows in place of rows: when Hide Fully
/// Nested Drawings () emptied a list of a nest that has
/// drawings, so they don't look lost; otherwise null. The filter stays the user's choice.
///
public static string EmptyText(bool hideFullyNested, int drawingCount) =>
hideFullyNested && drawingCount > 0 ? AllHiddenText : null;
}