using UnityEngine;
using EventsAppQuest.Core;

namespace EventsAppQuest.Presentation
{
    /// <summary>
    /// Controlador de navegação na apresentação Quest: expõe ações (próximo/anterior passo, hotspot, estado de cena, alternar modo, reiniciar).
    /// Input é fornecido por QuestPresentationInputAdapter (teclado no editor; estrutura pronta para input real do Quest).
    /// </summary>
    public class QuestPresentationNavigationController : MonoBehaviour
    {
        private QuestPresentationSession _session;

        /// <summary>Invocado após qualquer ação de navegação (para a UI atualizar).</summary>
        public System.Action OnNavigationChanged;

        public void Bind(QuestPresentationSession session)
        {
            _session = session;
        }

        public void NextStep()
        {
            if (_session?.GuidedSteps == null || _session.GuidedSteps.Count == 0) return;
            var total = _session.GuidedSteps.Count;
            _session.CurrentGuidedStepIndex = (_session.CurrentGuidedStepIndex + 1) % total;
            OnNavigationChanged?.Invoke();
        }

        public void PreviousStep()
        {
            if (_session?.GuidedSteps == null || _session.GuidedSteps.Count == 0) return;
            var total = _session.GuidedSteps.Count;
            _session.CurrentGuidedStepIndex = _session.CurrentGuidedStepIndex <= 0 ? total - 1 : _session.CurrentGuidedStepIndex - 1;
            OnNavigationChanged?.Invoke();
        }

        public void NextHotspot()
        {
            if (_session?.Hotspots == null || _session.Hotspots.Count == 0) return;
            var n = _session.Hotspots.Count;
            _session.CurrentHotspotIndex = _session.CurrentHotspotIndex < 0 ? 0 : (_session.CurrentHotspotIndex + 1) % n;
            OnNavigationChanged?.Invoke();
        }

        public void PreviousHotspot()
        {
            if (_session?.Hotspots == null || _session.Hotspots.Count == 0) return;
            var n = _session.Hotspots.Count;
            _session.CurrentHotspotIndex = _session.CurrentHotspotIndex <= 0 ? n - 1 : _session.CurrentHotspotIndex - 1;
            OnNavigationChanged?.Invoke();
        }

        public void NextSceneState()
        {
            if (_session?.SceneStates == null || _session.SceneStates.Count == 0) return;
            var n = _session.SceneStates.Count;
            _session.CurrentSceneStateIndex = (_session.CurrentSceneStateIndex + 1) % n;
            OnNavigationChanged?.Invoke();
        }

        public void PreviousSceneState()
        {
            if (_session?.SceneStates == null || _session.SceneStates.Count == 0) return;
            var n = _session.SceneStates.Count;
            _session.CurrentSceneStateIndex = _session.CurrentSceneStateIndex <= 0 ? n - 1 : _session.CurrentSceneStateIndex - 1;
            OnNavigationChanged?.Invoke();
        }

        public void GoToMainHotspot()
        {
            if (_session?.Hotspots == null || _session.Hotspots.Count == 0) return;
            _session.CurrentHotspotIndex = 0;
            OnNavigationChanged?.Invoke();
        }

        public void ToggleNavigationMode()
        {
            if (_session == null) return;
            var next = (int)_session.NavigationMode + 1;
            if (next > 2) next = 0;
            _session.NavigationMode = (QuestPresentationNavigationMode)next;
            OnNavigationChanged?.Invoke();
        }

        public void RestartPresentation()
        {
            if (_session == null) return;
            _session.CurrentGuidedStepIndex = 0;
            _session.CurrentHotspotIndex = _session.Hotspots != null && _session.Hotspots.Count > 0 ? 0 : -1;
            _session.CurrentSceneStateIndex = _session.SceneStates != null && _session.SceneStates.Count > 0 ? 0 : -1;
            OnNavigationChanged?.Invoke();
        }
    }
}
