using System; using System.Collections; using System.Collections.Generic; using CIG.Translation; using CIGEnums; using SUISS.Core; using UnityEngine; using UnityEngine.UI; public class HUDSaleButton : MonoBehaviour { private void OnEnable() { this.TrySubscribeBOBGOFManger(); this.TrySubscribeSaleManager(); this.UpdateButton(); base.StartCoroutine(this._updateTimeRemainingRoutine = this.TickTimer()); } private void OnDisable() { base.StopCoroutine(this._updateTimeRemainingRoutine); this.UnsubscribeManagers(); } public void OnSaleClicked() { if (this.IsBOBGOFSaleActive) { SingletonMonobehaviour.Instance.RequestPopup(null); return; } CIGSaleManager.Sale activeSale = this.GetActiveSale(); if (activeSale != null && activeSale.Cash && !activeSale.Gold) { SingletonMonobehaviour.Instance.OpenShopMenu(ShopMenuTabs.Cash); return; } SingletonMonobehaviour.Instance.OpenShopMenu(ShopMenuTabs.Gold); } private bool IsBOBGOFSaleActive { get { return this._bobgofManager != null && this._bobgofManager.State == CIGBuyOneBuildingGetOneFreeManager.BuyOneBuildingGetOneFreeState.Active; } } private void TrySubscribeBOBGOFManger() { if (!this._isSubscribedToBOBGOFManger && SingletonMonobehaviour.IsAvailable) { this._bobgofManager = SingletonMonobehaviour.Instance; this._bobgofManager.StateChangedEvent += this.OnBOBGOFChanged; this._isSubscribedToBOBGOFManger = true; } } private void TrySubscribeSaleManager() { if (!this._isSubscribedToSaleManger && SingletonMonobehaviour.IsAvailable) { this._saleManager = SingletonMonobehaviour.Instance; this._saleManager.SaleChanged += this.OnSaleChanged; this._isSubscribedToSaleManger = true; } } private void UnsubscribeManagers() { if (this._isSubscribedToSaleManger) { this._saleManager.SaleChanged -= this.OnSaleChanged; this._saleManager = null; this._isSubscribedToSaleManger = false; } if (this._isSubscribedToBOBGOFManger) { this._bobgofManager.StateChangedEvent -= this.OnBOBGOFChanged; this._bobgofManager = null; this._isSubscribedToBOBGOFManger = false; } } private void UpdateButton() { HUDSaleButtonType hudsaleButtonType = HUDSaleButtonType.NoSale; if (this.IsBOBGOFSaleActive) { if (this._bobgofManager.PaidBuilding != null) { hudsaleButtonType = HUDSaleButtonType.BOGOFSale; } else { UnityEngine.Debug.LogErrorFormat("BOBGOFManager.PaidBuilding is null but there is a sale active. I don't know how to handle this.", new object[0]); } ILocalizedString localizedString = Localization.Integer(1); this._dealLabel.LocalizedString = Localization.Concat(new ILocalizedString[] { localizedString, Localization.Key("CURRENCY_APPEND_PLUS"), localizedString }); } else { CIGSaleManager.Sale activeSale = this.GetActiveSale(); if (activeSale != null) { if (activeSale.Gold && activeSale.Cash) { hudsaleButtonType = HUDSaleButtonType.GoldCashSale; } else if (activeSale.Gold) { hudsaleButtonType = HUDSaleButtonType.GoldSale; } else if (activeSale.Cash) { hudsaleButtonType = HUDSaleButtonType.CashSale; } this._dealLabel.LocalizedString = Localization.Percentage(200); } } int count = this._saleIconObjects.Count; for (int i = 0; i < count; i++) { HUDSaleButton.HUDSaleButtonIcon hudsaleButtonIcon = this._saleIconObjects[i]; hudsaleButtonIcon.GameObject.SetActive(hudsaleButtonIcon.Type == hudsaleButtonType); } bool flag = hudsaleButtonType != HUDSaleButtonType.NoSale; this._saleActiveContainer.SetActive(flag); this._saleInactiveContainer.SetActive(!flag); this.UpdateTimer(); } private CIGSaleManager.Sale GetActiveSale() { return (!(this._saleManager != null)) ? null : this._saleManager.CurrentSale; } private void OnBOBGOFChanged() { this.UpdateButton(); } private void OnSaleChanged(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale) { this.UpdateButton(); } private void UpdateTimer() { if (this.IsBOBGOFSaleActive && this._bobgofManager.SaleTimeLeft > TimeSpan.Zero) { this._saleTimerLabel.LocalizedString = Localization.TimeSpan(this._bobgofManager.SaleTimeLeft, false); } else { CIGSaleManager.Sale activeSale = this.GetActiveSale(); if (activeSale != null) { this._saleTimerLabel.LocalizedString = Localization.TimeSpan(activeSale.SaleTimeLeft, false); } } } private IEnumerator TickTimer() { for (;;) { this.UpdateTimer(); yield return new WaitForSecondsRealtime(1f); } yield break; } [SerializeField] private Image _saleIcon; [SerializeField] private GameObject _saleActiveContainer; [SerializeField] private GameObject _saleInactiveContainer; [SerializeField] private LocalizedText _saleTimerLabel; [SerializeField] private LocalizedText _dealLabel; [SerializeField] private List _saleIconObjects; private bool _isSubscribedToBOBGOFManger; private CIGBuyOneBuildingGetOneFreeManager _bobgofManager; private bool _isSubscribedToSaleManger; private CIGSaleManager _saleManager; private IEnumerator _updateTimeRemainingRoutine; [Serializable] public class HUDSaleButtonIcon { public HUDSaleButtonType Type { get { return this._type; } } public GameObject GameObject { get { return this._gameObject; } } [SerializeField] private HUDSaleButtonType _type; [SerializeField] public GameObject _gameObject; } }