using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using System.Collections.Generic;
///
/// PreMatchIntroUI — "3 vs 3" matchup splash + countdown before gameplay.
///
/// Flow:
/// 1. Show team info (displayDuration seconds)
/// 2. Countdown: 3 → 2 → 1 → FIGHT!
/// 3. Fade out, set IsComplete = true
/// 4. CashSystemManager waits for IsComplete before enabling combat
///
/// Assign portrait Image slots in the Inspector. The script loads character
/// sprites into those Image components by team index.
///
/// Press Space (or skipButton) to skip intro → fast countdown then complete.
///
/// Uses CanvasGroup to hide (NOT SetActive(false)) so the script stays alive.
///
public class PreMatchIntroUI : MonoBehaviour
{
[Header("Panel")]
[SerializeField] private GameObject rootPanel;
[Header("Player Team (Left) — Assign portrait Image slots")]
[SerializeField] private Image[] playerPortraits = new Image[3];
[SerializeField] private TextMeshProUGUI[] playerNames = new TextMeshProUGUI[3];
[Header("Enemy Team (Right) — Assign portrait Image slots")]
[SerializeField] private Image[] enemyPortraits = new Image[3];
[SerializeField] private TextMeshProUGUI[] enemyNames = new TextMeshProUGUI[3];
[Header("Centre")]
[SerializeField] private TextMeshProUGUI vsText;
[Header("Countdown")]
[Tooltip("Text element for countdown (3, 2, 1, FIGHT!)")]
[SerializeField] private TextMeshProUGUI countdownText;
[Header("Skip")]
[Tooltip("Optional skip button — user can also press Space")]
[SerializeField] private Button skipButton;
[Tooltip("Text shown at bottom: 'Press Space to Skip'")]
[SerializeField] private TextMeshProUGUI skipHintText;
[Header("Game Mode Info (optional)")]
[SerializeField] private TextMeshProUGUI modeTitle;
[SerializeField] private TextMeshProUGUI objectiveText;
[Header("Timing")]
[SerializeField] private float displayDuration = 3.5f;
[SerializeField] private float fadeInDuration = 0.4f;
[SerializeField] private float fadeOutDuration = 0.5f;
[SerializeField] private float countdownStepTime = 1f;
[Header("Audio")]
[SerializeField] private AudioClip introSFX;
[SerializeField] private AudioClip countdownBeepSFX;
[SerializeField] private AudioClip fightSFX;
[SerializeField] private float sfxVolume = 1f;
[Header("Colors")]
[SerializeField] private Color playerTeamColor = new Color(0.13f, 0.59f, 0.95f);
[SerializeField] private Color enemyTeamColor = new Color(0.96f, 0.26f, 0.21f);
[SerializeField] private Color countdownColor = Color.white;
[SerializeField] private Color fightColor = new Color(1f, 0.84f, 0f); // Gold
// runtime
private AudioSource _audio;
private CanvasGroup _canvasGroup;
private Coroutine _sequenceCoroutine;
private bool _skipRequested;
/// True once intro sequence (including countdown) is fully done.
public bool IsComplete { get; private set; }
private static PreMatchIntroUI _instance;
public static PreMatchIntroUI Instance => _instance;
///
/// Ensures a PreMatchIntroUI instance exists in the scene.
/// Finds inactive instances first, or creates a minimal one on the existing Canvas.
/// Call from CashSystemManager.Start() so the intro always works.
///
public static void EnsureExists()
{
if (_instance != null) return;
// 1. Try to find an inactive instance already in the scene
PreMatchIntroUI[] all = Resources.FindObjectsOfTypeAll();
foreach (var p in all)
{
// Skip assets (prefabs) — only want scene objects
if (p.gameObject.scene.name == null) continue;
// Activate parents top-down first, then the object itself
// (Awake only fires when the ENTIRE chain is active)
List chain = new List();
Transform t = p.transform;
while (t != null)
{
if (!t.gameObject.activeSelf)
chain.Add(t.gameObject);
t = t.parent;
}
// Activate top-down
for (int i = chain.Count - 1; i >= 0; i--)
chain[i].SetActive(true);
if (_instance != null)
{
Debug.Log("[PreMatchIntroUI] Found existing inactive panel, activated it");
return;
}
}
// 2. Create a minimal panel on an existing Canvas
Canvas canvas = Object.FindObjectOfType