using HarmonyLib;
using GadgetCore.API;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;

namespace RecipeMenuCore.Patches
{
  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("RefreshRecipeUnlock")]
  [HarmonyGadget("RecipeMenuCore")]
  public static class Patch_GameScript_RefreshRecipeUnlock
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, int ___craftType, int ___curRecipePage, GameObject ___ultLocksObj, GameObject ___recipeButtons,
      GameObject[] ___recipeLock, GameObject[] ___ultLocks, int[,] ___ultLocksUnlocked)
    {
      if (___craftType == 0 || ___craftType == 1 || ___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
      {
        for (int i = 0; i < 12; i++)
        {
          ___recipeButtons.transform.GetChild(i).gameObject.SetActive(true);
        }
      }

      var hoverElements = ___ultLocksObj.transform.parent.Find("hoverElements").gameObject;
      hoverElements.SetActive(false);

      if (___craftType == 2 && ___curRecipePage < 6 && Core.pageUltimateForgeInfoListVanilla[___curRecipePage] == null)
      {
        hoverElements.SetActive(true);
        for (int i = 0; i < 36; i++)
        {
          if (___ultLocksUnlocked[___curRecipePage, i] == 0)
          {
            hoverElements.transform.GetChild(i).gameObject.SetActive(false);
          }
          else
          {
            hoverElements.transform.GetChild(i).gameObject.SetActive(true);
          }
        }
      }

      if (___craftType == 0 && (___curRecipePage >= 6 || Core.pageGearForgeInfoListVanilla[___curRecipePage] != null))
      {
        ___ultLocksObj.SetActive(false);
        ___recipeButtons.SetActive(true);
        var page = ___curRecipePage >= 6 ? Core.pageGearForgeInfoList[___curRecipePage - 6] : Core.pageGearForgeInfoListVanilla[___curRecipePage];
        for (int i = 0; i < 12; i++)
        {
          if (page.GetRecipePageEntries().Length > i)
          {
            int itemID = page.GetRecipePageEntries()[i].ItemIdBase;
            if (__instance.RecipeCraftedAlready(itemID, page.GetRecipePageEntries()[i].MinAmount))
              ___recipeLock[i].SetActive(false);
            else
              ___recipeLock[i].SetActive(true);
          }
          else
          {
            ___recipeLock[i].SetActive(false);
            ___recipeButtons.transform.GetChild(i).gameObject.SetActive(false);
          }
        }
        return false;
      }
      else if (___craftType == 1 && (___curRecipePage >=2  || Core.pageAlchemyStationInfoListVanilla[___curRecipePage] != null))
      {
        ___ultLocksObj.SetActive(false);
        ___recipeButtons.SetActive(true);
        var page = ___curRecipePage >= 6 ? Core.pageAlchemyStationInfoList[___curRecipePage - 2] : Core.pageAlchemyStationInfoListVanilla[___curRecipePage];
        for (int i = 0; i < 12; i++)
        {
          if (page.GetRecipePageEntries().Length > i)
          {
            int itemID = page.GetRecipePageEntries()[i].ItemIdBase;
            if (__instance.RecipeCraftedAlready(itemID, page.GetRecipePageEntries()[i].MinAmount))
              ___recipeLock[i].SetActive(false);
            else
              ___recipeLock[i].SetActive(true);
          }
          else
          {
            ___recipeLock[i].SetActive(false);
            ___recipeButtons.transform.GetChild(i).gameObject.SetActive(false);
          }
        }
        return false;
      }
      else if (___craftType == 2 && (___curRecipePage >= 6 || Core.pageUltimateForgeInfoListVanilla[___curRecipePage] != null))
      {
        hoverElements.SetActive(true);
        ___ultLocksObj.SetActive(true);
        ___recipeButtons.SetActive(false);
        var page = ___curRecipePage >= 6 ? Core.pageUltimateForgeInfoList[___curRecipePage - 6] : Core.pageUltimateForgeInfoListVanilla[___curRecipePage];
        for (int i = 0; i < 36; i++)
        {
          if (i < 12)
          {
            ___recipeLock[i].SetActive(false);
          }
          if (page.GetRecipePageEntries().Length > i / 3)
          {
            int itemID = page.GetRecipePageEntries()[i / 3].ItemIdExtension[i % 3];
            if (__instance.RecipeCraftedAlready(itemID, 1))
            {
              ___ultLocks[i].SetActive(false);
              hoverElements.transform.GetChild(i).gameObject.SetActive(true);
            }
            else
            {
              ___ultLocks[i].SetActive(true);
              hoverElements.transform.GetChild(i).gameObject.SetActive(false);
            }
          }
          else
          {
            ___ultLocks[i].SetActive(false);
            hoverElements.transform.GetChild(i).gameObject.SetActive(false);
          }
        }
        return false;
      }
      else if (___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
      {
        ___ultLocksObj.SetActive(false);
        ___recipeButtons.SetActive(true);
        var page = Core.pageUniversalCrafterInfoList[___curRecipePage];
        for (int i = 0; i < 12; i++)
        {
          if (page.GetRecipePageEntries().Length > i)
          {
            int itemID = page.GetRecipePageEntries()[i].ItemIdBase;
            if (((CraftMenuInfo)MenuRegistry.Singleton["Gadget Core:Crafter Menu"]).IsRecipeUnlocked(itemID))
              ___recipeLock[i].SetActive(false);
            else
              ___recipeLock[i].SetActive(true);
          }
          else
          {
            ___recipeLock[i].SetActive(false);
            ___recipeButtons.transform.GetChild(i).gameObject.SetActive(false);
          }
        }
        return false;
      }
      return true;
    }
  }
}
