using GadgetCore.API;
using GadgetCore.Util;
using HarmonyLib;
using System;
using System.Reflection;

namespace ItemLevelExtension.Patches
{

  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("GetItemLevel")]
  [HarmonyGadget("ItemLevelExtension")]
  public static class Patch_GameScript_GetItemLevel
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, int exp, ref int __result, ref int ___pastCap)
		{
			int level = 1;
			int nextLevelPrice = 100;
			int i = 0;
			while (exp > nextLevelPrice)
			{
				___pastCap = nextLevelPrice;
				exp -= nextLevelPrice;
				i++;
				nextLevelPrice += 75 + i * 100;
				level++;
				if (level == Core.settingMaxItemLevel)
				{
					break;
				}
			}

			if (level > Core.settingMaxItemLevel)
				level = Core.settingMaxItemLevel;

			__result = level;
      return false;
    }
  }
}