using System.Diagnostics.CodeAnalysis;
using ADOFAIRenderer.Core;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
namespace ADOFAIRenderer.Patch.Game;
///
/// Replaces the game's status text (normally "Autoplay", "Auto tile" or "Debug Mode") with the render status.
///
/// Patched member: scrShowIfDebug.Update(), which each frame sets its UI Text (txt) to the autoplay /
/// auto-tile / debug label or hides it (no-HUD modes, recording mode), in defaultColor (green) or orange for auto tiles.
///
///
/// Active while (stage >= OnScene). The postfix overwrites whatever the original set,
/// every frame, so the text is shown even in the game's no-HUD modes.
///
///
[HarmonyPatch(typeof(scrShowIfDebug))]
// ReSharper disable once InconsistentNaming
public static class scrShowIfDebugPatch
{
///
/// Postfix of scrShowIfDebug.Update. Shows, in the default (green) color:
///
/// - "Render in Ns.." during the countdown (N = RenderManager.countdown, seconds), otherwise "Render";
/// - after the end condition is met (endFrame != -1), a second line "Waiting for X.Xs.." with the remaining
/// part of DelayAfterEnd in render seconds ((frameCount - endFrame) / RenderFPS elapsed), clamped at 0.
///
///
/// Private field txt: the status Text component.
/// Private field defaultColor: the game's status text color.
[HarmonyPostfix]
[HarmonyPatch("Update")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
private static void IgnoreInput(Text ___txt, Color ___defaultColor)
{
if (!RenderManager.IsActive) return;
___txt.enabled = true;
___txt.text = "Render";
if (RenderManager.Instance)
{
if (RenderManager.Instance.countdown > 0) ___txt.text = $"Render in {RenderManager.Instance.countdown}s..";
if (RenderManager.Instance.endFrame != -1)
{
var elapsedFrames = RenderManager.Instance.frameCount - RenderManager.Instance.endFrame;
var remainingTime = RenderManager.Instance.Configuration.DelayAfterEnd - elapsedFrames / RenderManager.Instance.Configuration.RenderFPS;
if (remainingTime < 0) remainingTime = 0;
___txt.text += $"\nWaiting for {remainingTime:F1}s..";
}
}
___txt.color = ___defaultColor;
}
}