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

namespace RecipeMenuCore.Patches
{
  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("RefreshRecipe")]
  [HarmonyGadget("RecipeMenuCore")]
  public static class Patch_GameScript_RefreshRecipe
  {
    [HarmonyPrefix]
    public static bool Prefix(GameScript __instance, int ___craftType, int ___curRecipePage)
    {
      if (___craftType == 0)
      {
        __instance.txtRecipeName[0].text = string.Empty + __instance.GetRecipeName0(___curRecipePage);
        __instance.txtRecipeUnlocked[0].text = "Page " + (___curRecipePage + 1) + "/" + (6 + Core.pageGearForgeInfoList.Count);
      }
      else if (___craftType == 1)
      {
        __instance.txtRecipeName[0].text = string.Empty + __instance.GetRecipeName1(___curRecipePage);
        __instance.txtRecipeUnlocked[0].text = "Page " + (___curRecipePage + 1) + "/" + (2 + Core.pageAlchemyStationInfoList.Count);
      }
      else if (___craftType == 2)
      {
        __instance.txtRecipeName[0].text = string.Empty + __instance.GetRecipeName2(___curRecipePage);
        __instance.txtRecipeUnlocked[0].text = "Page " + (___curRecipePage + 1) + "/" + (6 + Core.pageUltimateForgeInfoList.Count);
      }
      else if (___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
      {
        __instance.txtRecipeName[0].text = string.Empty + __instance.GetRecipeName2(___curRecipePage);
        __instance.txtRecipeUnlocked[0].text = "Page " + (___curRecipePage + 1) + "/" + (Core.pageUniversalCrafterInfoList.Count);
      }
      __instance.txtRecipeUnlocked[0].gameObject.GetComponent<Animation>().Play();
      __instance.txtRecipeName[0].gameObject.GetComponent<Animation>().Play();
      __instance.txtRecipeName[1].text = __instance.txtRecipeName[0].text;
      __instance.txtRecipeUnlocked[1].text = __instance.txtRecipeUnlocked[0].text;
      if ((___craftType != 0 || (___curRecipePage <= 5 && Core.pageGearForgeInfoListVanilla[___curRecipePage] == null))
        && (___craftType != 1 || (___curRecipePage <= 1 && Core.pageAlchemyStationInfoListVanilla[___curRecipePage] == null))
        && (___craftType != 2 || (___curRecipePage <= 5 && Core.pageUltimateForgeInfoListVanilla[___curRecipePage] == null))
        && (___craftType != MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID()))
      {
        __instance.menuRecipe.GetComponent<Renderer>().material = (Material)Resources.Load(string.Concat(new object[] { "mat/r", ___curRecipePage, "t", ___craftType }));
      }
      else
      {
        if (___craftType == 0)
        {
          var page = ___curRecipePage >= 6 ? Core.pageGearForgeInfoList[___curRecipePage - 6] : Core.pageGearForgeInfoListVanilla[___curRecipePage];
          __instance.txtRecipeName[0].text = page.Title;
          __instance.txtRecipeName[1].text = __instance.txtRecipeName[0].text;
          __instance.menuRecipe.GetComponent<Renderer>().material = page.Mat;
        }
        else if (___craftType == 1)
        {
          var page = ___curRecipePage >= 2 ? Core.pageAlchemyStationInfoList[___curRecipePage - 2] : Core.pageAlchemyStationInfoListVanilla[___curRecipePage];
          __instance.txtRecipeName[0].text = page.Title;
          __instance.txtRecipeName[1].text = __instance.txtRecipeName[0].text;
          __instance.menuRecipe.GetComponent<Renderer>().material = page.Mat;
        }
        else if (___craftType == 2)
        {
          var page = ___curRecipePage >= 6 ? Core.pageUltimateForgeInfoList[___curRecipePage - 6] : Core.pageUltimateForgeInfoListVanilla[___curRecipePage];
          __instance.txtRecipeName[0].text = page.Title;
          __instance.txtRecipeName[1].text = __instance.txtRecipeName[0].text;
          __instance.menuRecipe.GetComponent<Renderer>().material = page.Mat;
        }
        else if (___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
        {
          var page = Core.pageUniversalCrafterInfoList[___curRecipePage];
          __instance.txtRecipeName[0].text = page.Title;
          __instance.txtRecipeName[1].text = __instance.txtRecipeName[0].text;
          __instance.menuRecipe.GetComponent<Renderer>().material = page.Mat;
        }
      }
      __instance.RefreshRecipeUnlock();
      return false;
    }


    [HarmonyPostfix]
    [HarmonyAfter("URP.URP.gadget")]
    public static void Postfix(GameScript __instance, int ___curRecipePage, int ___craftType)
    {
      if (___curRecipePage == 1 && ___craftType == 1 && Core.pageAlchemyStationInfoListVanilla[1] != null)
      {
        var page = Core.pageAlchemyStationInfoListVanilla[1];
        __instance.menuRecipe.GetComponent<Renderer>().material = page.Mat;
      }
    }
  }
}
