using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using UI.Core;
using UI.DragAndDrop.Core;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
namespace UI.CardSystem
{
///
/// UI page for viewing the player's card collection in an album.
/// Manages booster pack button visibility and opening flow.
///
public class AlbumViewPage : UIPage
{
[Header("UI References")]
[SerializeField] private CanvasGroup canvasGroup;
[SerializeField] private Button exitButton;
[SerializeField] private BookCurlPro.BookPro book;
[Header("Zone Navigation")]
[SerializeField] private Transform tabContainer; // Container holding all BookTabButton children
private BookTabButton[] _zoneTabs; // Discovered zone tab buttons
[Header("Album Card Reveal")]
[SerializeField] private SlotContainer bottomRightSlots;
[FormerlySerializedAs("albumCardPlacementPrefab")]
[SerializeField] private GameObject cardPrefab; // New Card prefab for placement
[Header("Card Enlarge System")]
[SerializeField] private GameObject cardEnlargedBackdrop; // Backdrop to block interactions
[SerializeField] private Transform cardEnlargedContainer; // Container for enlarged cards (sits above backdrop)
[Header("Booster Pack UI")]
[SerializeField] private GameObject[] boosterPackButtons;
[SerializeField] private BoosterOpeningPage boosterOpeningPage;
private Input.InputMode _previousInputMode;
private List _pendingCornerCards = new List();
private const int MaxPendingCorner = 3;
// Pending card placement coordination
private StateMachine.Card _pendingPlacementCard;
private bool _waitingForPageFlip;
private bool _cardDragReleased;
internal override void OnManagedStart()
{
// Discover zone tabs from container
DiscoverZoneTabs();
// Make sure we have a CanvasGroup for transitions
if (canvasGroup == null)
canvasGroup = GetComponent();
if (canvasGroup == null)
canvasGroup = gameObject.AddComponent();
// Hide backdrop initially
if (cardEnlargedBackdrop != null)
{
cardEnlargedBackdrop.SetActive(false);
}
// Set up exit button
if (exitButton != null)
{
exitButton.onClick.AddListener(OnExitButtonClicked);
}
// Set up booster pack button listeners
SetupBoosterButtonListeners();
// Subscribe to book page flip events
if (book != null)
{
book.OnFlip.AddListener(OnPageFlipped);
Logging.Debug("[AlbumViewPage] Subscribed to book.OnFlip event");
}
else
{
Logging.Warning("[AlbumViewPage] Book reference is null, cannot subscribe to OnFlip event!");
}
// Subscribe to CardSystemManager events (managers are guaranteed to be initialized)
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnBoosterCountChanged += OnBoosterCountChanged;
// NOTE: OnPendingCardAdded is subscribed in TransitionIn, not here
// This prevents spawning cards when page is not active
// Update initial button visibility
int initialCount = CardSystemManager.Instance.GetBoosterPackCount();
UpdateBoosterButtons(initialCount);
}
// UI pages should start disabled
gameObject.SetActive(false);
}
///
/// Discover all BookTabButton components from the tab container
///
private void DiscoverZoneTabs()
{
if (tabContainer == null)
{
Debug.LogError("[AlbumViewPage] Tab container is not assigned! Cannot discover zone tabs.");
_zoneTabs = new BookTabButton[0];
return;
}
// Get all BookTabButton components from children
_zoneTabs = tabContainer.GetComponentsInChildren(includeInactive: false);
if (_zoneTabs == null || _zoneTabs.Length == 0)
{
Logging.Warning($"[AlbumViewPage] No BookTabButton components found in tab container '{tabContainer.name}'!");
_zoneTabs = new BookTabButton[0];
}
else
{
Logging.Debug($"[AlbumViewPage] Discovered {_zoneTabs.Length} zone tabs from container '{tabContainer.name}'");
foreach (var tab in _zoneTabs)
{
Logging.Debug($" - Tab: {tab.name}, Zone: {tab.Zone}, TargetPage: {tab.TargetPage}");
}
}
}
private void SetupBoosterButtonListeners()
{
if (boosterPackButtons == null) return;
for (int i = 0; i < boosterPackButtons.Length; i++)
{
if (boosterPackButtons[i] == null) continue;
Button button = boosterPackButtons[i].GetComponent();
if (button != null)
{
button.onClick.AddListener(OnBoosterButtonClicked);
}
}
}
internal override void OnManagedDestroy()
{
// Unsubscribe from book events
if (book != null)
{
book.OnFlip.RemoveListener(OnPageFlipped);
}
// Unsubscribe from CardSystemManager
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnBoosterCountChanged -= OnBoosterCountChanged;
}
// Clean up exit button
if (exitButton != null)
{
exitButton.onClick.RemoveListener(OnExitButtonClicked);
}
// Clean up booster button listeners
if (boosterPackButtons != null)
{
foreach (var buttonObj in boosterPackButtons)
{
if (buttonObj == null) continue;
Button button = buttonObj.GetComponent();
if (button != null)
{
button.onClick.RemoveListener(OnBoosterButtonClicked);
}
}
}
// Clean up pending corner cards
CleanupPendingCornerCards();
}
private void OnExitButtonClicked()
{
if (book != null && book.CurrentPaper != 1)
{
// Not on page 0, flip to page 0 first
BookCurlPro.AutoFlip autoFlip = book.GetComponent();
if (autoFlip == null)
{
autoFlip = book.gameObject.AddComponent();
}
autoFlip.enabled = true;
autoFlip.StartFlipping(1);
}
else
{
// Already on page 0 or no book reference, exit
// Restore input mode before popping
if (Input.InputManager.Instance != null)
{
Input.InputManager.Instance.SetInputMode(_previousInputMode);
Logging.Debug($"[AlbumViewPage] Restored input mode to {_previousInputMode} on exit");
}
if (UIPageController.Instance != null)
{
UIPageController.Instance.PopPage();
}
}
}
private void OnBoosterCountChanged(int newCount)
{
UpdateBoosterButtons(newCount);
}
private void UpdateBoosterButtons(int boosterCount)
{
if (boosterPackButtons == null || boosterPackButtons.Length == 0) return;
int visibleCount = Mathf.Min(boosterCount, boosterPackButtons.Length);
for (int i = 0; i < boosterPackButtons.Length; i++)
{
if (boosterPackButtons[i] != null)
{
boosterPackButtons[i].SetActive(i < visibleCount);
}
}
}
private void OnBoosterButtonClicked()
{
if (boosterOpeningPage != null && UIPageController.Instance != null)
{
// Pass current booster count to the opening page
int boosterCount = CardSystemManager.Instance?.GetBoosterPackCount() ?? 0;
boosterOpeningPage.SetAvailableBoosterCount(boosterCount);
UIPageController.Instance.PushPage(boosterOpeningPage);
}
}
public override void TransitionIn()
{
// Only store and switch input mode if this is the first time entering
if (Input.InputManager.Instance != null)
{
// Store the current input mode before switching
_previousInputMode = Input.InputMode.GameAndUI;
Input.InputManager.Instance.SetInputMode(Input.InputMode.UI);
Logging.Debug("[AlbumViewPage] Switched to UI-only input mode on first entry");
}
// Only spawn pending cards if we're already on an album page (not the menu)
if (IsInAlbumProper())
{
Logging.Debug("[AlbumViewPage] Opening directly to album page - spawning cards immediately");
SpawnPendingCornerCards();
}
else
{
Logging.Debug("[AlbumViewPage] Opening to menu page - cards will spawn when entering album");
}
base.TransitionIn();
}
public override void TransitionOut()
{
// Clean up active pending cards to prevent duplicates on next opening
CleanupPendingCornerCards();
// Don't restore input mode here - only restore when actually exiting (in OnExitButtonClicked)
base.TransitionOut();
}
protected override void DoTransitionIn(System.Action onComplete)
{
// Simple fade in animation
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete, obeyTimescale: false);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
protected override void DoTransitionOut(System.Action onComplete)
{
// Clean up any enlarged card state before closing
CleanupEnlargedCardState();
// Simple fade out animation
if (canvasGroup != null)
{
Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete, obeyTimescale: false);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
///
/// Clean up enlarged card state when closing the album
///
private void CleanupEnlargedCardState()
{
// Hide backdrop if visible
if (cardEnlargedBackdrop != null && cardEnlargedBackdrop.activeSelf)
{
cardEnlargedBackdrop.SetActive(false);
}
// If there's an enlarged card in the container, return it to its slot
if (cardEnlargedContainer != null && cardEnlargedContainer.childCount > 0)
{
for (int i = cardEnlargedContainer.childCount - 1; i >= 0; i--)
{
Transform cardTransform = cardEnlargedContainer.GetChild(i);
var card = cardTransform.GetComponent();
var state = cardTransform.GetComponentInChildren(true);
if (card != null && state != null)
{
Transform originalParent = state.GetOriginalParent();
if (originalParent != null)
{
cardTransform.SetParent(originalParent, true);
cardTransform.localPosition = state.GetOriginalLocalPosition();
cardTransform.localRotation = state.GetOriginalLocalRotation();
}
}
}
}
}
///
/// Check if we're currently viewing the album proper (not the menu page)
///
private bool IsInAlbumProper()
{
if (book == null)
{
Logging.Warning("[AlbumViewPage] Book reference is null in IsInAlbumProper check");
return false;
}
// Page 1 is the menu/cover, page 2+ are album pages with card slots
bool inAlbum = book.CurrentPaper > 1;
return inAlbum;
}
///
/// Called when book page flips - show/hide pending cards based on whether we're in the album proper
///
private void OnPageFlipped()
{
bool isInAlbum = IsInAlbumProper();
if (isInAlbum && _pendingCornerCards.Count == 0)
{
// Entering album proper and no cards spawned yet - spawn them with animation
Logging.Debug("[AlbumViewPage] Entering album proper - spawning pending cards with animation");
SpawnPendingCornerCards();
}
else if (!isInAlbum && _pendingCornerCards.Count > 0)
{
// Returning to menu page - cleanup cards
Logging.Debug("[AlbumViewPage] Returning to menu page - cleaning up pending cards");
CleanupPendingCornerCards();
}
else
{
Logging.Debug($"[AlbumViewPage] Page flipped but no card state change needed (already in correct state)");
}
}
#region Card Enlarge System (Album Slots)
///
/// Subscribe to a placed card's enlarged state events to manage backdrop and reparenting.
/// Called by AlbumCardSlot when it spawns an owned card in a slot.
///
public void RegisterCardInAlbum(StateMachine.Card card)
{
if (card == null) return;
var enlargeState = card.GetStateComponent("AlbumEnlargedState");
if (enlargeState != null)
{
enlargeState.OnEnlargeRequested += OnCardEnlargeRequested;
enlargeState.OnShrinkRequested += OnCardShrinkRequested;
}
}
public void UnregisterCardInAlbum(StateMachine.Card card)
{
if (card == null) return;
var enlargeState = card.GetStateComponent("AlbumEnlargedState");
if (enlargeState != null)
{
enlargeState.OnEnlargeRequested -= OnCardEnlargeRequested;
enlargeState.OnShrinkRequested -= OnCardShrinkRequested;
}
}
private void OnCardEnlargeRequested(StateMachine.States.CardAlbumEnlargedState state)
{
if (state == null) return;
// Show backdrop
if (cardEnlargedBackdrop != null)
{
cardEnlargedBackdrop.SetActive(true);
}
// Reparent card root to enlarged container preserving world transform
if (cardEnlargedContainer != null)
{
var ctx = state.GetComponentInParent();
if (ctx != null)
{
ctx.RootTransform.SetParent(cardEnlargedContainer, true);
ctx.RootTransform.SetAsLastSibling();
}
}
}
private void OnCardShrinkRequested(StateMachine.States.CardAlbumEnlargedState state)
{
if (state == null) return;
// Hide backdrop
if (cardEnlargedBackdrop != null)
{
cardEnlargedBackdrop.SetActive(false);
}
// Reparent back to original parent and restore local transform
var ctx = state.GetComponentInParent();
if (ctx != null)
{
Transform originalParent = state.GetOriginalParent();
if (originalParent != null)
{
ctx.RootTransform.SetParent(originalParent, true);
ctx.RootTransform.localPosition = state.GetOriginalLocalPosition();
ctx.RootTransform.localRotation = state.GetOriginalLocalRotation();
}
}
}
#endregion
#region New Pending Corner Card System
///
/// Find a slot by its SlotIndex property
///
private DraggableSlot FindSlotByIndex(int slotIndex)
{
if (bottomRightSlots == null)
return null;
foreach (var slot in bottomRightSlots.Slots)
{
if (slot.SlotIndex == slotIndex)
{
return slot;
}
}
return null;
}
public void SpawnPendingCornerCards()
{
if (cardPrefab == null || bottomRightSlots == null) return;
CleanupPendingCornerCards();
// Get unique pending cards
var uniquePending = GetUniquePendingCards();
if (uniquePending.Count == 0)
{
Logging.Debug("[AlbumViewPage] No pending cards to spawn");
return;
}
// Spawn min(unique count, MaxPendingCorner)
int spawnCount = Mathf.Min(uniquePending.Count, MaxPendingCorner);
Logging.Debug($"[AlbumViewPage] Spawning {spawnCount} pending corner cards (out of {uniquePending.Count} unique pending)");
for (int i = 0; i < spawnCount; i++)
{
var slot = FindSlotByIndex(i);
if (slot == null) break;
GameObject cardObj = Instantiate(cardPrefab, bottomRightSlots.transform);
var card = cardObj.GetComponent();
if (card != null)
{
card.SetupForAlbumPending();
card.AssignToSlot(slot, true);
// Subscribe to both drag events
card.Context.OnDragStarted += OnCardDragStarted;
card.Context.OnDragEnded += OnCardDragEnded;
_pendingCornerCards.Add(card);
}
else
{
Destroy(cardObj);
}
}
}
///
/// Get unique pending cards (one per definition+rarity combo)
///
private List GetUniquePendingCards()
{
if (CardSystemManager.Instance == null) return new List();
var pending = CardSystemManager.Instance.GetPendingRevealCards();
// Group by definition+rarity, take first of each group
var uniqueDict = new Dictionary();
int duplicateCount = 0;
foreach (var card in pending)
{
string key = $"{card.DefinitionId}_{card.Rarity}";
if (!uniqueDict.ContainsKey(key))
{
uniqueDict[key] = card;
}
else
{
duplicateCount++;
}
}
if (duplicateCount > 0)
{
Logging.Warning($"[AlbumViewPage] Found {duplicateCount} duplicate pending cards (same definition+rarity combo)");
}
return new List(uniqueDict.Values);
}
private void CleanupPendingCornerCards()
{
foreach (var c in _pendingCornerCards)
{
if (c != null)
{
if (c.Context != null)
{
c.Context.OnDragStarted -= OnCardDragStarted;
c.Context.OnDragEnded -= OnCardDragEnded;
}
Destroy(c.gameObject);
}
}
_pendingCornerCards.Clear();
}
private void OnCardDragStarted(StateMachine.CardContext context)
{
if (context == null) return;
// Only handle if in PendingFaceDownState
var card = context.GetComponent();
if (card == null) return;
string stateName = card.GetCurrentStateName();
Logging.Debug($"[AlbumViewPage] OnCardDragStarted - Card state: {stateName}");
if (stateName != "PendingFaceDownState") return;
// Select smart pending card data
var selected = SelectSmartPendingCard();
if (selected == null)
{
Logging.Warning("[AlbumViewPage] No pending card data available!");
return; // no pending data
}
Logging.Debug($"[AlbumViewPage] Selected card: {selected.Name} ({selected.DefinitionId}), Zone: {selected.Zone}");
context.SetupCard(selected);
// Find target page based on card's zone using BookTabButtons
int targetPage = FindPageForZone(selected.Zone);
Logging.Debug($"[AlbumViewPage] Target page for zone {selected.Zone}: {targetPage}");
if (targetPage >= 0)
{
// Always flip to the zone's page (even if already there, for consistency)
Logging.Debug($"[AlbumViewPage] Flipping to page {targetPage} for zone {selected.Zone}");
_waitingForPageFlip = true;
_cardDragReleased = false;
_pendingPlacementCard = card;
NavigateToAlbumPage(targetPage, OnPageFlipComplete);
}
else
{
// No valid page found for zone - don't wait for flip
Logging.Warning($"[AlbumViewPage] No BookTabButton found for zone {selected.Zone}");
_waitingForPageFlip = false;
_cardDragReleased = false;
_pendingPlacementCard = card;
}
}
private void OnCardDragEnded(StateMachine.CardContext context)
{
if (context == null) return;
var card = context.GetComponent();
if (card == null) return;
// Only handle if this is the pending placement card
if (card != _pendingPlacementCard) return;
Logging.Debug($"[AlbumViewPage] Card drag released for: {card.CardData?.Name}");
// Mark drag as released - don't capture slot yet (pages may still be flipping)
_cardDragReleased = true;
// Try to place card (will check if flip is also complete)
TryPlaceCard();
}
private void OnPageFlipComplete()
{
Logging.Debug("[AlbumViewPage] Page flip complete");
_waitingForPageFlip = false;
// Try to place card (will check if drag is also released)
TryPlaceCard();
}
private void TryPlaceCard()
{
// Check if BOTH conditions are met:
// 1. Card has been drag-released
// 2. Page flip is complete (or wasn't needed)
if (_cardDragReleased && !_waitingForPageFlip)
{
if (_pendingPlacementCard == null || _pendingPlacementCard.CardData == null)
{
Logging.Warning("[AlbumViewPage] TryPlaceCard - No pending card or card data");
ResetPlacementState();
return;
}
// Find the correct slot for this card (AFTER pages have flipped)
var targetSlot = FindTargetSlotForCard(_pendingPlacementCard.CardData);
if (targetSlot == null)
{
Logging.Warning($"[AlbumViewPage] No slot found for card {_pendingPlacementCard.CardData.DefinitionId}");
// TODO: Return card to corner
ResetPlacementState();
return;
}
// Both conditions met and slot found - perform placement
PlaceCardInSlot(_pendingPlacementCard, targetSlot);
}
}
///
/// Find the AlbumCardSlot that accepts this card based on DefinitionId
///
private AlbumCardSlot FindTargetSlotForCard(CardData cardData)
{
if (cardData == null) return null;
var allSlots = FindObjectsByType(FindObjectsSortMode.None);
foreach (var slot in allSlots)
{
if (slot.TargetCardDefinition != null &&
slot.TargetCardDefinition.Id == cardData.DefinitionId)
{
Logging.Debug($"[AlbumViewPage] Found target slot for {cardData.DefinitionId}, slot: {slot}");
return slot;
}
}
return null;
}
private void PlaceCardInSlot(StateMachine.Card card, AlbumCardSlot slot)
{
if (card == null || slot == null) return;
Logging.Debug($"[AlbumViewPage] Placing card '{card.CardData?.Name}' in slot - starting tween");
// Reparent to slot immediately, keeping world position (card stays visible where it is)
card.transform.SetParent(slot.transform, true);
// Tween local position and scale simultaneously
float tweenDuration = 0.4f;
// Tween position to center of slot
Tween.LocalPosition(card.transform, Vector3.zero, tweenDuration, 0f, Tween.EaseOutBack);
// Tween scale to normal (same duration and easing)
Tween.LocalScale(card.transform, Vector3.one, tweenDuration, 0f, Tween.EaseOutBack);
// Tween rotation to identity (use this for the completion callback since all tweens are synchronized)
Tween.LocalRotation(card.transform, Quaternion.identity, tweenDuration, 0f, Tween.EaseOutBack,
completeCallback: () =>
{
// After tween completes, finalize
card.transform.localPosition = Vector3.zero;
card.transform.localRotation = Quaternion.identity;
// Resize to match slot
RectTransform cardRect = card.transform as RectTransform;
RectTransform slotRect = slot.transform as RectTransform;
if (cardRect != null && slotRect != null)
{
float targetHeight = slotRect.rect.height;
cardRect.sizeDelta = new Vector2(cardRect.sizeDelta.x, targetHeight);
}
// Transition to placed state
var placedState = card.GetStateComponent("PlacedInSlotState");
if (placedState != null)
{
placedState.SetParentSlot(slot);
}
card.ChangeState("PlacedInSlotState");
// Assign card to slot (so slot remembers it has a card)
slot.AssignCard(card);
// Mark as placed in inventory
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.MarkCardAsPlaced(card.CardData);
}
// Unsubscribe from drag events - card is now placed, shouldn't respond to future drags
if (card.Context != null)
{
card.Context.OnDragStarted -= OnCardDragStarted;
card.Context.OnDragEnded -= OnCardDragEnded;
}
// Remove from pending corner cards list
_pendingCornerCards.Remove(card);
// Register with album page for enlarge system
RegisterCardInAlbum(card);
Logging.Debug($"[AlbumViewPage] Card placement complete and unsubscribed from drag events");
// Reset state for next card
ResetPlacementState();
});
}
private void ResetPlacementState()
{
_pendingPlacementCard = null;
_waitingForPageFlip = false;
_cardDragReleased = false;
}
private CardData SelectSmartPendingCard()
{
if (CardSystemManager.Instance == null) return null;
var pending = CardSystemManager.Instance.GetPendingRevealCards();
if (pending.Count == 0) return null;
// Try current page match
var pageDefs = GetDefinitionsOnCurrentPage();
var match = pending.Find(c => pageDefs.Contains(c.DefinitionId));
if (match != null) return match;
// Fallback random
int idx = Random.Range(0, pending.Count);
return pending[idx];
}
///
/// Find the target page for a card zone using BookTabButtons
///
private int FindPageForZone(CardZone zone)
{
if (_zoneTabs == null || _zoneTabs.Length == 0)
{
Logging.Warning("[AlbumViewPage] No zone tabs discovered!");
return -1;
}
foreach (var tab in _zoneTabs)
{
if (tab.Zone == zone)
{
return tab.TargetPage;
}
}
Logging.Warning($"[AlbumViewPage] No BookTabButton found for zone {zone}");
return -1;
}
private List GetDefinitionsOnCurrentPage()
{
var result = new List();
if (book == null) return result;
int currentPage = book.CurrentPaper;
// Find all AlbumCardSlot in scene
var allSlots = FindObjectsByType(FindObjectsSortMode.None);
foreach (var slot in allSlots)
{
if (IsSlotOnPage(slot.transform, currentPage))
{
if (slot.TargetCardDefinition != null && !string.IsNullOrEmpty(slot.TargetCardDefinition.Id))
{
result.Add(slot.TargetCardDefinition.Id);
}
}
}
return result;
}
private bool IsSlotOnPage(Transform slotTransform, int pageIndex)
{
if (book == null || book.papers == null || pageIndex < 0 || pageIndex >= book.papers.Length)
return false;
var paper = book.papers[pageIndex];
if (paper == null) return false;
// Check if slotTransform parent hierarchy contains paper.Front or paper.Back
Transform current = slotTransform;
while (current != null)
{
if ((paper.Front != null && current.gameObject == paper.Front) ||
(paper.Back != null && current.gameObject == paper.Back))
return true;
current = current.parent;
}
return false;
}
private void NavigateToAlbumPage(int pageIndex, UnityEngine.Events.UnityAction onComplete = null)
{
if (book == null || pageIndex < 0) return;
// Get or add AutoFlip component
BookCurlPro.AutoFlip autoFlip = book.GetComponent();
if (autoFlip == null)
{
autoFlip = book.gameObject.AddComponent();
}
// Start flipping to target page with callback
autoFlip.enabled = true;
if (onComplete != null)
{
autoFlip.StartFlipping(pageIndex, onComplete);
}
else
{
autoFlip.StartFlipping(pageIndex);
}
}
#endregion
}
}