using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.Data;
namespace OpenNest.Forms
{
///
/// One entry of the machine list: is the machine to assign when it is
/// chosen, or null for an entry that only informs.
///
public sealed record MachineMenuItem(string Text, MachineConfig Machine, bool Checked, bool Enabled);
///
/// The machines listed by Nest > Machine and the status bar's machine label: those on
/// file by name, the active nest's checked; one assigned but no longer on file is shown
/// checked and disabled so the nest still says what it was made for.
///
public static class MachineMenu
{
public const string NoNestText = "(no nest open)";
public const string NoMachinesText = "(no machines; see Tools > Machine Configuration)";
/// The entries for (null when no nest is open).
public static IReadOnlyList Items(Nest nest, IEnumerable machines)
{
if (nest == null)
return new[] { new MachineMenuItem(NoNestText, null, false, false) };
var sorted = (machines ?? Enumerable.Empty())
.OrderBy(m => m.Name, StringComparer.CurrentCultureIgnoreCase)
.ToList();
var items = sorted
.Select(m => new MachineMenuItem(m.Name, m, m.Id == nest.Machine?.Id, true))
.ToList();
if (nest.Machine is { } assigned && sorted.All(m => m.Id != assigned.Id))
items.Add(new MachineMenuItem($"{assigned.Name} (not on file)", null, true, false));
if (items.Count == 0)
items.Add(new MachineMenuItem(NoMachinesText, null, false, false));
return items;
}
/// Whether is already the nest's, so choosing it changes nothing.
public static bool IsAssigned(Nest nest, MachineConfig machine)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(machine);
return nest.Machine?.Id == machine.Id;
}
}
}