using System;
using UnityEngine;
using UnityEngine.UI;
using RR.Common.Managers;
using RR.Common.Prizes;
using RR.Game2D.Behaviour;
using RR.Game2D.Core.Managers;
namespace RR.Common.UI
{
///
/// Контроллер панели завершения игры.
/// Управляет отображением баннеров выигрыша и проигрыша.
///
public class GameEndUIController : MonoBehaviour
{
[Header("Баннеры")]
[SerializeField] private WinBannerController winBanner;
[SerializeField] private LoseBannerController loseBanner;
[Header("Фон")]
[SerializeField] private Image backgroundOverlay;
[SerializeField] private Color overlayColor = new Color(0, 0, 0, 100f/255f);
///
/// Событие, вызываемое при показе баннера выигрыша.
///
public event Action WinBannerShown;
///
/// Событие, вызываемое при показе баннера проигрыша.
///
public event Action LoseBannerShown;
///
/// Событие, вызываемое при закрытии любого баннера.
///
public event Action BannerDismissed;
private void OnEnable()
{
if (GameManager.Instance == null) return;
GameManager.Instance.GameWonEvent += OnGameWon;
GameManager.Instance.GameOverEvent += OnGameOver;
// Подписываемся на события закрытия баннеров
if (winBanner)
{
winBanner.WinBannerDismissed += OnBannerDismissed;
}
if (loseBanner)
{
loseBanner.ContinueClicked += OnBannerDismissed;
}
}
private void OnDisable()
{
if (GameManager.Instance == null) return;
GameManager.Instance.GameWonEvent -= OnGameWon;
GameManager.Instance.GameOverEvent -= OnGameOver;
// Отписываемся от событий закрытия баннеров
if (winBanner)
{
winBanner.WinBannerDismissed -= OnBannerDismissed;
}
if (loseBanner)
{
loseBanner.ContinueClicked -= OnBannerDismissed;
}
}
private void Awake()
{
// Изначально скрываем фон
if (backgroundOverlay)
{
backgroundOverlay.color = new Color(0, 0, 0, 0);
}
}
private void OnGameWon(float gameTime)
{
if (GameManager.Instance)
{
GameManager.Instance.SetPause(true);
}
ShowOverlay();
if (winBanner)
{
LevelPrizeConfig prizeConfig = PrizeSystemManager.Instance != null
? PrizeSystemManager.Instance.GetPrizeConfigForCurrentLevel()
: new LevelPrizeConfig();
bool showFinalScore = GameManager.Instance != null && GameManager.Instance.IsCurrentLevelFinal();
int finalScore = ScoreSystem.Instance != null ? ScoreSystem.Instance.FinalScore : 0;
winBanner.ShowWin(gameTime, prizeConfig, finalScore, showFinalScore);
}
WinBannerShown?.Invoke(gameTime);
}
private void OnGameOver(float gameTime)
{
if (GameManager.Instance)
{
GameManager.Instance.SetPause(true);
}
ShowOverlay();
if (loseBanner)
{
loseBanner.ShowLose(gameTime);
}
LoseBannerShown?.Invoke(gameTime);
}
private void ShowOverlay()
{
if (backgroundOverlay)
{
backgroundOverlay.color = overlayColor;
}
}
private void HideOverlay()
{
if (backgroundOverlay)
{
backgroundOverlay.color = new Color(0, 0, 0, 0);
}
}
private void OnBannerDismissed()
{
HideOverlay();
BannerDismissed?.Invoke();
}
}
}