using GadgetCore.API;
using HarmonyLib;
using UnityEngine;

namespace StorageExpansion.Patches
{

  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("UpgradeStorage")]
  [HarmonyGadget("StorageExpansion")]
  public static class Patch_GameScript_UpgradeStorage
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, ref int ___storageLevel)
    {
			if (___storageLevel < __instance.storageButton.Length)
			{
				int storageUpgradeCost = __instance.GetStorageUpgradeCost(___storageLevel);
				if (__instance.GetCredits() >= storageUpgradeCost)
				{
					__instance.RemoveCredits(storageUpgradeCost);
          ___storageLevel++;
					PreviewLabs.PlayerPrefs.SetInt("storageLevel", ___storageLevel);
					__instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/chesthit2"), Menuu.soundLevel / 10f);
					Camera.main.GetComponent<Animation>().Play("craft");
          for (int i = 0; i < __instance.storageButton.Length; i++)
          {
            if (i <= ___storageLevel)
            {
              __instance.storageButton[i].GetComponent<Renderer>().enabled = false;
            }
            else
            {
              __instance.storageButton[i].GetComponent<Renderer>().enabled = true;
            }
          }
          int newStorageUpgradeCost = __instance.GetStorageUpgradeCost(___storageLevel);
          if (___storageLevel == __instance.storageButton.Length - 1)
          {
            __instance.txtStorageCost[0].text = "Max Lvl";
            __instance.txtStorageCost[1].text = "Max Lvl";
          }
          else
          {
            __instance.txtStorageCost[0].text = string.Empty + newStorageUpgradeCost;
            __instance.txtStorageCost[1].text = string.Empty + newStorageUpgradeCost;
          }
        }
				else
				{
					__instance.Error(3);
				}
			}
			return false;
    }
  }
}