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 async void OnClickClaimCoupon() { string enteredCode = couponInput.text.Trim(); RequestResult res = await BackendManager.Service.RedeemCouponAsync(enteredCode); if (!res.Success) { string msg = res.Reason switch { "0" => GetTranslatedString(couponAlreadyUsedTranslated, couponAlreadyUsed, currentLang), "1" => GetTranslatedString(couponInvalidCodeTranslated, couponInvalidCode, currentLang), _ => "Unknown error" }; DisplayResultMessage(msg, Color.red); return; } string[] parts = res.Reason.Split('|'); string currency = parts[0]; int amount = int.Parse(parts[1]); string successMsg = GetTranslatedString(couponClaimedSuccessTranslated, couponClaimedSuccess, currentLang); DisplayResultMessage(successMsg, Color.green); string rewardFmt = GetTranslatedString(couponRewardFormatTranslated, couponRewardFormat, currentLang); rewardInfo.text = string.Format(rewardFmt, amount, currency); UIProfileMenu.Singleton.OnRedeemCoupon.Invoke(); } /// /// 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; } } }