using System;
using System.Collections;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
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 BoosterNotificationDot boosterNotificationDot;
[Header("Notification Settings")]
[SerializeField] private AudioSource notificationSound;
// Public property to access the backpack icon for animations
public GameObject BackpackIcon => backpackIcon;
private UIPageController _pageController;
private CardSystemManager _cardManager;
private bool _isInitialized = false;
private bool _hasUnseenCards = 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();
// Hide notification dot initially
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
}
private void Start()
{
// Get card manager
_cardManager = CardSystemManager.Instance;
// Subscribe to events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged += UpdateBoosterCount;
_cardManager.OnBoosterOpened += HandleBoosterOpened;
_cardManager.OnCardCollected += HandleCardCollected;
_cardManager.OnCardRarityUpgraded += HandleCardRarityUpgraded;
// Initialize UI with current values
UpdateBoosterCount(_cardManager.GetBoosterPackCount());
}
_isInitialized = true;
}
private void OnDestroy()
{
// Unsubscribe from events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged -= UpdateBoosterCount;
_cardManager.OnBoosterOpened -= HandleBoosterOpened;
_cardManager.OnCardCollected -= HandleCardCollected;
_cardManager.OnCardRarityUpgraded -= HandleCardRarityUpgraded;
}
// Clean up button listeners
if (backpackButton != null)
{
backpackButton.onClick.RemoveListener(OnBackpackButtonClicked);
}
// Unsubscribe from page controller events
if (_pageController != null)
{
_pageController.OnPageChanged -= OnPageChanged;
}
}
///
/// 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()
{
// Play button sound if available
if (notificationSound != null)
notificationSound.Play();
// If no pages are open, push the main menu
if (_pageController.CurrentPage == null)
{
_pageController.PushPage(mainMenuPage);
// Clear notification for unseen cards when opening menu
if (_hasUnseenCards)
{
_hasUnseenCards = false;
}
// Hide the backpack button when entering menu
if (backpackButton != null)
{
backpackButton.gameObject.SetActive(false);
}
}
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
{
if (backpackButton != null)
backpackButton.gameObject.SetActive(false);
}
// Update menu if it's the main menu page
if (newPage == mainMenuPage && mainMenuPage is CardMenuPage menuPage)
{
// Force UI refresh when returning to main menu
menuPage.RefreshUI();
}
}
///
/// Shows only the backpack icon, hiding all other UI elements
///
private void ShowOnlyBackpackIcon()
{
if (backpackButton != null)
{
backpackButton.gameObject.SetActive(true);
// Update notification visibility based on booster count
bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0;
// Show notification dot if there are boosters or unseen cards
if (boosterNotificationDot != null)
{
boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards);
}
}
}
///
/// 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");
}
}
///
/// Updates the booster count display
///
private void UpdateBoosterCount(int count)
{
if (boosterNotificationDot != null)
{
boosterNotificationDot.SetCount(count);
// Animate the notification dot for feedback
boosterNotificationDot.transform.localScale = Vector3.one * 1.2f;
Tween.LocalScale(boosterNotificationDot.transform, Vector3.one, 0.3f, 0f);
// Update visibility based on count
UpdateBoosterVisibility();
}
}
///
/// Updates the visibility of the booster notification dot based on current state
///
private void UpdateBoosterVisibility()
{
if (boosterNotificationDot != null)
{
// Show dot if there are boosters or unseen cards
bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0;
boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards);
}
}
///
/// Handles event when a booster pack is opened
///
private void HandleBoosterOpened(System.Collections.Generic.List cards)
{
Logging.Debug($"[CardAlbumUI] Booster opened with {cards.Count} cards");
// The booster opening page handles the UI for this event
}
///
/// Handles event when a new card is collected
///
private void HandleCardCollected(CardData card)
{
// If we're not in the album view or booster opening view,
// show a notification dot on the backpack
if (_pageController.CurrentPage != albumViewPage &&
_pageController.CurrentPage != boosterOpeningPage)
{
_hasUnseenCards = true;
UpdateBoosterVisibility();
}
Logging.Debug($"[CardAlbumUI] New card collected: {card.Name}");
}
///
/// Handles event when a card is upgraded to a higher rarity
///
private void HandleCardRarityUpgraded(CardData card)
{
// Just log the upgrade event without showing a notification
Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}");
}
}
}