using System; using System.Collections.Generic; using Bootstrap; using Core; using UnityEngine; using UnityEngine.InputSystem; namespace AppleHills.UI.CardSystem { /// /// Manages UI page transitions and maintains a stack of active pages. /// Pages are pushed onto a stack for navigation and popped when going back. /// public class UIPageController : MonoBehaviour { private static UIPageController _instance; public static UIPageController Instance => _instance; private Stack _pageStack = new Stack(); public UIPage CurrentPage => _pageStack.Count > 0 ? _pageStack.Peek() : null; // Event fired when the page stack changes public event Action OnPageChanged; private PlayerInput _playerInput; private InputAction _cancelAction; private void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); return; } _instance = this; // TODO: Handle generic "cancel" action // _playerInput = FindFirstObjectByType(); // if (_playerInput == null) // { // Logging.Warning("[UIPageController] No PlayerInput found in the scene. Cancel action might not work."); // } // else // { // // Get the Cancel action from the UI action map // _cancelAction = _playerInput.actions.FindAction("UI/Cancel"); // if (_cancelAction != null) // { // _cancelAction.performed += OnCancelActionPerformed; // } // else // { // Logging.Warning("[UIPageController] Cancel action not found in the input actions asset."); // } // } // Register for post-boot initialization BootCompletionService.RegisterInitAction(InitializePostBoot); } private void OnDestroy() { // Clean up event subscription when the controller is destroyed if (_cancelAction != null) { _cancelAction.performed -= OnCancelActionPerformed; } } private void OnCancelActionPerformed(InputAction.CallbackContext context) { if (_pageStack.Count > 0) { _pageStack.Peek().OnBackPressed(); } } private void InitializePostBoot() { // Initialize any dependencies that require other services to be ready Logging.Debug("[UIPageController] Post-boot initialization complete"); } /// /// Pushes a new page onto the stack, hiding the current page and showing the new one. /// public void PushPage(UIPage page) { if (page == null) return; // Hide current page if there is one if (_pageStack.Count > 0) { UIPage currentPage = _pageStack.Peek(); currentPage.TransitionOut(); } // Push and show new page _pageStack.Push(page); page.TransitionIn(); OnPageChanged?.Invoke(page); Logging.Debug($"[UIPageController] Pushed page: {page.PageName}"); } /// /// Pops the current page from the stack and shows the previous page. /// public void PopPage() { if (_pageStack.Count <= 0) return; // Hide and pop current page UIPage currentPage = _pageStack.Pop(); currentPage.TransitionOut(); // Show previous page if there is one if (_pageStack.Count > 0) { UIPage previousPage = _pageStack.Peek(); previousPage.TransitionIn(); OnPageChanged?.Invoke(previousPage); Logging.Debug($"[UIPageController] Popped to previous page: {previousPage.PageName}"); } else { OnPageChanged?.Invoke(null); Logging.Debug("[UIPageController] Popped last page, no pages left in stack"); } } /// /// Clears all pages from the stack. /// public void ClearStack() { if (_pageStack.Count <= 0) return; // Hide current page UIPage currentPage = _pageStack.Peek(); currentPage.TransitionOut(); // Clear stack _pageStack.Clear(); OnPageChanged?.Invoke(null); Logging.Debug("[UIPageController] Cleared page stack"); } } }