using GadgetCore.API;
using HarmonyLib;
using UnityEngine;

namespace StorageExpansion.Patches
{

  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("Update")]
  [HarmonyGadget("StorageExpansion")]
  public static class Patch_GameScript_Update
  {
    [HarmonyPrefix]
    public static void Prefix(GameScript __instance, ref int ___storageLevel)
    {
      if (MenuScript.player)
      {
        if (!GameScript.pausing)
        {
          if (Input.GetMouseButtonDown(0) && GameScript.inventoryOpen)
          {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 7f))
            {
              if (hit.transform.gameObject.layer != 16 && hit.transform.gameObject.layer != 17)
              {
                string name = hit.transform.gameObject.name;
                if (name.StartsWith("page"))
                {
                  var slot = int.Parse(hit.transform.gameObject.name.Substring("page".Length));
                  __instance.SelectPage(slot);
                }
              }
            }
          }
        }
      }
    }
  }
}