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

namespace stupid.solutions.Features.ESP;

public class ExfiltrationPointsESP : MonoBehaviour
{
	private List<GameExfiltrationPoint> _gameExfiltrationPoints = new List<GameExfiltrationPoint>();

	private List<GameTransitPoint> _gameTransitPoints = new List<GameTransitPoint>();

	private static readonly float CacheExfiltrationPointInterval = 5f;

	private float _nextLootItemCacheTime;

	public void Update()
	{
		if (Time.time >= _nextLootItemCacheTime && Main.GameWorld != null && Main.GameWorld.ExfiltrationController.ExfiltrationPoints != null)
		{
			_gameExfiltrationPoints.Clear();
			ExfiltrationPoint[] exfiltrationPoints = Main.GameWorld.ExfiltrationController.ExfiltrationPoints;
			foreach (ExfiltrationPoint exfiltrationPoint in exfiltrationPoints)
			{
				if (GameUtils.IsExfiltrationPointValid(exfiltrationPoint))
				{
					_gameExfiltrationPoints.Add(new GameExfiltrationPoint(exfiltrationPoint));
				}
			}
			foreach (TransitPoint transitPoint in LocationsFixerV2.transitPoints)
			{
				if (GameUtils.IsTransitPointValid(transitPoint))
				{
					_gameTransitPoints.Add(new GameTransitPoint(transitPoint));
				}
			}
			_nextLootItemCacheTime = Time.time + CacheExfiltrationPointInterval;
		}
		if (!(Main.GameWorld != null))
		{
			return;
		}
		foreach (GameExfiltrationPoint gameExfiltrationPoint in _gameExfiltrationPoints)
		{
			gameExfiltrationPoint.RecalculateDynamics();
		}
		foreach (GameTransitPoint gameTransitPoint in _gameTransitPoints)
		{
			gameTransitPoint.RecalculateDynamics();
		}
	}

	private string RemoveUnderscores(string input)
	{
		return input.Replace("_", " ");
	}

	private void OnGUI()
	{
		if (!(Main.GameWorld != null))
		{
			return;
		}
		if (Settings.DrawExfiltrationPoints)
		{
			foreach (GameExfiltrationPoint gameExfiltrationPoint in _gameExfiltrationPoints)
			{
				if (GameUtils.IsExfiltrationPointValid(gameExfiltrationPoint.ExfiltrationPoint) && gameExfiltrationPoint.IsOnScreen)
				{
					string text = ColorUtility.ToHtmlStringRGB(Settings.ExfilESPColor);
					string text2 = ColorUtility.ToHtmlStringRGB((gameExfiltrationPoint.status == "Open") ? Color.green : Color.red);
					string label = "<color=#" + text + ">" + RemoveUnderscores(gameExfiltrationPoint.ExfiltrationPoint.Settings.Name) + " - </color><color=#" + text2 + ">" + gameExfiltrationPoint.status + "</color> <color=#" + text + ">[" + gameExfiltrationPoint.FormattedDistance + "]</color>";
					Render.DrawRichTextString(new Vector2(gameExfiltrationPoint.ScreenPosition.x - 50f, gameExfiltrationPoint.ScreenPosition.y), label, Color.white);
				}
			}
			foreach (GameTransitPoint gameTransitPoint in _gameTransitPoints)
			{
				if (GameUtils.IsTransitPointValid(gameTransitPoint.transitPoint) && gameTransitPoint.IsOnScreen)
				{
					string label2 = " " + RemoveUnderscores(gameTransitPoint.transitPoint.name) + " [" + gameTransitPoint.FormattedDistance + "] ";
					Render.DrawString(new Vector2(gameTransitPoint.ScreenPosition.x - 50f, gameTransitPoint.ScreenPosition.y), label2, Settings.TransitPointColor);
				}
			}
		}
		if (Main.LocalPlayerWeapon == null || !(Main.LocalPlayer != null) || !Settings.DrawInfo)
		{
			return;
		}
		string label3 = string.Empty;
		if (Main.LocalPlayer.HandsController.Item is Weapon)
		{
			Weapon localPlayerWeapon = Main.LocalPlayerWeapon;
			_EF1A currentMagazine = localPlayerWeapon.GetCurrentMagazine();
			if (currentMagazine != null)
			{
				label3 = $" {currentMagazine.Count + localPlayerWeapon.ChamberAmmoCount}/{currentMagazine.MaxCount + localPlayerWeapon.ChamberAmmoCount} ";
			}
		}
		else
		{
			label3 = "";
		}
		float x = Screen.width / 2;
		float num = Screen.height / 2;
		Render.DrawString(new Vector2(x, num + 60f), label3, Settings.AmmoCounterColor);
	}
}
