using GadgetCore.API;
using GadgetCore.Util;
using HarmonyLib;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;

namespace StorageExpansion.Patches
{
  internal class SlotsPerPageGetter
  {
    public static int SlotsPerPage { get => Core.settingAmountSlots; }
  }

  [HarmonyPatch()]
  [HarmonyGadget("StorageExpansion")]
  public static class Patch_GameScript_MultiPachSlots
  {
    [HarmonyTargetMethods]
    static IEnumerable<MethodBase> TargetMethods()
    {
      yield return AccessTools.Method(typeof(GameScript), "Update");
      yield return AccessTools.Method(typeof(GameScript), "PlaceItemStorage");
      yield return AccessTools.Method(typeof(GameScript), "PlaceOneItemStorage");
      yield return AccessTools.Method(typeof(GameScript), "SelectItemStorage");
      //yield return AccessTools.Method(typeof(GameScript), "ShiftClickStorage");
      //yield return AccessTools.Method(typeof(GameScript), "ShiftClick");
      yield return AccessTools.Method(typeof(GameScript), "SplitItemStorage");
      yield return AccessTools.Method(typeof(GameScript), "CombineItemStorage");
      yield return AccessTools.Method(typeof(GameScript), "SwapItemStorage");
      //yield return AccessTools.Method(typeof(GameScript), "RefreshSlotStorage");
      yield return AccessTools.Method(typeof(GameScript), "HoverItemStorage");
    
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshSlotStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshSlotStorage"), "Postfix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_ShiftClick"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_HoverItemStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_CombineItemStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_PlaceOneItemStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_SplitItemStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_ShiftClickStorage"), "Prefix");
      yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshStoragePage"), "Prefix");
      //yield return AccessTools.Method("GadgetCore.Patches.Patch_GameScript_RefreshStoragePage:Prefix"); 
    }


    [HarmonyTranspiler]
    public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator gen)
    {
      var p = TranspilerHelper.CreateProcessor(instructions, gen);
      var ilRefs = p.FindAllRefsByInsn(new CodeInstruction(OpCodes.Ldc_I4_S, 30));
      MethodInfo getValue = typeof(SlotsPerPageGetter).GetProperty("SlotsPerPage").GetGetMethod();
      foreach (var ilRef in ilRefs)
      {
        p.InjectInsn(ilRef, new CodeInstruction(OpCodes.Call, getValue), false);
      }
      return p.Insns;
    }
  }
}