using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace jeanf.universalplayer
{
///
/// Battery warnings for every mode (ships wired on the Player prefab, zero setup):
/// - XR: a small camera-anchored panel warns when the headset or a controller runs
/// low (levels from the XR devices, same source XrHealthMonitor logs from);
/// - Gamepad: a screen-space overlay warns when the gamepad runs low (Windows
/// XInput — other platforms report Unknown and stay silent).
/// At the critical level the game is paused through PlayerEvents.PauseRequested
/// (the bridge forwards it to the project's pause channel) and a failsafe countdown
/// starts: when it elapses, control is forced back to mouse and keyboard so a dying
/// device can never strand the player.
/// Probes are injectable (Func seams) so tests run without hardware.
///
public class BatteryWarningSystem : MonoBehaviour
{
private const string LogPrefix = "[UniversalPlayer.XR]";
public enum GamepadBatteryState { Unknown, Wired, Ok, Low, Critical }
[Header("Thresholds")]
[Range(0.05f, 0.5f)][SerializeField] private float warnLevel = 0.2f;
[Range(0.01f, 0.2f)][SerializeField] private float criticalLevel = 0.08f;
[SerializeField] private float pollIntervalSeconds = 10f;
[Header("Critical failsafe")]
[Tooltip("Pause the game (PlayerEvents.PauseRequested -> pause channel on the bridge) when any battery goes critical.")]
[SerializeField] private bool pauseOnCritical = true;
[Tooltip("Seconds of persisting critical battery before control is forced back to mouse & keyboard. 0 disables the failsafe.")]
[SerializeField] private float failsafeSwitchSeconds = 30f;
/// Test seam: (label, 0..1 level) per XR device that reports a battery.
public Func> XrBatteryProbe;
/// Test seam: coarse gamepad battery state.
public Func GamepadBatteryProbe;
private float _nextPoll;
private string _warningText = "";
private bool _critical;
private float _criticalSince;
private bool _pauseRaised;
private bool _failsafeFired;
private Text _vrText;
private Text _overlayText;
private GameObject _vrPanel;
private GameObject _overlayPanel;
private void Awake()
{
XrBatteryProbe ??= ReadXrBatteries;
GamepadBatteryProbe ??= ReadGamepadBattery;
}
private void Update()
{
if (Time.unscaledTime >= _nextPoll)
{
_nextPoll = Time.unscaledTime + pollIntervalSeconds;
Poll();
}
UpdateUi();
UpdateFailsafe();
}
private void Poll()
{
var warnings = new List();
var critical = false;
var scheme = BroadcastControlsStatus.controlScheme;
if (scheme == BroadcastControlsStatus.ControlScheme.XR)
{
foreach (var (label, level) in XrBatteryProbe())
{
// 0 (or less) means "could not read" on several runtimes (Quest Link
// among them) — a genuinely empty headset is already off. Treating it
// as critical would pause the game forever on a healthy setup.
if (level <= 0f || level > warnLevel) continue;
warnings.Add($"{label} battery {Mathf.RoundToInt(level * 100)}%");
if (level <= criticalLevel) critical = true;
}
}
else if (scheme == BroadcastControlsStatus.ControlScheme.Gamepad)
{
var state = GamepadBatteryProbe();
if (state == GamepadBatteryState.Low) warnings.Add("Controller battery low");
else if (state == GamepadBatteryState.Critical)
{
warnings.Add("Controller battery critical");
critical = true;
}
}
_warningText = string.Join("\n", warnings);
if (critical && !_critical)
{
_criticalSince = Time.unscaledTime;
_failsafeFired = false;
Debug.LogWarning($"{LogPrefix} BatteryWarningSystem: CRITICAL battery — {_warningText.Replace('\n', ';')}. " +
(pauseOnCritical ? "Pausing the game. " : "") +
(failsafeSwitchSeconds > 0 ? $"Switching to mouse & keyboard in {failsafeSwitchSeconds:F0}s unless it recovers." : ""));
if (pauseOnCritical && !_pauseRaised)
{
_pauseRaised = true;
PlayerEvents.RaisePause(true);
}
}
_critical = critical;
}
private void UpdateFailsafe()
{
if (!_critical || _failsafeFired || failsafeSwitchSeconds <= 0) return;
if (Time.unscaledTime - _criticalSince < failsafeSwitchSeconds) return;
_failsafeFired = true;
// This lives under Settings/Diagnostics while BroadcastControlsStatus sits on
// Settings/XR/HmdStatus (a cousin, not a parent or child) — fall back to a scene search.
var broadcaster = GetComponentInParent();
if (broadcaster == null) broadcaster = GetComponentInChildren(true);
if (broadcaster == null) broadcaster = FindAnyObjectByType(FindObjectsInactive.Include);
if (broadcaster != null) broadcaster.ForceDesktopControls("battery critical for too long");
else Debug.LogWarning($"{LogPrefix} BatteryWarningSystem: failsafe elapsed but no BroadcastControlsStatus found — cannot switch to desktop controls.", this);
}
private void UpdateUi()
{
var scheme = BroadcastControlsStatus.controlScheme;
var text = _warningText;
if (_critical && failsafeSwitchSeconds > 0 && !_failsafeFired)
{
var remaining = Mathf.Max(0f, failsafeSwitchSeconds - (Time.unscaledTime - _criticalSince));
text += $"\nSwitching to keyboard in {Mathf.CeilToInt(remaining)}s";
}
var showVr = scheme == BroadcastControlsStatus.ControlScheme.XR && text.Length > 0;
var showOverlay = scheme == BroadcastControlsStatus.ControlScheme.Gamepad && text.Length > 0;
if (showVr && _vrPanel == null) BuildVrPanel();
if (showOverlay && _overlayPanel == null) BuildOverlayPanel();
if (_vrPanel != null && _vrPanel.activeSelf != showVr) _vrPanel.SetActive(showVr);
if (_overlayPanel != null && _overlayPanel.activeSelf != showOverlay) _overlayPanel.SetActive(showOverlay);
if (showVr && _vrText != null) _vrText.text = text;
if (showOverlay && _overlayText != null) _overlayText.text = text;
}
// ---- runtime-built UI (no prefab assets, no wiring) ----
private void BuildVrPanel()
{
var camera = Camera.main;
if (camera == null) return;
_vrPanel = new GameObject("BatteryWarning_VR");
_vrPanel.transform.SetParent(camera.transform, false);
_vrPanel.transform.localPosition = new Vector3(0f, -0.12f, 1.2f);
var canvas = _vrPanel.AddComponent