using System.Collections;
using UnityEngine;
using Wizard;

public class MyPageShopCrystalApeal : MonoBehaviour
{
	private enum State
	{
		INIT,
		SUCCESS,
		ERROR
	}

	private const int TIME_LIMIT_BORDER_SECOND = 172800;

	[SerializeField]
	private UILabel _timeLimit;

	[SerializeField]
	private GameObject _newIcon;

	private State _state;

	public IEnumerator Initialize(double serverTime)
	{
		_state = State.INIT;
		PaymentPC paymentImpl = PaymentPC.GetInstance();
		paymentImpl.initialize();
		paymentImpl.ProductListSucceeded += OnSuccessGetProductList;
		paymentImpl.ProductListFailed += OnFailedGetProductList;
		while (_state == State.INIT)
		{
			yield return null;
		}
		ClearPaymentHandler();
		if (_state == State.ERROR)
		{
			yield break;
		}
		RemainTime remainTime = null;
		foreach (string productId in paymentImpl.ProductIdList)
		{
			if (paymentImpl.ProductCurrentPurchaseCount.ContainsKey(productId))
			{
				int num = paymentImpl.ProductCurrentPurchaseCount[productId];
				int num2 = paymentImpl.ProductPurchaseLimitCount[productId];
				if (num >= num2)
				{
					continue;
				}
			}
			RemainTime remainTime2 = new RemainTime(paymentImpl.ProductEndTime[productId], serverTime);
			if (remainTime2.Second <= 172800 && remainTime2.Second > 0)
			{
				if (remainTime == null)
				{
					remainTime = remainTime2;
				}
				else if (remainTime2.Second < remainTime.Second)
				{
					remainTime = remainTime2;
				}
			}
		}
		if (remainTime != null)
		{
			_timeLimit.gameObject.SetActive(value: true);
			_timeLimit.text = remainTime.GetShowText();
		}
		else
		{
			_timeLimit.gameObject.SetActive(value: false);
		}
		_newIcon.SetActive(Data.Load.data._userCrystalCount.NeedNewIcon);
	}

	private void OnSuccessGetProductList()
	{
		_state = State.SUCCESS;
	}

	private void OnFailedGetProductList()
	{
		_state = State.ERROR;
	}

	private void ClearPaymentHandler()
	{
		PaymentPC instance = PaymentPC.GetInstance();
		instance.ProductListSucceeded -= OnSuccessGetProductList;
		instance.ProductListFailed -= OnFailedGetProductList;
	}
}
