using System;
using System.Collections.Generic;
using EFT.UI;
using UnityEngine;

namespace stupid.solutions.Utils;

public static class Render
{
	private class RingArray
	{
		public Vector2[] Positions { get; private set; }

		public RingArray(int numSegments)
		{
			Positions = new Vector2[numSegments];
			float num = 360f / (float)numSegments;
			for (int i = 0; i < numSegments; i++)
			{
				float f = (float)Math.PI / 180f * num * (float)i;
				Positions[i] = new Vector2(Mathf.Sin(f), Mathf.Cos(f));
			}
		}
	}

	private static Color _texturesColor;

	private static Texture2D _currentTexture;

	private static Color _currentTextureColor = Color.black;

	private static readonly Texture2D _texture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false)
	{
		filterMode = FilterMode.Bilinear
	};

	private static readonly Texture2D Texture2D = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false)
	{
		filterMode = FilterMode.Bilinear
	};

	public static Material material;

	private static Dictionary<int, RingArray> ringDict = new Dictionary<int, RingArray>();

	public static Vector4[] taaJitter = new Vector4[4]
	{
		new Vector4(0.5f, 0.5f, 0f, 0f),
		new Vector4(-0.5f, 0.5f, 0f, 0f),
		new Vector4(-0.5f, -0.5f, 0f, 0f),
		new Vector4(0.5f, -0.5f, 0f, 0f)
	};

	public static int frameIndex = 0;

	public static GUIStyle StringStyle { get; set; } = new GUIStyle(GUI.skin.label);

	public static GUIStyle StringStyleOutline { get; set; } = new GUIStyle(GUI.skin.label);

	public static Color Color
	{
		get
		{
			return GUI.color;
		}
		set
		{
			GUI.color = value;
		}
	}

	public static void DrawLine(Vector2 from, Vector2 to, float thickness, Color color)
	{
		Color = color;
		DrawLine(from, to, thickness);
	}

	public static void DrawLine(Vector2 from, Vector2 to, float thickness)
	{
		Main.AAMaterial.SetPass(0);
		Vector2 normalized = (to - from).normalized;
		float num = Mathf.Atan2(normalized.y, normalized.x) * 57.29578f;
		GUIUtility.RotateAroundPivot(num, from);
		DrawBox(from, Vector2.right * (from - to).magnitude, thickness, centered: false);
		GUIUtility.RotateAroundPivot(0f - num, from);
	}

	public static void DrawLineGL(Vector2 from, Vector2 to, Color color)
	{
		GL.PushMatrix();
		GameUtils.DrawMaterial.SetPass(0);
		GL.LoadOrtho();
		GL.Begin(1);
		GL.Color(color);
		GL.Vertex(new Vector3(from.x / (float)Screen.width, from.y / (float)Screen.height, 0f));
		GL.Vertex(new Vector3(to.x / (float)Screen.width, to.y / (float)Screen.height, 0f));
		GL.End();
		GL.PopMatrix();
	}

	public static void DrawBox(float x, float y, float w, float h, Color color)
	{
		DrawLine(new Vector2(x, y), new Vector2(x + w, y), 1f, color);
		DrawLine(new Vector2(x, y), new Vector2(x, y + h), 1f, color);
		DrawLine(new Vector2(x + w, y), new Vector2(x + w, y + h), 1f, color);
		DrawLine(new Vector2(x, y + h), new Vector2(x + w, y + h), 1f, color);
	}

	public static void DrawBox(Vector2 position, Vector2 size, float thickness, Color color, bool centered = true)
	{
		Color = color;
		DrawBox(position, size, thickness, centered);
	}

	public static void DrawBox(Vector2 position, Vector2 size, float thickness, bool centered = true)
	{
		if (centered)
		{
			_ = position - size / 2f;
		}
		GUI.DrawTexture(new Rect(position.x, position.y, size.x, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(position.x, position.y, thickness, size.y), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(position.x + size.x, position.y, thickness, size.y), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(position.x, position.y + size.y, size.x + thickness, thickness), Texture2D.whiteTexture);
	}

	public static void DrawCornerBox(Vector2 position, Vector2 size, float thickness, Color color, float cornerLength = 10f, bool centered = true)
	{
		Vector2 vector = (centered ? (position - size / 2f) : position);
		GUI.color = color;
		GUI.DrawTexture(new Rect(vector.x, vector.y, cornerLength, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x, vector.y, thickness, cornerLength), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x + size.x - cornerLength, vector.y, cornerLength, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x + size.x - thickness, vector.y, thickness, cornerLength), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x, vector.y + size.y - cornerLength, thickness, cornerLength), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x, vector.y + size.y - thickness, cornerLength, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x + size.x - cornerLength, vector.y + size.y - thickness, cornerLength, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(vector.x + size.x - thickness, vector.y + size.y - cornerLength, thickness, cornerLength), Texture2D.whiteTexture);
		GUI.color = Color.white;
	}

	public static void DrawCross(Vector2 position, Vector2 size, float thickness, Color color)
	{
		Color = color;
		DrawCross(position, size, thickness);
	}

	public static void DrawCross(Vector2 position, Vector2 size, float thickness)
	{
		GUI.DrawTexture(new Rect(position.x - size.x / 2f, position.y, size.x, thickness), Texture2D.whiteTexture);
		GUI.DrawTexture(new Rect(position.x, position.y - size.y / 2f, thickness, size.y), Texture2D.whiteTexture);
	}

	public static void DrawDot(Vector2 position)
	{
		DrawBox(position - Vector2.one, Vector2.one * 2f, 1f);
	}

	public static void DrawString(Vector2 position, string label, Color color, bool centered = true, Color outlineColor = default(Color), bool outline = true)
	{
		if (outlineColor == default(Color))
		{
			outlineColor = Color.black;
		}
		DrawText(position, label, color, outlineColor, centered, outline);
	}

	public static void DrawRichTextString(Vector2 position, string label, Color color, bool centered = true)
	{
		DrawTextRich(position, label, color, centered);
	}

	private static void DrawTextRich(Vector2 position, string label, Color color, bool centered = true, bool outline = true)
	{
		if (Main.TXTFONT == null)
		{
			ConsoleScreen.Log("TEXT FONT NOT LOADED !");
			return;
		}
		GUIContent content = new GUIContent(label);
		StringStyle.fontSize = Settings.fontsize;
		if (StringStyle.font != Main.TXTFONT)
		{
			StringStyle.font = Main.TXTFONT;
		}
		StringStyle.normal.textColor = color;
		StringStyle.alignment = TextAnchor.MiddleCenter;
		Vector2 vector = StringStyle.CalcSize(content);
		Vector2 position2 = (centered ? (position - vector / 2f) : position);
		Color = color;
		StringStyle.normal.textColor = color;
		GUI.Label(new Rect(position2, vector), content, StringStyle);
	}

	public static void DrawTextRadar(Vector2 position, string label, Color color, bool centered = true)
	{
		if (Main.TXTFONT == null)
		{
			ConsoleScreen.Log("TEXT FONT NOT LOADED !");
		}
		GUIContent content = new GUIContent(label);
		StringStyle.fontSize = 12;
		if (StringStyle.font != Main.TXTFONT)
		{
			StringStyle.font = Main.TXTFONT;
		}
		StringStyle.alignment = TextAnchor.MiddleCenter;
		Vector2 vector = StringStyle.CalcSize(content);
		Vector2 position2 = (centered ? (position - vector / 2f) : position);
		StringStyle.normal.textColor = color;
		Color = color;
		GUI.Label(new Rect(position2, vector), content, StringStyle);
	}

	public static void DrawText1(Vector2 position, string label, Color color, bool centered = true)
	{
		if (Main.TXTFONT == null)
		{
			ConsoleScreen.Log("TEXT FONT NOT LOADED !");
		}
		GUIContent content = new GUIContent(label);
		StringStyle.fontSize = Settings.fontsize;
		if (StringStyle.font != Main.TXTFONT)
		{
			StringStyle.font = Main.TXTFONT;
		}
		StringStyle.alignment = TextAnchor.MiddleCenter;
		Vector2 vector = StringStyle.CalcSize(content);
		Vector2 position2 = (centered ? (position - vector / 2f) : position);
		StringStyle.normal.textColor = color;
		Color = color;
		GUI.Label(new Rect(position2, vector), content, StringStyle);
	}

	private static void DrawText(Vector2 position, string label, Color color, Color outlinecolor, bool centered = true, bool outline = true)
	{
		if (Main.TXTFONT == null)
		{
			ConsoleScreen.Log("TEXT FONT NOT LOADED !");
			return;
		}
		GUIContent content = new GUIContent(label);
		StringStyle.fontSize = Settings.fontsize;
		if (StringStyle.font != Main.TXTFONT)
		{
			StringStyle.font = Main.TXTFONT;
		}
		StringStyle.normal.textColor = color;
		StringStyle.alignment = TextAnchor.MiddleCenter;
		Vector2 vector = StringStyle.CalcSize(content);
		Vector2 vector2 = (centered ? (position - vector / 2f) : position);
		if (Settings.DrawOutline && outline)
		{
			StringStyleOutline.fontSize = Settings.fontsize;
			if (StringStyleOutline.font != Main.TXTFONT)
			{
				StringStyleOutline.font = Main.TXTFONT;
			}
			StringStyleOutline.alignment = TextAnchor.MiddleCenter;
			StringStyleOutline.normal.textColor = outlinecolor;
			Vector2[] array = new Vector2[8]
			{
				new Vector2(-1f, 0f),
				new Vector2(1f, 0f),
				new Vector2(0f, -1f),
				new Vector2(0f, 1f),
				new Vector2(-1f, -1f),
				new Vector2(1f, -1f),
				new Vector2(-1f, 1f),
				new Vector2(1f, 1f)
			};
			foreach (Vector2 vector3 in array)
			{
				GUI.Label(new Rect(vector2 + vector3, vector), content, StringStyleOutline);
			}
		}
		Color = color;
		StringStyle.normal.textColor = color;
		GUI.Label(new Rect(vector2, vector), content, StringStyle);
	}

	public static void DrawCircle(Vector2 position, float radius, int numSides, Color color, bool centered = true)
	{
		GL.PushMatrix();
		GameUtils.DrawMaterial.SetPass(0);
		GL.LoadOrtho();
		GL.Begin(1);
		GL.Color(color);
		float num = 360f / (float)numSides;
		Vector2 vector = (centered ? position : (position + Vector2.one * radius));
		for (int i = 0; i < numSides; i++)
		{
			float f = (float)Math.PI / 180f * ((float)i * num);
			float f2 = (float)Math.PI / 180f * ((float)(i + 1) * num);
			Vector2 vector2 = vector + new Vector2(Mathf.Cos(f), Mathf.Sin(f)) * radius;
			Vector2 vector3 = vector + new Vector2(Mathf.Cos(f2), Mathf.Sin(f2)) * radius;
			GL.Vertex(new Vector3(vector2.x / (float)Screen.width, vector2.y / (float)Screen.height, 0f));
			GL.Vertex(new Vector3(vector3.x / (float)Screen.width, vector3.y / (float)Screen.height, 0f));
		}
		GL.End();
		GL.PopMatrix();
	}

	public static void DrawCornerBox(Vector2 headPosition, float width, float height, Color color, bool outline)
	{
		int num = (int)(width / 4f);
		int num2 = num;
		if (outline)
		{
			RectFilled(headPosition.x - width / 2f - 1f, headPosition.y - 1f, num + 2, 3f, Color.black);
			RectFilled(headPosition.x - width / 2f - 1f, headPosition.y - 1f, 3f, num2 + 2, Color.black);
			RectFilled(headPosition.x + width / 2f - (float)num - 1f, headPosition.y - 1f, num + 2, 3f, Color.black);
			RectFilled(headPosition.x + width / 2f - 1f, headPosition.y - 1f, 3f, num2 + 2, Color.black);
			RectFilled(headPosition.x - width / 2f - 1f, headPosition.y + height - 4f, num + 2, 3f, Color.black);
			RectFilled(headPosition.x - width / 2f - 1f, headPosition.y + height - (float)num2 - 4f, 3f, num2 + 2, Color.black);
			RectFilled(headPosition.x + width / 2f - (float)num - 1f, headPosition.y + height - 4f, num + 2, 3f, Color.black);
			RectFilled(headPosition.x + width / 2f - 1f, headPosition.y + height - (float)num2 - 4f, 3f, num2 + 3, Color.black);
		}
		RectFilled(headPosition.x - width / 2f, headPosition.y, num, 1f, color);
		RectFilled(headPosition.x - width / 2f, headPosition.y, 1f, num2, color);
		RectFilled(headPosition.x + width / 2f - (float)num, headPosition.y, num, 1f, color);
		RectFilled(headPosition.x + width / 2f, headPosition.y, 1f, num2, color);
		RectFilled(headPosition.x - width / 2f, headPosition.y + height - 3f, num, 1f, color);
		RectFilled(headPosition.x - width / 2f, headPosition.y + height - (float)num2 - 3f, 1f, num2, color);
		RectFilled(headPosition.x + width / 2f - (float)num, headPosition.y + height - 3f, num, 1f, color);
		RectFilled(headPosition.x + width / 2f, headPosition.y + height - (float)num2 - 3f, 1f, num2 + 1, color);
	}

	public static void RectFilled(float x, float y, float width, float height, Color color)
	{
		if (color != _texturesColor)
		{
			_texturesColor = color;
			_texture.SetPixel(0, 0, color);
			_texture.Apply();
		}
		GUI.DrawTexture(new Rect(x, y, width, height), _texture);
	}

	public static void BoxRect(Rect rect, Color color)
	{
		if (_currentTexture == null)
		{
			_currentTexture = new Texture2D(1, 1);
			_currentTexture.SetPixel(0, 0, color);
			_currentTexture.Apply();
			_currentTextureColor = color;
		}
		else if (color != _currentTextureColor)
		{
			_currentTexture.SetPixel(0, 0, color);
			_currentTexture.Apply();
			_currentTextureColor = color;
		}
		GUI.DrawTexture(rect, _currentTexture);
	}

	public static void DrawRadarBackground(Rect rect)
	{
		Color color = new Color(0f, 0f, 0f, 0.5f);
		Texture2D.SetPixel(0, 0, color);
		Texture2D.Apply();
		GUI.color = color;
		GUI.DrawTexture(rect, Texture2D);
	}

	public static void ApplyTAA()
	{
		if (Main.AAMaterial != null)
		{
			frameIndex = (frameIndex + 1) % taaJitter.Length;
			Main.AAMaterial.SetVector("_Jitter", taaJitter[frameIndex]);
		}
	}
}
