using System;
using System.Text;
using System.Threading;
using ADOFAIRenderer.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace ADOFAIRenderer.UI;
///
/// On-screen overlay shown while a render is running: a statistics panel in the top-right corner and a level progress
/// bar near the bottom of the screen.
///
/// Created by RenderManager.StartFFmpegThread on a GameObject kept across scene loads, and reused for later
/// renders. The overlay is drawn on the game screen only; it is not part of the encoded video frames.
/// rebuilds the text every frame on the Unity main thread.
///
///
internal class DebugMonitorUI : MonoBehaviour
{
// --- Progress Bar UI Configuration Constants ---
// Sizes and positions are canvas units at the 1920x1080 reference resolution of the CanvasScaler.
/// Width of the progress bar; the fill width is BarWidth * progress.
private const float BarWidth = 800f;
/// Height of the progress bar.
private const float BarHeight = 28f;
/// Horizontal offset of the bar center from the screen center (0 = centered).
private const float BarPosX = 0f;
/// Vertical offset of the bar center from the screen center; -420 is 120 units above the bottom edge (540 - 420).
private const float BarPosY = -420f; // Y position relative to screen center (Reference: 1920x1080)
/// Font size of the percentage text drawn on the bar.
private const int BarTextFontSize = 16; // Increased font size for visibility
/// Unfilled bar color: opaque near-black with a slight blue tint.
private static readonly Color BarBackgroundColor = new(0.08f, 0.08f, 0.1f, 1.0f);
/// Filled part color: opaque cyan (#00CCFF).
private static readonly Color BarFillColor = new(0.0f, 0.8f, 1.0f, 1.0f); // Sleek cyan/blue
/// Percentage text color: opaque white.
private static readonly Color BarTextColor = new(1.0f, 1.0f, 1.0f, 1.0f);
// --- Progress Bar Outline Configuration Constants ---
/// Color of the uGUI drawn around the bar background.
private static readonly Color BarOutlineColor = Color.white; // Contrasting white outline
///
/// Outline.effectDistance of the bar outline: the background is redrawn shifted by +-3 units on both axes,
/// which shows as a border about 3 units wide.
///
private static readonly Vector2 BarOutlineDistance = new(3.0f, -3.0f); // Outline thickness
/// Statistics text inside the top-right panel.
private TextMeshProUGUI _text = null!;
/// Progress bar background object (parent of the fill and the percentage text).
private GameObject _progressBarObj = null!;
/// Progress bar fill object.
private GameObject _progressBarFillObj = null!;
/// Fill rect, anchored to the left edge of the bar; its width is set every frame.
private RectTransform _fillRect = null!;
/// Text on the bar: percentage and "current floor / last floor index".
private TextMeshProUGUI _progressText = null!;
///
/// Builds the overlay canvas, the statistics panel and the progress bar. Layout values are canvas units at the
/// 1920x1080 reference resolution.
///
private void Start()
{
// Set UI layer (5, Unity's built-in "UI" layer)
gameObject.layer = 5;
// Canvas setup. Sorting order 9999 is below EscapeHandlerUI (10000) and the dialogs/windows of the mod.
var canvas = gameObject.AddComponent