using System;
using UnityEngine;
using RR.Common.UI;
using RR.Game2D.Behaviour;
using RR.Game2D.Core.Managers;
namespace RR.Common.Prizes
{
///
/// Исполнитель приза «QR-код».
/// Показывает модальное окно с QR-кодами. Сами QR-коды получает через делегат —
/// не знает откуда они берутся.
///
public class QRCodePrize : PrizeBase
{
public override PrizeType Type => PrizeType.QRCode;
public override bool IsGiving => _isGiving;
private readonly Func _resolveModal;
private readonly Func _getQRCodes;
private bool _isGiving;
private QRCodeModal _activeModal;
/// Делегат для поиска модального окна в текущих сценах.
/// Делегат получения QR-текстур.
public QRCodePrize(Func resolveModal, Func getQRCodes)
{
_resolveModal = resolveModal;
_getQRCodes = getQRCodes;
}
public override void Execute(MonoBehaviour host, LevelPrizeConfig config)
{
Texture2D[] codes = _getQRCodes?.Invoke(config != null ? config.qrTier : QRTier.None);
if (codes == null || codes.Length == 0)
{
Debug.LogWarning("[QRCodePrize] Нет QR-кодов для отображения.");
InvokeResult(false);
return;
}
QRCodeModal modal = _resolveModal?.Invoke();
if (modal == null)
{
Debug.LogWarning("[QRCodePrize] QRCodeModal не найден на сцене.");
InvokeResult(false);
return;
}
_isGiving = true;
bool showFinalScore = GameManager.Instance != null && GameManager.Instance.IsCurrentLevelFinal();
float gameTime = GameManager.Instance != null ? GameManager.Instance.GetPendingOrCurrentWinGameTime() : 0f;
int finalScore = showFinalScore && ScoreSystem.Instance != null
? ScoreSystem.Instance.CalculatePreviewScore(gameTime, true)
: 0;
// Отписываемся от предыдущего, если было
if (_activeModal != null)
_activeModal.OnClosed -= OnModalClosed;
_activeModal = modal;
_activeModal.OnClosed += OnModalClosed;
_activeModal.ShowQRCodes(codes, config != null ? config.qrTier : QRTier.None, gameTime, finalScore, showFinalScore);
}
private void OnModalClosed()
{
if (_activeModal != null)
{
_activeModal.OnClosed -= OnModalClosed;
_activeModal = null;
}
_isGiving = false;
InvokeResult(true);
}
}
}