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

namespace Subworlds.Patches
{

  [HarmonyPatch(typeof(Chunk))]
  [HarmonyPatch("SpawnBiomeSlot")]
  [HarmonyGadget("SubworldCore")]
  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, 100);
      if (num < 3 && !Core.shipPorterPlaced)
      {
        Core.shipPorterPlaced = true;
        if (a == 0 || a == 1 || a == 2 || a == 3 || a == 4 || a == 5)
        {
          var portal = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), transform.position + new Vector3(-0.5f, 1.5f - 0.05f, 0.2f), Quaternion.identity, 0);
          portal.name = "portal/5/obj";
          var iUA = portal.transform.GetChild(0).gameObject;
          portal.GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
          iUA.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.shipPlanet.GetID(), 0, 5 });
          ___networkStuff[___temp] = portal;
          ___temp++;
          var crates = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenCrates1"), transform.position + new Vector3(1.5f, 0, 0.2f), Quaternion.identity, 0);
          ___networkStuff[___temp] = crates;
          ___temp++;
          return false;
        }
      }
      if (num < 4 && Core.shipPorterPlaced && !Core.meteorPorterPlaced)
      {
        Core.meteorPorterPlaced = true;
        if (a == 0 || a == 1 || a == 2 || a == 3 || a == 4 || a == 5)
        {
          var portal = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), transform.position + new Vector3(0.5f, 1.5f - 0.05f, 0.2f), Quaternion.identity, 0);
          portal.name = "portal/6/obj";
          var iUA = portal.transform.GetChild(0).gameObject;
          portal.GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
          iUA.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.meteroidPlanet.GetID(), 0, 6 });
          ___networkStuff[___temp] = portal;
          ___temp++;
          var crates = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenMeteor"), transform.position + new Vector3(-1.5f, 0 + 2.03f, 0.2f), Quaternion.identity, 0);
          ___networkStuff[___temp] = crates;
          ___temp++;
          return false;
        }
      }
      return true;
    }
  }
}