using System.Collections; using TMPro; using UnityEngine; namespace BulletHellTemplate { /// /// Manages coupon redemption UI, including validating coupon codes and rewarding the player. /// public class UICoupon : MonoBehaviour { [Header("UI Components")] [Tooltip("Input field for entering the coupon code.")] public TMP_InputField couponInput; [Tooltip("UI text element to display the reward information.")] public TextMeshProUGUI rewardInfo; [Tooltip("UI text element to display the result message.")] public TextMeshProUGUI resultMessage; [Header("Translations")] [Tooltip("Fallback message when the coupon was already used.")] public string couponAlreadyUsed = "Coupon already used."; [Tooltip("Translations for 'Coupon already used'.")] public NameTranslatedByLanguage[] couponAlreadyUsedTranslated; [Tooltip("Fallback message for successful coupon claim.")] public string couponClaimedSuccess = "Coupon claimed successfully!"; [Tooltip("Translations for 'Coupon claimed successfully!'.")] public NameTranslatedByLanguage[] couponClaimedSuccessTranslated; [Tooltip("Fallback message for invalid coupon code.")] public string couponInvalidCode = "Invalid coupon code."; [Tooltip("Translations for 'Invalid coupon code.'.")] public NameTranslatedByLanguage[] couponInvalidCodeTranslated; [Tooltip("Fallback format for the reward message: 'You received {0} {1}!'")] public string couponRewardFormat = "You received {0} {1}!"; [Tooltip("Translations for the coupon reward format.")] public NameTranslatedByLanguage[] couponRewardFormatTranslated; private string currentLang; /// /// Subscribes to language change events. /// private void Awake() { LanguageManager.LanguageManager.onLanguageChanged += UpdateText; } /// /// Unsubscribes from language change events on destroy. /// private void OnDestroy() { LanguageManager.LanguageManager.onLanguageChanged -= UpdateText; } /// /// Initializes current language when the object is enabled. /// private void OnEnable() { currentLang = LanguageManager.LanguageManager.Instance.GetCurrentLanguage(); } /// /// Updates the current language whenever LanguageManager triggers a language change. /// private void UpdateText() { currentLang = LanguageManager.LanguageManager.Instance.GetCurrentLanguage(); } /// /// Opens the coupon menu, resetting input fields and messages. /// public void OnOpenCouponMenu() { couponInput.text = string.Empty; rewardInfo.text = string.Empty; resultMessage.text = string.Empty; } /// /// Called when the user clicks the "Claim Coupon" button. /// Verifies the coupon and processes the reward if valid. /// public void OnClickClaimCoupon() { string enteredCode = couponInput.text.Trim(); // Check if the coupon code exists CouponItem validCoupon = null; foreach (var coupon in GameInstance.Singleton.couponData) { if (coupon.couponCode.Equals(enteredCode, System.StringComparison.OrdinalIgnoreCase)) { validCoupon = coupon; break; } } if (validCoupon != null) { // Check if the coupon has already been used if (PlayerSave.IsCouponUsed(validCoupon.idCoupon)) { DisplayResultMessage(GetTranslatedString(couponAlreadyUsedTranslated, couponAlreadyUsed, currentLang), Color.red); } else { // Process the reward MonetizationManager.SetCurrency( validCoupon.currencyRewardId, MonetizationManager.GetCurrency(validCoupon.currencyRewardId) + validCoupon.currencyAmount ); // Mark the coupon as used PlayerSave.MarkCouponAsUsed(validCoupon.idCoupon); DisplayResultMessage(GetTranslatedString(couponClaimedSuccessTranslated, couponClaimedSuccess, currentLang), Color.green); // Format the dynamic reward text rewardInfo.text = string.Format( GetTranslatedString(couponRewardFormatTranslated, couponRewardFormat, currentLang), validCoupon.currencyAmount, validCoupon.currencyRewardId ); // Invoke the redeem event UIProfileMenu.Singleton.OnRedeemCoupon.Invoke(); } } else { DisplayResultMessage(GetTranslatedString(couponInvalidCodeTranslated, couponInvalidCode, currentLang), Color.red); } } /// /// Displays a result message and reward info for a limited time. /// /// The message to display. /// The color of the message text. private void DisplayResultMessage(string message, Color color) { resultMessage.text = message; resultMessage.color = color; StartCoroutine(ClearMessagesAfterDelay(3f)); } /// /// Coroutine to clear the result message and reward info after a delay. /// /// The delay in seconds before clearing the message and reward info. private IEnumerator ClearMessagesAfterDelay(float delay) { yield return new WaitForSeconds(delay); resultMessage.text = string.Empty; rewardInfo.text = string.Empty; } /// /// Returns the translated string for the current language, or a fallback value if no translation is found. /// /// Array of NameTranslatedByLanguage entries. /// Fallback text in case the translation is not found. /// The current language ID. /// A translated string if found, otherwise the fallback. private string GetTranslatedString(NameTranslatedByLanguage[] translations, string fallback, string currentLang) { if (translations != null) { foreach (var translation in translations) { if (!string.IsNullOrEmpty(translation.LanguageId) && translation.LanguageId.Equals(currentLang) && !string.IsNullOrEmpty(translation.Translate)) { return translation.Translate; } } } return fallback; } } }