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(ObjectScript))]
  [HarmonyPatch("Die")]
  [HarmonyGadget("MonsterNests")]
  public static class Patch_ObjectScript_Die
  {
    [HarmonyPostfix]
    public static void Prefix(ObjectScript __instance, ref int ___id)
    {
      if (___id == Core.objectBugspotBig.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x, __instance.transform.position.y, 10, 6, "e/wasp", 0.1f));
      }
      else if (___id == Core.objectSpikePlant.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x, __instance.transform.position.y, 0, 12, "haz/vinegas", 0.1f));
      }
      else if (___id == Core.objectSpikePlantCM.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x, __instance.transform.position.y, 0, 6, "e/sliver", 0.3f));
      }
      else if (___id == Core.objectSpiderEgg.GetID() || ___id == Core.objectSpiderEggCM.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x, __instance.transform.position.y, 4, 10, "e/rockspider", 0.3f));
      }
      else if (___id == Core.objectGoldenShroom.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          Network.Instantiate(Resources.Load("e/bully"), new Vector3(__instance.transform.position.x, __instance.transform.position.y + 1.1f, 0f), Quaternion.identity, 0);
      }
      else if (___id == Core.objectAncientCrystal.GetID() || ___id == Core.objectAncientCrystalCM.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          Network.Instantiate(Resources.Load("e/golem"), new Vector3(__instance.transform.position.x, __instance.transform.position.y + 1.1f, 0f), Quaternion.identity, 0);
      }
      else if (___id == Core.objectPlagueNest.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          Network.Instantiate(Resources.Load("e/plaguebeast"), new Vector3(__instance.transform.position.x, __instance.transform.position.y + 1.1f, 0f), Quaternion.identity, 0);
      }
      else if (___id == Core.objectFrozenWisp.GetID())
      {
        GameScript.cadetValue += 34;
        __instance.StartCoroutine(Shake());
        if (Network.isServer)
          __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x, __instance.transform.position.y, 5, 6, "e/wisp", 0.35f));
      }
    }

    private static IEnumerator Shake()
    {
      Camera.main.GetComponent<Animation>().Play("shake");
      yield return new WaitForSeconds(1.0f);
      Camera.main.GetComponent<Animation>().Stop();
      Camera.main.transform.eulerAngles = new Vector3(0f, 0f, 0f);
      yield break;
    }

    private static IEnumerator SpawnMultiple(float xBase, float yBase, float range, int amount, string name, float speed)
    {
      for (int i = 0; i < amount; i++)
      {
        int x = 0;
        int y = 0;
        if (range > 0)
        {
          x = Random.Range(-1 * (int)(range * 10), (int)(range * 10));
          y = Random.Range(-1 * (int)(range * 10), (int)(range * 10));
        }
        Network.Instantiate(Resources.Load(name), new Vector3(xBase + x / 10f, yBase + 1.1f + y / 10f, 0f), Quaternion.identity, 0);
        yield return new WaitForSeconds(speed);
      }
      yield break;
    }
  }
}