using GadgetCore.API;
using GadgetCore.Util;
using HarmonyLib;
using System.Collections;
using System.Threading;
using System.Windows.Threading;
using UnityEngine;

namespace MonsterNests.Patches
{

  [HarmonyPatch(typeof(Chunk))]
  [HarmonyPatch("SpawnBiomeSlot")]
  [HarmonyGadget("MonsterNests")]
  public static class Patch_Chunk_SpawnBiomeSlot
  {
    [HarmonyPostfix]
    public static bool Prefix(Chunk __instance, int a, int i, int mid, ref GameObject[] ___spawnSpot, ref GameObject[] ___spawnSpotMid, ref GameObject[] ___networkStuff, ref int ___temp)
    {
      Transform transform;
      if (mid == 1)
        transform = ___spawnSpotMid[i].transform;
      else
        transform = ___spawnSpot[i].transform;

      int num = UnityEngine.Random.Range(0, 1000);
      if (num < 5)
      {
        if (a == 0)
        {
          ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectBugspotBig.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 1)
        {
          if (GameScript.challengeLevel <= 0)
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectSpikePlant.Object, transform.position, Quaternion.identity, 0);
          else
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectSpikePlantCM.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 2)
        {
          if (GameScript.challengeLevel <= 0)
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectSpiderEgg.Object, transform.position, Quaternion.identity, 0);
          else
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectSpiderEggCM.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 3)
        {
          ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectGoldenShroom.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 4)
        {
          if (GameScript.challengeLevel <= 0)
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectAncientCrystal.Object, transform.position, Quaternion.identity, 0);
          else
            ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectAncientCrystalCM.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 5)
        {
          ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectPlagueNest.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
        else if (a == 6)
        {
          ___networkStuff[___temp] = (GameObject)Network.Instantiate(Core.objectFrozenWisp.Object, transform.position, Quaternion.identity, 0);
          ___temp++;
          return false;
        }
      }
      return true;
    }
  }
}