using System.Collections.Generic;
using EFT.SynchronizableObjects;
using EFT.UI;
using stupid.solutions;
using stupid.solutions.Utils;
using UnityEngine;

public class GrenadeESP : MonoBehaviour
{
	public Color grenadeColor = Color.red;

	private List<Throwable> grenades = new List<Throwable>();

	private HashSet<int> processedGrenades = new HashSet<int>();

	private HashSet<int> processedTripwires = new HashSet<int>();

	private Dictionary<Renderer, Shader> originalGrenadeShaders = new Dictionary<Renderer, Shader>();

	public static bool IsGrenadeValid(Throwable grenade)
	{
		return grenade != null;
	}

	public void Update()
	{
		if (Main.GameWorld == null || Main.GameWorld.Grenades == null || !Settings.grenadeesp)
		{
			return;
		}
		grenades.Clear();
		for (int i = 0; i < Main.GameWorld.Grenades.Count; i++)
		{
			Throwable byIndex = Main.GameWorld.Grenades.GetByIndex(i);
			if (IsGrenadeValid(byIndex))
			{
				grenades.Add(byIndex);
			}
		}
		if ((Main.GameWorld == null && processedTripwires != null) || processedGrenades != null)
		{
			processedTripwires.Clear();
			processedGrenades.Clear();
		}
	}

	private void OnGUI()
	{
		if (Settings.grenadeesp && Main.GameWorld != null)
		{
			foreach (Throwable grenade in grenades)
			{
				float num = Vector3.Distance(Main.MainCamera.transform.position, grenade.transform.position);
				if (!(num > Settings.DrawPlayersDistance))
				{
					Vector3 vector = Camera.main.WorldToScreenPoint(grenade.transform.position);
					if (vector.z > 0f)
					{
						vector.y = (float)Screen.height - vector.y;
						Color color = ((num <= 10f) ? Color.red : new Color(1f, 0.65f, 0f));
						Render.DrawString(new Vector2(vector.x, vector.y + 15f), "! Grenade !", color);
						Render.DrawString(new Vector2(vector.x, vector.y + 30f), $"{num:F1}m", color);
					}
				}
			}
		}
		if (Main.GameWorld != null && Main.ActiveCamera != null)
		{
			ApplyGrenadeChams();
		}
		if (!Settings.tripwire || !(Main.GameWorld != null))
		{
			return;
		}
		foreach (SynchronizableObject synchronizableObject in Main.GameWorld.SynchronizableObjectLogicProcessor.GetSynchronizableObjects())
		{
			if (Settings.Chams)
			{
				ApplyTripwireChams(synchronizableObject);
			}
			if (synchronizableObject.Type != SynchronizableObjectType.Tripwire)
			{
				continue;
			}
			float num2 = Vector3.Distance(Main.MainCamera.transform.position, ((Component)(object)synchronizableObject).transform.position);
			if (!(num2 > Settings.DrawPlayersDistance))
			{
				Vector3 vector2 = Camera.main.WorldToScreenPoint(((Component)(object)synchronizableObject).transform.position);
				if (vector2.z > 0f)
				{
					vector2.y = (float)Screen.height - vector2.y;
					Color color2 = ((num2 <= 5f) ? Color.red : new Color(1f, 0.65f, 0f));
					Render.DrawString(new Vector2(vector2.x, vector2.y + 15f), " ! Trip Wire !", color2);
					Render.DrawString(new Vector2(vector2.x, vector2.y + 30f), $"{num2:F1}m", color2);
				}
			}
		}
	}

	private void ApplyGrenadeChams()
	{
		if (!Settings.Chams || !Settings.grenadeesp)
		{
			return;
		}
		foreach (Throwable grenade in grenades)
		{
			if (!processedGrenades.Contains(grenade.GetInstanceID()))
			{
				ApplyChams(grenade.gameObject, processedGrenades, Settings.GrenadeVisible, Settings.GrenadeHidden);
			}
		}
	}

	private void ApplyTripwireChams(SynchronizableObject tripwire)
	{
		if (Settings.Chams && Settings.tripwire && !processedTripwires.Contains(((Object)(object)tripwire).GetInstanceID()))
		{
			ApplyChams(((Component)(object)tripwire).gameObject, processedTripwires, Settings.TripwireVisible, Settings.TripwireVisible);
		}
	}

	private void ApplyChams(GameObject targetObject, HashSet<int> processedEntities, Color visible, Color hidden)
	{
		Renderer[] componentsInChildren = targetObject.GetComponentsInChildren<Renderer>();
		foreach (Renderer renderer in componentsInChildren)
		{
			if (!(renderer == null) && !(renderer.material == null))
			{
				if (!originalGrenadeShaders.ContainsKey(renderer))
				{
					originalGrenadeShaders[renderer] = renderer.material.shader;
				}
				Shader shader = Main.bundle.LoadAsset<Shader>("Chams.shader");
				if (shader == null)
				{
					ConsoleScreen.Log("Failed to load Chams.shader from AssetBundle!");
					continue;
				}
				renderer.material.shader = shader;
				renderer.material.SetColor("_ColorVisible", visible);
				renderer.material.SetColor("_ColorBehind", hidden);
				renderer.allowOcclusionWhenDynamic = false;
				renderer.forceRenderingOff = false;
				renderer.enabled = true;
			}
		}
		processedEntities.Add(targetObject.GetInstanceID());
	}
}
