|
- using System;
- using System.Collections;
- using CIG;
- using CIG.Translation;
- using CIGEnums;
- using SUISS.Core;
- using SUISSEngine;
- using UnityEngine;
-
- public class GiftsPopupState : PopupBaseState
- {
- public override void Init()
- {
- base.Init();
- this._webService = SingletonMonobehaviour<CIGWebService>.Instance;
- }
-
- public override void Enter(State oldState)
- {
- base.Enter(oldState);
- if (this.HasUserKey)
- {
- ((GiftsPopupView)this.View).Initialize(this._webService.UserKey);
- }
- else
- {
- ((GiftsPopupView)this.View).Initialize(string.Empty);
- if (this._webService != null)
- {
- this._webService.PullRequestCompleted += this.UpdateView;
- this._webService.ForcePullRequest();
- }
- }
- }
-
- public override void Leave(State newState)
- {
- base.Leave(newState);
- ((GiftsPopupView)this.View).Deinitialize();
- }
-
- public void OpenInstagram()
- {
- Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.InstagramUrl);
- }
-
- public void OpenFacebook()
- {
- Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.FacebookUrl);
- }
-
- public void OpenTwitter()
- {
- Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.TwitterUrl);
- }
-
- public void RedeemCode(string code)
- {
- if (this._webService == null)
- {
- UnityEngine.Debug.LogWarning("Cannot redeem code: null web service");
- return;
- }
- code = code.Trim();
- if (code.Length > 0)
- {
- base.StartCoroutine(this.RedeemRoutine(code));
- }
- }
-
- private bool HasUserKey
- {
- get
- {
- return this._webService != null && this._webService.UserKey != null && this._webService.UserKey.Length > 0;
- }
- }
-
- private void UpdateView()
- {
- if (this.HasUserKey)
- {
- ((GiftsPopupView)this.View).Initialize(this._webService.UserKey);
- }
- }
-
- private void DisableInput()
- {
- ((GiftsPopupView)this.View).UpdateInput(false);
- }
-
- private void EnableInput()
- {
- if (this._isInState)
- {
- ((GiftsPopupView)this.View).UpdateInput(true);
- }
- }
-
- private void ShowErrorPopup(ILocalizedString text)
- {
- this._fsm.SwitchState<GenericPopupState>().UpdateInfo(null, Localization.Key("oops_something_went_wrong"), text, Localization.Key("ok"), null, null, null, null, true);
- }
-
- private void ShowRewardPopup(Currencies reward)
- {
- base.ClosePopup(delegate()
- {
- SingletonMonobehaviour<PopupManager>.Instance.ShowReceiveRewardPopup(reward);
- });
- }
-
- private IEnumerator RedeemRoutine(string code)
- {
- this.DisableInput();
- WWW request = this._webService.RedeemCode(code);
- yield return request;
- if (request.error != null && request.error.Length > 0)
- {
- UnityEngine.Debug.LogError("Redeem code error: " + request.error);
- this.ShowErrorPopup(Localization.Key("social_code_error"));
- }
- else
- {
- string text = request.text;
- if (text == "FRIENDCODE_ALREADY_USED")
- {
- this.ShowErrorPopup(Localization.Key("social_code_already_used_friendcode"));
- }
- else if (text == "FRIENDCODE_LIMIT_REACHED")
- {
- this.ShowErrorPopup(Localization.Key("social_code_maximum_reached"));
- }
- else if (text == "GIFTCODE_ALREADY_USED")
- {
- this.ShowErrorPopup(Localization.Key("social_code_already_used_giftcode"));
- }
- else if (text == "GIFTCODE_EXPIRED")
- {
- this.ShowErrorPopup(Localization.Key("social_code_expired"));
- }
- else if (text == "UNKNOWN_CODE")
- {
- this.ShowErrorPopup(Localization.Key("social_code_unknown"));
- }
- else
- {
- int num;
- Currencies currencies = CIGWebService.ParseCurrencies(text, out num);
- if (currencies.IsEmpty())
- {
- UnityEngine.Debug.LogError("Result did not have currencies: '" + text + "'");
- }
- else
- {
- CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
- instance.EarnCurrencies(currencies, this);
- this.ShowRewardPopup(currencies);
- }
- }
- }
- this.EnableInput();
- yield break;
- }
-
- private CIGWebService _webService;
- }
|