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

namespace stupid.solutions.Features.ESP;

public class LootableContainerESP : MonoBehaviour
{
	private static readonly float CacheLootItemsInterval = 600f;

	private float _nextLootContainerCacheTime;

	public static List<GameLootContainer> _gameLootContainers;

	private static readonly Color LootableContainerColor = new Color(1f, 0.2f, 0.09f);

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

	private static readonly float ItemListCacheInterval = 10f;

	private float _nextItemListCache;

	private bool gameworldwasnotnull;

	private bool repatchbecausenewgame;

	public void Start()
	{
		_gameLootContainers = new List<GameLootContainer>();
	}

	public void Update()
	{
		if (!Settings.DrawLootableContainers)
		{
			return;
		}
		if ((Time.time >= _nextLootContainerCacheTime || repatchbecausenewgame) && Main.GameWorld != null && Main.GameWorld.LootItems != null && Main.LocalPlayer != null && Main.MainCamera != null)
		{
			repatchbecausenewgame = false;
			_gameLootContainers.Clear();
			ConsoleScreen.Log("Getting Container Instances (game start)");
			LootableContainer[] array = Object.FindObjectsOfType<LootableContainer>();
			foreach (LootableContainer lootableContainer in array)
			{
				if (GameUtils.IsLootableContainerValid(lootableContainer))
				{
					_gameLootContainers.Add(new GameLootContainer(lootableContainer));
				}
			}
			_nextLootContainerCacheTime = Time.time + CacheLootItemsInterval;
			gameworldwasnotnull = true;
		}
		if (gameworldwasnotnull && Main.GameWorld == null)
		{
			repatchbecausenewgame = true;
			gameworldwasnotnull = false;
		}
		if (Main.GameWorld != null && _gameLootContainers.Count > 0)
		{
			foreach (GameLootContainer gameLootContainer2 in _gameLootContainers)
			{
				gameLootContainer2.RecalculateDynamics();
			}
		}
		if (!(Time.time >= _nextItemListCache))
		{
			return;
		}
		foreach (GameLootContainer gameLootContainer in _gameLootContainers)
		{
			if (gameLootContainer.LootableContainer.ItemOwner.RootItem.GetAllItems().Count((Item item) => item != null && !gameLootContainer.IsContainerName(item.LocalizedName())) != gameLootContainer.CachedItemCount)
			{
				gameLootContainer.RefreshItems();
				_ = gameLootContainer.ItemInit;
			}
		}
		_nextItemListCache = Time.time + ItemListCacheInterval;
	}

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