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

namespace ItemLevelExtension.Patches
{

  [HarmonyPatch(typeof(GadgetCoreAPI))]
  [HarmonyPatch("GetGearLevel")]
  [HarmonyGadget("ItemLevelExtension")]
  public static class Patch_GameScript_GetGearLevel
	{
    [HarmonyPrefix]
    public static bool Prefix(Item item, ref int __result)
		{
			int exp = item.exp;
			int level = 1;
			int nextLevelPrice = 100;
			int i = 0;
			while (exp > 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;
    }
  }
}