using EFT;
using EscapeFromTarkovCheat.Data;
using EscapeFromTarkovCheat.Utils;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace EscapeFromTarkovCheat.Feauters.ESP
{
    public class PlayerESP : MonoBehaviour
    {
        private static readonly Color _playerColor = Color.cyan;
        private static readonly Color _botColor = Color.yellow;
        private static readonly Color _healthColor = Color.green;
        private static readonly Color _bossColor = Color.red;
        private static readonly Color _cultistLeader = Color.red;
        private static readonly Color _cultistGuard = Color.red;
        private static readonly Color _guardColor = Color.red;
        private static readonly Color _skeletonColor = Color.cyan;

        private static readonly HashSet<string> BossNames = new HashSet<string>
        {
            "Killa",
            "Reshala",
            "Sanitar",
            "Glukhar",
            "Shturman",
            "Tagilla",
            "Knight",
            "BirdEye",
            "Zryachiy",
            "Big Pipe",
            "Kaban"
        };

        // Token: 0x0200001D RID: 29
        public enum PlayerType
        {
            SCAV,
            PLAYERSCAV,
            PLAYER,
            TEAMMATE,
            FOLLOWER,
            BOSS
        }
        public static bool IsBossByName(string playerName)
        {
            return PlayerESP.BossNames.Contains(playerName);
        }
        public static string PlayerName(Player player, ref PlayerESP.PlayerType playerType)
        {
            if (player.Profile.Info.RegistrationDate > 0)
            {
                if (player.Profile.Info.Side == EPlayerSide.Savage)
                {
                    playerType = PlayerESP.PlayerType.PLAYERSCAV;
                    return "player";
                }
                playerType = PlayerESP.PlayerType.PLAYER;
                return player.Profile.Info.Side.ToString() + " [" + player.Profile.Info.Level.ToString() + "]";
            }

            if (player.Profile.Info.Settings.Role.ToString().IndexOf("boss") != -1)
            {
                playerType = PlayerESP.PlayerType.BOSS;
                return "BOSS";
            }

            if (player.Profile.Info.Settings.Role.ToString().IndexOf("follower") != -1)
            {
                playerType = PlayerESP.PlayerType.FOLLOWER;
                return "Follower";
            }

            if (player.Profile.Info.Settings.Role.ToString().ToLower().IndexOf("pmcbot") != -1)
            {
                playerType = PlayerESP.PlayerType.SCAV;
                return "Raider";
            }

            playerType = PlayerESP.PlayerType.SCAV;
            return "";
        }

        private void DrawPlayerName(GamePlayer gamePlayer, ref Color color)
        {
            string text = "";
            WildSpawnType role = gamePlayer.Player.Profile.Info.Settings.Role;

            if (gamePlayer.IsAI)
            {
                text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_BOT)} [{gamePlayer.FormattedDistance}]";
                color = PlayerESP._botColor;
            }
            else
            {
                text = $"{gamePlayer.Player.Profile.Info.Nickname} [{gamePlayer.FormattedDistance}]";
                color = PlayerESP._playerColor;
            }

            if (gamePlayer.Player.Profile.Info.Settings.IsFollower())
            {
                text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_GUARD)}-{gamePlayer.Player.Profile.Info.Nickname} [{gamePlayer.FormattedDistance}]";
                color = PlayerESP._guardColor;
            }

            if (gamePlayer.Player.Profile.Info.Settings.IsBoss())
            {
                switch (role)
                {
                    case WildSpawnType.sectantPriest:
                        text = $"{gamePlayer.Player.Profile.Info.Nickname} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._cultistLeader;
                        break;
                    case WildSpawnType.sectantWarrior:
                        text = $"{gamePlayer.Player.Profile.Info.Nickname} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._cultistGuard;
                        break;
                    case WildSpawnType.pmcBEAR:
                        text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_BEAR)} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._playerColor;
                        break;
                    case WildSpawnType.pmcUSEC:
                        text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_USEC)} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._playerColor;
                        break;
                    case WildSpawnType.pmcBot:
                        text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_PMC)} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._playerColor;
                        break;
                    default:
                        text = $"{LocalisationManager.GetString(Settings.Language, StringKey.DATA_PLAYERTYPE_BOSS)}-{gamePlayer.Player.Profile.Info.Nickname} [{gamePlayer.FormattedDistance}]";
                        color = PlayerESP._bossColor;
                        break;
                }
            }

            Vector2 vector = GUI.skin.GetStyle(text).CalcSize(new GUIContent(text));
            Render.DrawString(new Vector2(gamePlayer.ScreenPosition.x - vector.x / 2f, gamePlayer.HeadScreenPosition.y - 20f), text, color, true);
        }
        private void DrawPlayerHealth(GamePlayer gamePlayer, float boxPositionY, float boxHeight, float boxWidth)
        {
            if (!gamePlayer.Player.HealthController.IsAlive)
                return;

            float current = gamePlayer.Player.HealthController.GetBodyPartHealth(EBodyPart.Common, false).Current;
            float maximum = gamePlayer.Player.HealthController.GetBodyPartHealth(EBodyPart.Common, false).Maximum;
            float healthBarHeight = GameUtils.Map(current, 0f, maximum, 0f, boxHeight);
            Render.DrawLine(new Vector2(gamePlayer.ScreenPosition.x - boxWidth / 2f - 3f, boxPositionY + boxHeight - healthBarHeight),
                new Vector2(gamePlayer.ScreenPosition.x - boxWidth / 2f - 3f, boxPositionY + boxHeight), 3f, PlayerESP._healthColor);
        }
        public void OnGUI()
        {
            if (!Settings.DrawPlayers)
                return;
            if (Main.notReady())
                return;
            foreach (GamePlayer gamePlayer in Main.GamePlayers)
            {
                if (!gamePlayer.IsOnScreen)
                    continue;
                if (gamePlayer.Distance > Settings.DrawPlayersDistance)
                    continue;
                if (gamePlayer.Player == Main.LocalPlayer)
                    continue;

                Color color = gamePlayer.IsAI ? PlayerESP._botColor : PlayerESP._playerColor;
                float boxPositionY = (gamePlayer.HeadScreenPosition.y - 10f);
                float boxHeight = (Math.Abs(gamePlayer.HeadScreenPosition.y - gamePlayer.ScreenPosition.y) + 10f);
                float boxWidth = (boxHeight * 0.65f);

                if (Settings.DrawPlayerBox)
                    Render.DrawBox(gamePlayer.ScreenPosition.x - boxWidth / 2f, boxPositionY, boxWidth, boxHeight, color);

                if (Settings.DrawPlayerSkeletons)
                    PlayerSkeletons.DrawSkeleton(new GamePlayer(gamePlayer.Player), _skeletonColor);

                if (Settings.DrawPlayerHealth)
                    DrawPlayerHealth(gamePlayer, boxPositionY, boxHeight, boxWidth);

                if (Settings.DrawPlayerName)
                    DrawPlayerName(gamePlayer, ref color);

                if (Settings.DrawPlayerLine)
                    Render.DrawLine(new Vector2((float)(Screen.width / 2), (float)Screen.height),
                                    new Vector2(gamePlayer.ScreenPosition.x, gamePlayer.ScreenPosition.y),
                                    1.5f, Color.red);
            }
        }
    }
}