using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using Verse;
namespace BlaxCastes
{
///
/// The weapon loadout slot's link to [1001] Weapons Tab Reborn (packageId papercrane1001.WeaponsTab).
/// Bound by reflection to the static API it publishes for Better Pawn Control, in
/// WeaponsTabReborn.LoadoutDictionary (read from its 1.6 assembly, which 1.7 loads):
/// - int BPC_GetLoadoutId(Pawn): the pawn's loadout id; a pawn without one gets the default loadout.
/// - void BPC_SetLoadoutId(Pawn, int): an id that does not exist clears the pawn's loadout, so Castes
/// checks the id first.
/// - int BPC_GetDefaultLoadoutId(): the first loadout.
/// - string BPC_GetLoadoutNamebyId(int): throws InvalidOperationException for an id that does not exist.
/// - Dictionary<string, int> BPC_GetWeaponsLoadoutsDatabase(): label to id. It only ever grows
/// (deleted and renamed loadouts stay in it, and a new loadout whose label is already a key is left
/// out), so every id in it is checked by name, and the ids just above the highest one are probed.
/// Loadout ids are the highest id plus one, so an id freed by deleting the newest loadout can come back.
/// A missing type or method turns the slot off for the session; a call that throws anything else turns
/// it off for the rest of the game. Either logs one warning and never throws.
/// The loadout database is null after loading a save that has none (the mod added mid-save, or saved
/// without it), until the mod's own tab or job giver creates it through GetCurrentLoadoutDatabase(). The
/// BPC methods read the field directly and would throw, so every call goes through that public method
/// first, which creates the database the same way the mod does.
///
public static class WeaponsTab
{
public const string ApiTypeName = "WeaponsTabReborn.LoadoutDictionary";
/// How many ids above the highest known one Loadouts() checks.
private const int ProbeAhead = 16;
private static bool? modActive;
private static bool bound;
private static bool bindFailed;
private static bool callFailed;
private static Func getLoadoutId;
private static Action setLoadoutId;
private static Func getDefaultLoadoutId;
private static Func getLoadoutName;
private static Func> getDatabase;
/// LoadoutDictionary.GetCurrentLoadoutDatabase(): returns the database, creating it if it is null. Optional.
private static Func