#nullable enable
using UnityEngine;

namespace IGP.UnitySDK
{
    public sealed class IGPNetworkDiagnosticsOverlay : MonoBehaviour
    {
        private static IGPNetworkDiagnosticsOverlay? instance;

        private bool expanded = true;
        private GUIStyle? labelStyle;
        private string copyHint = string.Empty;
        private float copyHintUntil;

        public static void EnsureExists()
        {
            if (instance != null)
            {
                return;
            }

            var go = new GameObject("[IGPNetDiagOverlay]");
            DontDestroyOnLoad(go);
            instance = go.AddComponent<IGPNetworkDiagnosticsOverlay>();
        }

        private void Update()
        {
            try
            {
                if (Input.GetKeyDown(KeyCode.P))
                {
                    expanded = !expanded;
                }
            }
            catch
            {
                // New Input System projects may throw when using legacy Input.
            }
        }

        private void OnGUI()
        {
            if (!IGPNetworkDiagnostics.Enabled || !IGPNetworkDiagnostics.OverlayEnabled)
            {
                return;
            }

            labelStyle ??= new GUIStyle(GUI.skin.label)
            {
                fontSize = 13,
                wordWrap = true,
                richText = false
            };

            const float x = 8f;
            float y = 8f;

            string title = expanded ? "v NetDiag (P)" : "> NetDiag (P)";
            if (GUI.Button(new Rect(x, y, 150f, 24f), title))
            {
                expanded = !expanded;
            }

            if (!expanded)
            {
                return;
            }

            y += 28f;
            const float width = 440f;
            const float panelHeight = 235f;
            GUI.Box(new Rect(x, y, width, panelHeight), GUIContent.none);

            GUI.Label(
                new Rect(x + 8f, y + 4f, width - 16f, 18f),
                "Log: " + IGPNetworkDiagnostics.LogFilePath,
                new GUIStyle(labelStyle) { fontSize = 10 });

            GUI.Label(
                new Rect(x + 8f, y + 24f, width - 16f, panelHeight - 88f),
                IGPNetworkDiagnostics.CurrentSnapshot,
                labelStyle);

            float btnY = y + panelHeight - 46f;
            if (GUI.Button(new Rect(x + 8f, btnY, 120f, 26f), "Copy all"))
            {
                GUIUtility.systemCopyBuffer = IGPNetworkDiagnostics.GetFullHistory();
                copyHint = "Copied";
                copyHintUntil = Time.realtimeSinceStartup + 2f;
            }

            if (GUI.Button(new Rect(x + 136f, btnY, 90f, 26f), "Clear"))
            {
                IGPNetworkDiagnostics.ResetHistory();
            }

            if (!string.IsNullOrEmpty(copyHint) && Time.realtimeSinceStartup < copyHintUntil)
            {
                GUI.Label(new Rect(x + 234f, btnY + 3f, width - 240f, 24f), copyHint, labelStyle);
            }
        }
    }
}
