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

namespace stupid.solutions.Features.ESP;

public class ItemESP : MonoBehaviour
{
	private static readonly float CacheLootItemsInterval = 5f;

	private static readonly float clearcacheinterval = 600f;

	private float _nextclearCacheTime;

	private float _nextLootItemCacheTime;

	public static List<GameLootItem> _gameLootItems = new List<GameLootItem>();

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

	private bool addedwishitems;

	private static Shader _cachedForceFieldShader;

	public void Update()
	{
		if (Main.GameWorld == null && _gameLootItems.Count > 0)
		{
			_gameLootItems.Clear();
		}
		if (!Settings.DrawLootableContainers && !Settings.DrawLootItems)
		{
			return;
		}
		if (Main.LocalPlayer != null && !addedwishitems)
		{
			Main.GetWishlistItems();
			addedwishitems = true;
			ConsoleScreen.Log("Added wishlist items");
		}
		else if (Main.LocalPlayer == null && addedwishitems)
		{
			ClearWishlistItems();
		}
		if (!Settings.DrawLootItems)
		{
			return;
		}
		if (Time.time >= _nextLootItemCacheTime)
		{
			CacheLootItems();
			_nextLootItemCacheTime = Time.time + CacheLootItemsInterval;
		}
		_gameLootItems.RemoveAll((GameLootItem li) => !GameUtils.IsLootItemValid(li.LootItem) || li == null || li.LootItem.transform == null);
		foreach (GameLootItem gameLootItem in _gameLootItems)
		{
			if (gameLootItem != null && GameUtils.IsLootItemValid(gameLootItem.LootItem))
			{
				if (Main.GameWorld == null)
				{
					return;
				}
				gameLootItem.RecalculateDynamics();
				if (gameLootItem.Itemcat == ItemCategories.Uninitialized)
				{
					gameLootItem.SetItemCat();
				}
				if (!gameLootItem.itemprice.HasValue)
				{
					gameLootItem.CalculateItemPrice();
				}
			}
		}
		if (Time.time >= _nextclearCacheTime)
		{
			_gameLootItems.Clear();
			_nextclearCacheTime = Time.time + clearcacheinterval;
			ConsoleScreen.Log("memory cleaned");
		}
	}

	private void CacheLootItems()
	{
		if (Main.GameWorld == null || Main.GameWorld.LootItems == null)
		{
			return;
		}
		int num = 0;
		for (int i = 0; i < Main.GameWorld.LootItems.Count; i++)
		{
			LootItem byIndex = Main.GameWorld.LootItems.GetByIndex(i);
			if (!(byIndex == null) && GameUtils.IsLootItemValid(byIndex))
			{
				_ = byIndex.transform.position;
				if (!(byIndex.Item.LocalizedName() == "Default Inventory"))
				{
					num++;
				}
			}
		}
		if (_gameLootItems.Count == num)
		{
			return;
		}
		_gameLootItems.Clear();
		ConsoleScreen.Log("Item ESP Item Instance Mismatch, Updating...");
		for (int j = 0; j < Main.GameWorld.LootItems.Count; j++)
		{
			LootItem byIndex2 = Main.GameWorld.LootItems.GetByIndex(j);
			if (!(byIndex2 == null) && GameUtils.IsLootItemValid(byIndex2))
			{
				_ = byIndex2.transform.position;
				if (!(byIndex2.Item.LocalizedName() == "Default Inventory"))
				{
					_gameLootItems.Add(new GameLootItem(byIndex2));
				}
			}
		}
	}

	public void ClearWishlistItems()
	{
		Main.wishlistitemids.Clear();
		Main.hideoutitemids.Clear();
		addedwishitems = false;
	}

	private void RenderItemESP()
	{
		List<(Vector2, string, Color)> list = new List<(Vector2, string, Color)>();
		foreach (GameLootItem gameLootItem in _gameLootItems)
		{
			if (gameLootItem != null && gameLootItem.IsOnScreen && !(gameLootItem.Distance > Settings.DrawLootItemsDistance))
			{
				_ = gameLootItem.LocalizedName;
				_ = gameLootItem.ItemID;
				string localizedName = gameLootItem.LocalizedName;
				localizedName = ((!(gameLootItem.stackcount > 1)) ? gameLootItem.LocalizedName : $"{gameLootItem.LocalizedName}({gameLootItem.stackcount})");
				int? itemprice = gameLootItem.itemprice;
				bool flag = true;
				if (Settings.DrawSimpleStrings)
				{
					Vector3 position = gameLootItem.LootItem.gameObject.transform.position;
					flag = Aimbot.CaulculateInFov2(new Vector3(position.x, position.y, position.z)) <= Settings.SimpleStringsFOV;
				}
				string text = ((Settings.drawvalue && itemprice.HasValue) ? $"{localizedName} [{itemprice / 1000}K] - {gameLootItem.FormattedDistance} " : (localizedName + " - " + gameLootItem.FormattedDistance + " "));
				string text2 = " + ";
				string item = ((!Settings.DrawSimpleStrings) ? text : (flag ? text : text2));
				ItemCategories itemcat = gameLootItem.Itemcat;
				Vector2 screenPositionFinal = gameLootItem.ScreenPositionFinal;
				Color itemColor = GameUtils.GetItemColor(itemcat);
				if (itemcat == ItemCategories.Quest && Settings.quest)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Superrare && Settings.superrare)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Kappa && Settings.kappa)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Stim && Settings.stim)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Searched && Settings.searchItem)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Wishlist && Settings.drawwishlistitem)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Hideout && Settings.drawhideoutitems)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (itemcat == ItemCategories.Common && Settings.common && gameLootItem.Distance < Settings.commonitemdistance)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
				else if (gameLootItem.itemprice > Settings.ESPPRICEfiltervalue && Settings.ESPFilterbyprice)
				{
					list.Add((screenPositionFinal, item, itemColor));
				}
			}
		}
		foreach (var (position2, label, color) in list)
		{
			Render.DrawString(position2, label, color);
		}
	}

	private void OnGUI()
	{
		if (Settings.DrawLootItems && !(Main.GameWorld == null) && !(Main.LocalPlayer == null))
		{
			RenderItemESP();
			if (Settings.weaponchams)
			{
				weaponChams();
			}
		}
	}

	private void weaponChams()
	{
		if (Main.GameWorld == null || !Settings.weaponchams)
		{
			ResetWeaponChams();
			return;
		}
		if (_cachedForceFieldShader == null)
		{
			_cachedForceFieldShader = Main.bundle.LoadAsset<Shader>("wireframe.shader");
		}
		Renderer[] componentsInChildren = Main.LocalPlayer.GetComponentsInChildren<Renderer>();
		foreach (Renderer renderer in componentsInChildren)
		{
			Material material = renderer.material;
			if (!(material == null))
			{
				if (!originalWeaponShaders.ContainsKey(renderer))
				{
					originalWeaponShaders[renderer] = material.shader;
				}
				material.shader = _cachedForceFieldShader;
				material.SetColor("_VisibleColor", Settings.SelfChamsColor);
				material.SetColor("_InvisibleColor", Settings.SelfChamsColor);
			}
		}
	}

	private void ResetWeaponChams()
	{
		Renderer[] componentsInChildren = Main.LocalPlayer.GetComponentsInChildren<Renderer>();
		foreach (Renderer renderer in componentsInChildren)
		{
			if (!(renderer == null) && originalWeaponShaders.ContainsKey(renderer))
			{
				Material material = renderer.material;
				if (!(material == null))
				{
					material.shader = originalWeaponShaders[renderer];
					material.SetColor("_Color", Color.white);
				}
			}
		}
		originalWeaponShaders.Clear();
	}
}
