using Aki.Reflection.Patching;
using BepInEx;
using EFT.UI;
using System.Linq;
using System.Reflection;

namespace IcyClawz.CustomInteractions;

[BepInPlugin("com.IcyClawz.CustomInteractions", "IcyClawz.CustomInteractions", "1.4.0")]
public class Plugin : BaseUnityPlugin
{
    private void Awake()
    {
        new ItemUiContextPatch().Enable();
        new InteractionButtonsContainerPatch().Enable();
    }
}

internal class ItemUiContextPatch : ModulePatch
{
    protected override MethodBase GetTargetMethod() =>
        typeof(ItemUiContext).GetMethod("GetItemContextInteractions", BindingFlags.Public | BindingFlags.Instance);

    [PatchPostfix]
    private static void Postfix(ref ItemInfoInteractionsAbstractClass<EFT.InventoryLogic.EItemInfoButton> __result,
        ref ItemUiContext __instance, ItemContextClass itemContext)
    {
        foreach (var provider in CustomInteractionsManager.Providers.OfType<IItemCustomInteractionsProvider>())
        {
            var interactions = provider.GetCustomInteractions(__instance, itemContext.ViewType, itemContext.Item);
            if (interactions is null)
                continue;
            foreach (CustomInteraction interaction in interactions)
                __result.AddCustomInteraction(interaction);
        }
    }
}

internal class InteractionButtonsContainerPatch : ModulePatch
{
    protected override MethodBase GetTargetMethod() =>
        typeof(InteractionButtonsContainer).GetMethod("method_3", BindingFlags.Public | BindingFlags.Instance);

    [PatchPrefix]
    private static bool Prefix(ref InteractionButtonsContainer __instance, DynamicInteractionClass interaction)
    {
        if (interaction is CustomInteractionImpl impl)
        {
            __instance.AddCustomButton(impl);
            return false;
        }
        return true;
    }
}