using System;
using System.Collections.Generic;
using System.Linq;
using RynthCore.Plugin.RynthDefective.Items;
namespace RynthCore.Plugin.RynthDefective.Model;
/// One row of the weapon picker.
internal sealed record PickerRow(CarriedItem Item, WeaponGroup Group, string Element, bool InHand, bool Favourite);
/// A picker group: a kind of weapon and the rows under it.
internal sealed record PickerGroup(WeaponGroup Group, List Rows);
/// What the picker shows: favourites and recent weapons first, then every kind.
internal sealed class PickerModel
{
public List Favourites = new();
public List Recent = new();
public List Groups = new();
/// Weapons carried (before the search and kind filter).
public int Total;
/// Rows that passed the search and the kind filter.
public int Shown;
}
///
/// The weapon picker's list, pure: the carried weapons and wands grouped by kind (melee, missile,
/// war magic, void magic, then shields and ammunition), narrowed by the search words and the kind
/// filter. Favourites and the recently wielded come first, as shortcuts: they are also listed under
/// their kind, so a weapon is always where its kind says. Inside a group, A-Z by full name.
///
internal static class WeaponPicker
{
public static PickerModel Build(IReadOnlyList items, IReadOnlyList favourites,
IReadOnlyList recent, string search, KindFilter filter)
{
var model = new PickerModel();
string[] words = (search ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries);
var rows = new List();
foreach (CarriedItem it in items)
{
ItemKind kind = ItemKinds.Classify(it);
if (ItemKinds.GroupOf(kind) is not { } group) continue;
model.Total++;
if (!PassesFilter(group, filter)) continue;
if (!MatchesSearch(it, group, words)) continue;
rows.Add(new PickerRow(it, group, ItemKinds.Element(it), it.Wielded, IsFavourite(it, favourites)));
}
model.Shown = rows.Count;
rows.Sort(CompareRows);
foreach (WeaponGroup g in Enum.GetValues())
{
var inGroup = rows.Where(r => r.Group == g).ToList();
if (inGroup.Count > 0) model.Groups.Add(new PickerGroup(g, inGroup));
}
// Shortcuts keep their own order: favourites as the player listed them, recent newest first.
foreach (ItemRef f in favourites)
AddFirstMatch(model.Favourites, rows, f);
foreach (ItemRef r in recent)
AddFirstMatch(model.Recent, rows, r);
return model;
}
private static void AddFirstMatch(List into, List rows, ItemRef r)
{
PickerRow? best = null;
foreach (PickerRow row in rows)
{
if (!r.Matches(row.Item)) continue;
if (row.Item.Id == r.Id) { best = row; break; }
best ??= row;
}
if (best != null && !into.Contains(best)) into.Add(best);
}
public static bool IsFavourite(CarriedItem it, IReadOnlyList favourites)
{
foreach (ItemRef f in favourites)
if (f.Id == it.Id || f.Matches(it)) return true;
return false;
}
private static bool PassesFilter(WeaponGroup g, KindFilter f) => f switch
{
KindFilter.Melee => g == WeaponGroup.Melee,
KindFilter.Missile => g == WeaponGroup.Missile || g == WeaponGroup.Ammo,
KindFilter.War => g == WeaponGroup.WarMagic,
KindFilter.Void => g == WeaponGroup.VoidMagic,
_ => true,
};
/// Every search word is in the weapon's full name, its element or its kind's label.
private static bool MatchesSearch(CarriedItem it, WeaponGroup group, string[] words)
{
if (words.Length == 0) return true;
string full = it.FullName;
string element = ItemKinds.Element(it);
string label = ItemKinds.Label(group);
foreach (string w in words)
{
if (full.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0) continue;
if (element.Length > 0 && element.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0) continue;
if (label.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0) continue;
return false;
}
return true;
}
private static int CompareRows(PickerRow a, PickerRow b)
{
int c = string.Compare(a.Item.FullName, b.Item.FullName, StringComparison.OrdinalIgnoreCase);
return c != 0 ? c : a.Item.Id.CompareTo(b.Item.Id);
}
public static string KindLabel(KindFilter f) => f switch
{
KindFilter.Melee => "Melee",
KindFilter.Missile => "Missile",
KindFilter.War => "War",
KindFilter.Void => "Void",
_ => "All",
};
}