using GadgetCore.API;
using HarmonyLib;
using System.Collections;
using System.Reflection;
using UnityEngine;

namespace StorageExpansion.Patches
{

  [HarmonyPatch]
  [HarmonyGadget("StorageExpansion")]
  public static class RePatch_GameScript_RefreshStoragePage
  {
    [HarmonyTargetMethod]
    static MethodBase TargetMethod()
    {
      return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshStoragePage"), "RefreshStoragePage");
    }

    public static readonly FieldInfo curStoragePage = typeof(GameScript).GetField("curStoragePage", BindingFlags.NonPublic | BindingFlags.Instance);

    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, int p, ref IEnumerator __result)
    {
      __result = RefreshStoragePage(__instance, p);
      return false;
    }

    private static IEnumerator RefreshStoragePage(GameScript instance, int p)
    {
      var __instance = InstanceTracker.GameScript;
      curStoragePage.SetValue(__instance, p);
      __instance.storageSelect.transform.position = new Vector3(__instance.storageButton[p].transform.position.x, __instance.storageButton[p].transform.position.y, __instance.storageSelect.transform.position.z);
      for (int i = 0; i < Core.settingAmountSlots; i++)
      {
        __instance.RefreshSlotStorage(i);
      }
      yield break;
    }
  }
}