using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

namespace FrameDoctor.Demo
{
    /// <summary>
    /// Displays on-screen instructions for using the FrameDoctor demo.
    /// </summary>
    public class DemoInstructions : MonoBehaviour
    {
        [Header("Display Settings")]
        public bool showInstructions = true;
        public Key toggleKey = Key.H;

        [Header("Style")]
        public int fontSize = 16;
        public Color backgroundColor = new Color(0, 0, 0, 0.8f);
        public Color textColor = Color.white;
        public Color highlightColor = new Color(0.4f, 0.8f, 1f);

        private GUIStyle _boxStyle;
        private GUIStyle _titleStyle;
        private GUIStyle _textStyle;
        private GUIStyle _highlightStyle;
        private bool _stylesInitialized;

        private readonly string[] _instructions = new string[]
        {
            "1. Open <b>Tools → FrameDoctor</b> in the menu bar",
            "2. Sign in to your FrameDoctor account (or create one)",
            "3. Click <b>Start Capture</b> to begin recording profiler data",
            "4. Let the scene run for 5-10 seconds",
            "5. Click <b>Stop & Analyze</b> to send data for AI analysis",
            "6. View your performance report with actionable recommendations"
        };

        void Update()
        {
#if ENABLE_INPUT_SYSTEM
            if (Keyboard.current != null && Keyboard.current[toggleKey].wasPressedThisFrame)
            {
                showInstructions = !showInstructions;
            }
#else
            if (Input.GetKeyDown((KeyCode)toggleKey))
            {
                showInstructions = !showInstructions;
            }
#endif
        }

        void InitStyles()
        {
            if (_stylesInitialized) return;

            _boxStyle = new GUIStyle(GUI.skin.box);
            _boxStyle.normal.background = MakeTexture(2, 2, backgroundColor);
            _boxStyle.padding = new RectOffset(20, 20, 20, 20);

            _titleStyle = new GUIStyle(GUI.skin.label);
            _titleStyle.fontSize = fontSize + 8;
            _titleStyle.fontStyle = FontStyle.Bold;
            _titleStyle.normal.textColor = highlightColor;
            _titleStyle.alignment = TextAnchor.MiddleCenter;
            _titleStyle.margin = new RectOffset(0, 0, 0, 15);

            _textStyle = new GUIStyle(GUI.skin.label);
            _textStyle.fontSize = fontSize;
            _textStyle.normal.textColor = textColor;
            _textStyle.richText = true;
            _textStyle.margin = new RectOffset(0, 0, 5, 5);

            _highlightStyle = new GUIStyle(_textStyle);
            _highlightStyle.normal.textColor = highlightColor;

            _stylesInitialized = true;
        }

        void OnGUI()
        {
            if (!showInstructions) return;

            InitStyles();

            float boxWidth = 450;
            float x = 20;
            float y = 20;

            // Use GUILayout to auto-size the panel
            GUILayout.BeginArea(new Rect(x, y, boxWidth, Screen.height - 40));

            GUILayout.BeginVertical(_boxStyle);

            GUILayout.Label("FrameDoctor Demo", _titleStyle);

            GUILayout.Label("This scene generates CPU, memory, and rendering load\nto demonstrate FrameDoctor's profiler analysis.", _textStyle);

            GUILayout.Space(15);

            GUILayout.Label("<b>How to use:</b>", _textStyle);

            GUILayout.Space(5);

            foreach (var instruction in _instructions)
            {
                GUILayout.Label(instruction, _textStyle);
            }

            GUILayout.Space(15);

            GUILayout.Label($"Press <b>H</b> to toggle this panel", _textStyle);

            GUILayout.EndVertical();

            GUILayout.EndArea();

            // Show FPS counter in corner
            DrawFPSCounter();
        }

        private void DrawFPSCounter()
        {
            float fps = 1f / Time.deltaTime;
            string fpsText = $"FPS: {fps:F1}";

            GUIStyle fpsStyle = new GUIStyle(GUI.skin.label);
            fpsStyle.fontSize = fontSize + 2;
            fpsStyle.fontStyle = FontStyle.Bold;
            fpsStyle.normal.textColor = fps < 30 ? Color.red : (fps < 60 ? Color.yellow : Color.green);

            float padding = 10;
            Vector2 size = fpsStyle.CalcSize(new GUIContent(fpsText));

            GUI.Label(new Rect(Screen.width - size.x - padding, padding, size.x, size.y), fpsText, fpsStyle);
        }

        private Texture2D MakeTexture(int width, int height, Color color)
        {
            Color[] pixels = new Color[width * height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = color;
            }
            Texture2D texture = new Texture2D(width, height);
            texture.SetPixels(pixels);
            texture.Apply();
            return texture;
        }
    }
}
