using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
///
/// Legal Move information for a single , for indicating if a move is legal or not.
///
public sealed class LegalMoveSource
{
public readonly bool[] IsMoveBoxOrdered = new bool[4];
/// Creates a shallow copy of the array reference for use in binding.
public IReadOnlyList DataSource => (ComboItem[])MoveDataAllowed.Clone();
///
/// Checks if the requested is legally able to be learned.
///
/// Move to check if can be learned
/// True if can learn the move
public bool CanLearn(int move) => AllowedMoves.Contains(move);
private readonly HashSet AllowedMoves = new();
private ComboItem[] MoveDataAllowed = Array.Empty();
///
/// Reloads the legality sources to permit the provided legal .
///
/// Legal moves to allow
public void ReloadMoves(IReadOnlyList moves)
{
// check prior move-pool to not needlessly refresh the data set
if (AllowedMoves.Count == moves.Count && AllowedMoves.SetEquals(moves))
return;
AllowedMoves.Clear();
foreach (var m in moves)
AllowedMoves.Add(m);
Array.Sort(MoveDataAllowed, Compare);
// MoveDataAllowed = MoveDataAllowed.OrderByDescending(m => AllowedMoves.Contains(m.Value)).ToArray();
// defer re-population until dropdown is opened; handled by dropdown event
Array.Clear(IsMoveBoxOrdered, 0, IsMoveBoxOrdered.Length);
}
private int Compare(ComboItem i1, ComboItem i2)
{
// split into 2 groups: Allowed & Not, and sort each sublist
var (strA, value1) = i1;
var (strB, value2) = i2;
var c1 = AllowedMoves.Contains(value1);
var c2 = AllowedMoves.Contains(value2);
if (c1)
return c2 ? string.CompareOrdinal(strA, strB) : -1;
return c2 ? 1 : string.CompareOrdinal(strA, strB);
}
public void ReloadMoves(IReadOnlyList moves)
{
MoveDataAllowed = moves.ToArray();
}
}
}