using System.Collections;
using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AppleHills.UI.CardSystem
{
///
/// UI page for opening booster packs and displaying the cards obtained.
///
public class BoosterOpeningPage : UIPage
{
[Header("UI Elements")]
[SerializeField] private GameObject boosterPackObject;
[SerializeField] private RectTransform cardRevealContainer; // This should have a HorizontalLayoutGroup component
[SerializeField] private GameObject cardPrefab;
[SerializeField] private GameObject cardBackPrefab;
[SerializeField] private Button openBoosterButton;
[SerializeField] private Button continueButton;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Navigation")]
[SerializeField] private Button backButton;
[Header("Animation Settings")]
[SerializeField] private float cardRevealDelay = 0.3f;
[SerializeField] private float cardMoveToBackpackDelay = 0.8f;
[SerializeField] private float flipAnimationDuration = 0.5f;
// State tracking
private enum OpeningState
{
BoosterReady,
CardBacksVisible,
CardsRevealing,
CardsRevealed,
MovingToBackpack,
Completed
}
private OpeningState _currentState = OpeningState.BoosterReady;
private List _revealedCards = new List();
private List _cardBacks = new List();
private List _boosterCards = new List();
private int _revealedCardCount = 0;
private CardSystemManager _cardManager;
private CardAlbumUI _cardAlbumUI;
private Coroutine _moveToBackpackCoroutine;
private void Awake()
{
_cardManager = CardSystemManager.Instance;
_cardAlbumUI = FindObjectOfType();
// Set up button listeners
if (openBoosterButton != null)
{
openBoosterButton.onClick.AddListener(OnOpenBoosterClicked);
}
if (continueButton != null)
{
continueButton.onClick.AddListener(OnContinueClicked);
continueButton.gameObject.SetActive(false);
}
// Set up back button
if (backButton != null)
{
backButton.onClick.AddListener(OnBackButtonClicked);
}
// Make sure we have a CanvasGroup for transitions
if (canvasGroup == null)
canvasGroup = GetComponent();
if (canvasGroup == null)
canvasGroup = gameObject.AddComponent();
}
private void OnDestroy()
{
// Clean up button listeners
if (openBoosterButton != null)
{
openBoosterButton.onClick.RemoveListener(OnOpenBoosterClicked);
}
if (continueButton != null)
{
continueButton.onClick.RemoveListener(OnContinueClicked);
}
if (backButton != null)
{
backButton.onClick.RemoveListener(OnBackButtonClicked);
}
// Stop any running coroutines
if (_moveToBackpackCoroutine != null)
StopCoroutine(_moveToBackpackCoroutine);
}
///
/// Handles click on the back button
///
private void OnBackButtonClicked()
{
// Don't allow going back during animations or card reveals
if (_currentState == OpeningState.CardsRevealing ||
_currentState == OpeningState.MovingToBackpack)
{
return;
}
// Use the UIPageController to go back to the previous page
UIPageController pageController = UIPageController.Instance;
if (pageController != null)
{
pageController.PopPage();
}
}
///
/// Resets the page to its initial state when it becomes active
///
public override void TransitionIn()
{
base.TransitionIn();
ResetState();
}
///
/// Resets the state of the booster opening process
///
private void ResetState()
{
// Clear any previously revealed cards
foreach (var card in _revealedCards)
{
if (card != null && card.gameObject != null)
Destroy(card.gameObject);
}
_revealedCards.Clear();
// Clear card backs
foreach (var cardBack in _cardBacks)
{
if (cardBack != null)
Destroy(cardBack);
}
_cardBacks.Clear();
// Reset state
_currentState = OpeningState.BoosterReady;
_revealedCardCount = 0;
// Show booster pack, show open button, hide continue button
if (boosterPackObject != null)
{
boosterPackObject.SetActive(true);
}
if (openBoosterButton != null)
{
openBoosterButton.gameObject.SetActive(true);
}
if (continueButton != null)
{
continueButton.gameObject.SetActive(false);
}
// Make back button visible
if (backButton != null)
{
backButton.gameObject.SetActive(true);
}
}
///
/// Handles click on the booster pack to open it
///
private void OnOpenBoosterClicked()
{
if (_currentState != OpeningState.BoosterReady) return;
_currentState = OpeningState.CardBacksVisible;
// Open the booster pack and get the cards
_boosterCards = _cardManager.OpenBoosterPack();
if (_boosterCards.Count > 0)
{
// Hide the booster pack and open button
if (boosterPackObject != null)
{
// Animate the booster pack opening
Tween.LocalScale(boosterPackObject.transform, Vector3.zero, 0.3f, 0f, Tween.EaseInBack, Tween.LoopType.None, null, () => {
boosterPackObject.SetActive(false);
});
}
if (openBoosterButton != null)
{
openBoosterButton.gameObject.SetActive(false);
}
// Show card backs first
StartCoroutine(ShowCardBacks());
}
else
{
Logging.Warning("[BoosterOpeningPage] No cards were obtained from the booster pack.");
UIPageController.Instance.PopPage();
}
}
///
/// Shows card backs in the reveal positions
///
private IEnumerator ShowCardBacks()
{
// Wait a short delay before showing card backs
yield return new WaitForSeconds(0.5f);
// Create card backs for each position (usually 3)
for (int i = 0; i < Mathf.Min(_boosterCards.Count, cardRevealContainer.childCount); i++)
{
if (cardBackPrefab == null || cardRevealContainer.GetChild(i) == null) continue;
// Instantiate card back object at the correct position
GameObject cardBackObj = Instantiate(cardBackPrefab, cardRevealContainer.GetChild(i));
cardBackObj.transform.localPosition = Vector3.zero;
_cardBacks.Add(cardBackObj);
// Store the index for later reference when clicked
int cardIndex = i;
// Add click handler to the card back
Button cardBackButton = cardBackObj.GetComponent