using System.Collections.Generic;
using Comfort.Common;
using EFT;
using EFT.Interactive;
using EscapeFromTarkovCheat.Data;
using EscapeFromTarkovCheat.Utils;
using JsonType;
using UnityEngine;

namespace EscapeFromTarkovCheat.Feauters.ESP
{
    public class ExfiltrationPointsESP : MonoBehaviour
    {
        private List<GameExfiltrationPoint> _gameExfiltrationPoints= new List<GameExfiltrationPoint>();
        private static readonly float CacheExfiltrationPointInterval = 5f;
        private float _nextLootItemCacheTime;

        private static readonly Color ExfiltrationPointColour = Color.green;
        private static readonly Color ClosedExfiltrationPointColour = Color.yellow;

        public void Update()
        {
            if (!Settings.DrawExfiltrationPoints) 
                return;

            if (Time.time >= _nextLootItemCacheTime)
            {
                if ((Main.GameWorld != null) && (Main.GameWorld.ExfiltrationController.ExfiltrationPoints != null))
                {
                    _gameExfiltrationPoints.Clear();
                    foreach (var exfiltrationPoint in Main.GameWorld.ExfiltrationController.EligiblePoints(Main.LocalPlayer.Profile))
                    {
                        if (!GameUtils.IsExfiltrationPointValid(exfiltrationPoint))
                            continue;

                        _gameExfiltrationPoints.Add(new GameExfiltrationPoint(exfiltrationPoint, true));
                    }

                    foreach (var exfiltrationPoint in Main.GameWorld.ExfiltrationController.ExfiltrationPoints)
                    {
                        bool trig = false;
                        if (!GameUtils.IsExfiltrationPointValid(exfiltrationPoint))
                            continue;
                        foreach (var ep in _gameExfiltrationPoints)
                        {
                            if (ep.Name == exfiltrationPoint.Settings.Name.Localized())
                                trig = !trig;
                            continue;
                        }
                        if (trig)
                            continue;
                        _gameExfiltrationPoints.Add(new GameExfiltrationPoint(exfiltrationPoint, false));
                    }

                    _nextLootItemCacheTime = (Time.time + CacheExfiltrationPointInterval);
                }
            }

            foreach (GameExfiltrationPoint gameExfiltrationPoint in _gameExfiltrationPoints)
                gameExfiltrationPoint.RecalculateDynamics();

        }
        private static string GetName(ExfiltrationPoint point, bool isEligible)
        {
            var localizedName = point.Settings.Name.Localized();
            return !isEligible ? localizedName : $"{localizedName} ({GetStatus(point.Status)})";
        }

        public static string GetStatus(EExfiltrationStatus status)
        {
            return status switch
            {
                EExfiltrationStatus.AwaitsManualActivation => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_ACTIVE),
                EExfiltrationStatus.Countdown => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_TIMER),
                EExfiltrationStatus.NotPresent => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_CLOSED),
                EExfiltrationStatus.Pending => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_PENDING),
                EExfiltrationStatus.RegularMode => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_OPEN),
                EExfiltrationStatus.UncompleteRequirements => LocalisationManager.GetString(Settings.Language, StringKey.DATA_EP_REQUIREMENT),
                _ => string.Empty
            };
        }

        private void OnGUI()
        {
            if (Settings.DrawExfiltrationPoints)
            {
                foreach (var exfiltrationPoint in _gameExfiltrationPoints)
                {
                    if (!GameUtils.IsExfiltrationPointValid(exfiltrationPoint.ExfiltrationPoint) || !exfiltrationPoint.IsOnScreen)
                        continue;

                    string exfiltrationPointText = $"{GetName(exfiltrationPoint.ExfiltrationPoint, exfiltrationPoint.Eligible)} [{exfiltrationPoint.FormattedDistance}]";

                    Render.DrawString(new Vector2(exfiltrationPoint.ScreenPosition.x - 50f, exfiltrationPoint.ScreenPosition.y), exfiltrationPointText, exfiltrationPoint.Eligible ? ExfiltrationPointColour : ClosedExfiltrationPointColour);
                }
            }
        }

    }
}
