using System; using AppleHills.Data.CardSystem; using Core; using Data.CardSystem; using UnityEngine; using UnityEngine.UI; namespace AppleHills.UI.CardSystem { /// /// Main UI controller for the card album system. /// Manages the backpack icon and navigation between card system pages. /// public class CardAlbumUI : MonoBehaviour { [Header("UI References")] [SerializeField] private GameObject backpackIcon; [SerializeField] private UIPage mainMenuPage; [SerializeField] private UIPage albumViewPage; [SerializeField] private UIPage boosterOpeningPage; [Header("UI Elements")] [SerializeField] private Button backpackButton; [SerializeField] private Text boosterCountText; private UIPageController _pageController; private CardSystemManager _cardManager; private bool _isInitialized = false; private void Awake() { // Get or create a UI page controller _pageController = FindAnyObjectByType(); if (_pageController == null) { GameObject controllerObj = new GameObject("UIPageController"); controllerObj.transform.SetParent(transform); _pageController = controllerObj.AddComponent(); } // Initialize pages and hide them InitializePages(); // Set up backpack button if (backpackButton != null) { backpackButton.onClick.AddListener(OnBackpackButtonClicked); } // Initially show only the backpack icon ShowOnlyBackpackIcon(); } private void Start() { // Get card manager _cardManager = CardSystemManager.Instance; // Subscribe to events if (_cardManager != null) { _cardManager.OnBoosterCountChanged += UpdateBoosterCount; UpdateBoosterCount(_cardManager.GetBoosterPackCount()); } _isInitialized = true; } private void OnDestroy() { // Unsubscribe from events if (_cardManager != null) { _cardManager.OnBoosterCountChanged -= UpdateBoosterCount; } // Clean up button listeners if (backpackButton != null) { backpackButton.onClick.RemoveListener(OnBackpackButtonClicked); } } /// /// Initializes all UI pages and sets them up with proper callbacks /// private void InitializePages() { // Ensure all pages are inactive at start if (mainMenuPage != null) { mainMenuPage.gameObject.SetActive(false); } if (albumViewPage != null) { albumViewPage.gameObject.SetActive(false); } if (boosterOpeningPage != null) { boosterOpeningPage.gameObject.SetActive(false); } // Set up page changed callback if (_pageController != null) { _pageController.OnPageChanged += OnPageChanged; } } /// /// Handles click on the backpack icon /// private void OnBackpackButtonClicked() { // If no pages are open, push the main menu if (_pageController.CurrentPage == null) { _pageController.PushPage(mainMenuPage); } else if (_pageController.CurrentPage == mainMenuPage) { // If main menu is open, pop it _pageController.PopPage(); } } /// /// Handles changes to the current page /// private void OnPageChanged(UIPage newPage) { // Hide/show backpack icon based on current page if (newPage == null) { ShowOnlyBackpackIcon(); } else { backpackIcon.SetActive(false); } } /// /// Shows only the backpack icon, hiding all other UI elements /// private void ShowOnlyBackpackIcon() { backpackIcon.SetActive(true); } /// /// Opens the album view page /// public void OpenAlbumView() { _pageController.PushPage(albumViewPage); } /// /// Opens the booster opening page if boosters are available /// public void OpenBoosterPack() { if (_cardManager != null && _cardManager.GetBoosterPackCount() > 0) { _pageController.PushPage(boosterOpeningPage); } else { Logging.Debug("[CardAlbumUI] No booster packs available"); // TODO: Show "no boosters available" message } } /// /// Updates the booster count display /// private void UpdateBoosterCount(int count) { if (boosterCountText != null) { boosterCountText.text = count.ToString(); } } } }