using UnityEngine;
using UnityEngine.UI;

namespace AR51.HeadsetCalibration.Samples
{
    /// <summary>
    /// Example: show the calibration hints in your own UI instead of the ready-made
    /// <see cref="CalibrationHintPanel"/>. Listens to the calibrator's two events and writes the
    /// hint into a Text of your choice (or only logs it). Copy it and adapt it to your app.
    /// </summary>
    public sealed class CustomHintExample : MonoBehaviour
    {
        [Tooltip("The calibrator to listen to. Found in the scene when empty.")]
        public HeadsetCalibrator Calibrator;

        [Tooltip("Your own text element for the hint. Leave empty to only log.")]
        public Text Label;

        private void OnEnable()
        {
            if (Calibrator == null) Calibrator = FindFirstObjectByType<HeadsetCalibrator>();
            if (Calibrator == null)
            {
                Debug.LogWarning($"{nameof(CustomHintExample)}: no {nameof(HeadsetCalibrator)} in the scene.");
                return;
            }

            Calibrator.HintChanged += OnHintChanged;
            Calibrator.StateChanged += OnStateChanged;
            OnHintChanged(Calibrator.Hint);
        }

        private void OnDisable()
        {
            if (Calibrator == null) return;
            Calibrator.HintChanged -= OnHintChanged;
            Calibrator.StateChanged -= OnStateChanged;
        }

        private void OnHintChanged(CalibrationHint hint)
        {
            if (Label != null) Label.text = MessageFor(hint);
            Debug.Log($"Calibration hint: {hint}");
        }

        private void OnStateChanged(CalibrationState previous, CalibrationState current)
        {
            Debug.Log($"Calibration: {previous} -> {current}");
        }

        /// <summary>Your app's wording for each hint.</summary>
        private static string MessageFor(CalibrationHint hint)
        {
            switch (hint)
            {
                case CalibrationHint.StepIntoView: return "Step into the play area.";
                case CalibrationHint.HeadsetTrackingLost: return "Tracking lost - is the room lit?";
                case CalibrationHint.FindingYou: return "Take a few steps.";
                case CalibrationHint.HoldStill: return "Hold still and look ahead.";
                case CalibrationHint.KeepWalking: return "Keep walking a little.";
                default: return string.Empty;
            }
        }
    }
}
