using System.Collections.Generic;
using System.Linq;
using EFT.Interactive;
using stupid.solutions.Data;
using stupid.solutions.stupid.solutions.Data;
using stupid.solutions.Utils;
using UnityEngine;

namespace stupid.solutions.Features.ESP;

internal class CorpseEsp : MonoBehaviour
{
	private Texture2D stex;

	public static List<GameCorpse> Corpses = new List<GameCorpse>();

	private float _nextBodyCacheTime;

	private static readonly float CacheLootItemsInterval = 10f;

	private int initialYOffset = Settings.initialYOffset;

	private Dictionary<string, int?> lootItemPrices = new Dictionary<string, int?>();

	private static readonly float ItemListCacheInterval = 30f;

	private float _nextItemListCache;

	private void Start()
	{
		stex = new Texture2D(1, 1);
		stex.SetPixel(0, 0, Color.blue);
		stex.Apply();
	}

	public void Update()
	{
		if (!Settings.BodyESP)
		{
			return;
		}
		if (Time.time >= _nextBodyCacheTime && Main.GameWorld != null && Main.GameWorld.ObservedPlayersCorpses != null)
		{
			Corpses.Clear();
			Corpse[] array = Object.FindObjectsOfType<Corpse>();
			foreach (Corpse corpse in array)
			{
				if (GameUtils.IsCorpseValid(corpse) && !(Vector3.Distance(Main.MainCamera.transform.position, corpse.transform.position) > Settings.DrawLootableContainersDistance))
				{
					Corpses.Add(new GameCorpse(corpse));
				}
			}
			_nextBodyCacheTime = Time.time + CacheLootItemsInterval;
		}
		foreach (GameCorpse corpse2 in Corpses)
		{
			corpse2.RecalculateDynamics();
			if (!(Time.time >= _nextItemListCache) || corpse2.Corpse.ItemOwner.RootItem.GetAllItems().Count() < 2)
			{
				continue;
			}
			int num = 1;
			foreach (ContainerItem lootItem in corpse2.LootItems)
			{
				num += lootItem.Count * lootItem.Item.StackObjectsCount;
			}
			if (corpse2.Corpse.ItemOwner.RootItem.GetAllItems().Count() != num)
			{
				corpse2.RefreshItems();
			}
			_nextItemListCache = Time.time + ItemListCacheInterval;
		}
		if (Settings.playerWeapon)
		{
			initialYOffset = Settings.initialYOffset - 20;
		}
		if (!Input.GetKeyDown(KeyCode.Delete) || Settings.allinputdisabled)
		{
			return;
		}
		foreach (GameCorpse corpse3 in Corpses)
		{
			corpse3.RecalculateDynamics();
			corpse3.RefreshItems();
		}
	}

	private void OnGUI()
	{
		if (!Settings.BodyESP || Main.MainCamera == null || Main.GameWorld == null || Main.LocalPlayer == null)
		{
			return;
		}
		int xOffset = Settings.xOffset;
		int num = Settings.initialYOffset;
		int itemLineHeight = Settings.itemLineHeight;
		int lineWidth = Settings.lineWidth;
		int lineX = Settings.lineX;
		GUIStyle style = new GUIStyle(GUI.skin.label)
		{
			alignment = TextAnchor.UpperLeft,
			fontSize = 12,
			font = Main.TXTFONT,
			normal = new GUIStyleState
			{
				textColor = Color.cyan
			}
		};
		GUIStyle gUIStyle = new GUIStyle(GUI.skin.label)
		{
			alignment = TextAnchor.UpperLeft,
			fontSize = 12,
			font = Main.TXTFONT
		};
		foreach (GameCorpse corpse in Corpses)
		{
			if (!corpse.IsOnScreen || corpse.Distance > Settings.DrawLootableContainersDistance)
			{
				continue;
			}
			Vector2 vector = new Vector2(corpse.ScreenPosition.x, corpse.ScreenPosition.y);
			if (!corpse.ItemInit)
			{
				corpse.RefreshItems();
			}
			if (corpse.LootItems.Count <= 0)
			{
				continue;
			}
			Vector3 position = corpse.Corpse.transform.position;
			bool flag = Aimbot.CaulculateInFov2(new Vector3(position.x, position.y, position.z)) <= Settings.SimpleStringsFOV;
			if (Settings.DrawSimpleStrings)
			{
				string text = "";
				string text2 = "";
				string text3 = "";
				foreach (ContainerItem lootItem in corpse.LootItems)
				{
					if (GameUtils.ShouldDisplayItem(lootItem))
					{
						switch (lootItem.Itemcat)
						{
						case ItemCategories.Kappa:
						{
							string text11 = ColorUtility.ToHtmlStringRGB(Settings.KappaColor);
							text2 = text2 + "<color=#" + text11 + ">+</color>";
							break;
						}
						case ItemCategories.Superrare:
						{
							string text10 = ColorUtility.ToHtmlStringRGB(Settings.SuperrareColor);
							text2 = text2 + "<color=#" + text10 + ">+</color>";
							break;
						}
						case ItemCategories.Stim:
						{
							string text9 = ColorUtility.ToHtmlStringRGB(Settings.StimItemColor);
							text2 = text2 + "<color=#" + text9 + ">+</color>";
							break;
						}
						case ItemCategories.Quest:
						{
							string text8 = ColorUtility.ToHtmlStringRGB(Settings.QuestItemColor);
							text2 = text2 + "<color=#" + text8 + ">+</color>";
							break;
						}
						case ItemCategories.Wishlist:
						{
							string text7 = ColorUtility.ToHtmlStringRGB(Settings.WishlistColor);
							text2 = text2 + "<color=#" + text7 + ">+</color>";
							break;
						}
						case ItemCategories.Hideout:
						{
							string text6 = ColorUtility.ToHtmlStringRGB(Settings.HideoutColor);
							text2 = text2 + "<color=#" + text6 + ">+</color>";
							break;
						}
						case ItemCategories.Searched:
						{
							string text5 = ColorUtility.ToHtmlStringRGB(Settings.SearchedColor);
							text2 = text2 + "<color=#" + text5 + ">+</color>";
							break;
						}
						case ItemCategories.Common:
						{
							string text4 = ColorUtility.ToHtmlStringRGB(Settings.CommonItemColor);
							text2 = text2 + "<color=#" + text4 + ">+</color>";
							break;
						}
						}
					}
				}
				if (!string.IsNullOrEmpty(text2))
				{
					text += text2;
					text3 = "<color=cyan>[</color>" + text + "<color=cyan>]</color>";
				}
				if (!flag)
				{
					GUI.Label(new Rect(vector.x - (float)lineX - (float)xOffset - 8f, vector.y - (float)num - (float)itemLineHeight + 3f, 200f, itemLineHeight), text3 ?? "", style);
				}
			}
			if (!(!Settings.DrawSimpleStrings || flag) || 0 >= corpse.LootItems.Count((ContainerItem item) => GameUtils.ShouldDisplayItem(item)))
			{
				continue;
			}
			int num2 = corpse.LootItems.Count((ContainerItem item) => GameUtils.ShouldDisplayItem(item));
			if (num2 <= 0)
			{
				break;
			}
			new GUIStyle();
			Texture2D texture2D = new Texture2D(1, 1);
			texture2D.SetPixel(0, 0, Color.yellow);
			texture2D.Apply();
			int? totalprice = corpse.totalprice;
			GUI.DrawTexture(new Rect(vector.x - (float)lineX, vector.y - (float)num, lineWidth, num2 * itemLineHeight + itemLineHeight - 25), texture2D);
			string text12 = ((totalprice.HasValue && Settings.drawvalue) ? $"Dead Body [{totalprice / 1000}K] - {corpse.FormattedDistance} " : ("Dead Body - " + corpse.FormattedDistance + " "));
			GUI.Label(new Rect(vector.x - (float)lineX - (float)xOffset - 8f, vector.y - (float)num - (float)itemLineHeight + 3f, 200f, itemLineHeight), text12, style);
			int num3 = num + itemLineHeight / 3;
			foreach (ContainerItem lootItem2 in corpse.LootItems)
			{
				if (GameUtils.ShouldDisplayItem(lootItem2))
				{
					string shortName = lootItem2.ShortName;
					int count = lootItem2.Count;
					Color itemColor = GameUtils.GetItemColor(lootItem2.Itemcat);
					string text13 = ((count > 1) ? $" ({count})" : "");
					int? num4 = null;
					if (Settings.drawvalue)
					{
						num4 = lootItem2.itemprice;
					}
					string text14 = ((num4.HasValue && Settings.drawvalue) ? $"{shortName} [{num4 / 1000}K]{text13}" : (shortName + text13));
					gUIStyle.normal.textColor = itemColor;
					GUI.Label(new Rect(vector.x - (float)lineX - (float)xOffset, vector.y - (float)(num3 - itemLineHeight) - 16f, 200f, itemLineHeight), text14, gUIStyle);
					num3 -= itemLineHeight;
				}
			}
		}
	}
}
