using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Ashwild.Settings
{
///
/// Standalone modal overlay for rebinding, living as its own object under the SettingsPanel (a
/// sibling of the category sub-panels) so it renders in front of everything. One message label
/// plus two buttons: while capturing it reads "press a key" with the buttons hidden; on a clash
/// it shows "X already uses this key" with Replace / Cancel. Pure UI driven by the keybindings
/// sub-panel, which holds a reference and calls these methods. Buttons are wired per-conflict.
///
public class RebindPromptUI : MonoBehaviour
{
#region Serialized Fields
[Header("Root")]
[Tooltip("The overlay object toggled on/off. Leave empty to use this GameObject.")]
[SerializeField] private GameObject root;
[Header("References")]
[SerializeField] private TMP_Text messageText;
[SerializeField] private Button replaceButton;
[SerializeField] private Button cancelButton;
[Header("Text")]
[SerializeField] private string listeningText = "Appuyez sur une touche…";
[Tooltip("{0} = the action that already uses the key.")]
[SerializeField] private string conflictFormat = "« {0} » utilise déjà cette touche.";
#endregion
#region Public API
///
/// Shows the overlay in "press a key" mode (buttons hidden).
///
public void ShowListening()
{
Target.SetActive(true);
if (messageText != null) messageText.text = listeningText;
SetButtonsActive(false);
}
///
/// Switches the open overlay to the conflict prompt, showing and wiring the two buttons.
///
public void ShowConflict(string conflictLabel, Action onReplace, Action onCancel)
{
Target.SetActive(true);
if (messageText != null) messageText.text = string.Format(conflictFormat, conflictLabel);
SetButtonsActive(true);
if (replaceButton != null)
{
replaceButton.onClick.RemoveAllListeners();
replaceButton.onClick.AddListener(() => onReplace?.Invoke());
}
if (cancelButton != null)
{
cancelButton.onClick.RemoveAllListeners();
cancelButton.onClick.AddListener(() => onCancel?.Invoke());
}
}
///
/// Hides the overlay.
///
public void Hide() => Target.SetActive(false);
#endregion
#region Internal Helpers
///
/// Shows/hides the Replace and Cancel buttons (only relevant in the conflict state).
///
private void SetButtonsActive(bool active)
{
if (replaceButton != null) replaceButton.gameObject.SetActive(active);
if (cancelButton != null) cancelButton.gameObject.SetActive(active);
}
private GameObject Target => root != null ? root : gameObject;
#endregion
}
}