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

namespace ItemLevelExtension.Patches
{

  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("GetItemLevel2")]
  [HarmonyGadget("ItemLevelExtension")]
  public static class Patch_GameScript_GetItemLevel2
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, int exp, ref float[] __result)
    {
      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;

      float progress;
      if (level == Core.settingMaxItemLevel)
        progress = 1.75f;
      else
        progress = ((float)exp) / nextLevelPrice * 1.75f;

      __result = new float[] { level, progress };
      return false;
    }
  }
}