using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.ErrorDialog;

public class SpecialCrystalDialog : MonoBehaviour
{
	private const int TIME_LIMIT_ERROR_CODE = 4305;

	private const float FLICK_MARGIN = 70f;

	private List<string> _assetList = new List<string>();

	[SerializeField]
	private UITexture _tipsTexture;

	[SerializeField]
	private GameObject _jpnOnlyRoot;

	[SerializeField]
	private UIDragScrollView _dragScrollView;

	[SerializeField]
	private UIButton _fundSettlementButton;

	[SerializeField]
	private UIButton _legalButton;

	[SerializeField]
	private UILabel _priceLabel;

	[SerializeField]
	private UILabel _timeLimitLabel;

	[SerializeField]
	private UILabel _purchaseCountLabel;

	[SerializeField]
	private GameObject _buyFinishDialogPrefab;

	[SerializeField]
	private GameObject _rootObject;

	[SerializeField]
	private UIButton _arrowLeftButton;

	[SerializeField]
	private UIButton _arrowRightButton;

	[SerializeField]
	private GameObject _dragObject;

	private string _defaultOpen = string.Empty;

	private bool _isDragging;

	private int _selectIndex;

	private DialogBase _dialog;

	private bool _finishGetProduct;

	private static string _defaultOpenPageStatus = string.Empty;

	private static GameObject _originalPrefab;

	private static string _productName;

	private static Action _saveOnCancel;

	private static Action _saveOnFinish;

	private static string saveScrollToProductId;

	private static List<SpecialCrystalInfo> _specialCrystalInfo = null;

	private static GameObject _finishDialogPrefab;

	public static void Create(GameObject prefab, string scrollToProductId, Action onCancel, Action onFinish)
	{
		_originalPrefab = prefab;
		_saveOnCancel = onCancel;
		_saveOnFinish = onFinish;
		saveScrollToProductId = scrollToProductId;
		SystemText systemText = Wizard.Data.SystemText;
		GameObject gameObject = UnityEngine.Object.Instantiate(prefab);
		SpecialCrystalDialog specialDialog = gameObject.GetComponent<SpecialCrystalDialog>();
		specialDialog._defaultOpen = _defaultOpenPageStatus;
		_defaultOpenPageStatus = string.Empty;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetButtonText(systemText.Get("Dia_BuyCrystal_004_Button"), systemText.Get("MyPage_0054"));
		dialogBase.onPushButton1 = delegate
		{
			specialDialog.OnClickBuyButton();
		};
		dialogBase.onPushButton2 = delegate
		{
			BuyCrystal.InstantiateCrystalDialog(scrollToProductId, onlyInputBirthday: false, onCancel, onFinish);
		};
		dialogBase.onCloseWithoutSelect = delegate
		{
			PaymentPC.GetInstance().finalize();
		};
		dialogBase.ClickSe_Btn2 = Se.TYPE.SYS_BTN_DECIDE;
		dialogBase.SetDisp(inDisp: false);
		dialogBase.gameObject.SetActive(value: false);
		specialDialog._dialog = dialogBase;
		specialDialog._rootObject.SetActive(value: false);
	}

	public static void SetDefaultOpenPage(string openPageStatus)
	{
		_defaultOpenPageStatus = openPageStatus;
	}

	private string CorrectPriceText(string price)
	{
		return Wizard.Data.SystemText.Get("Shop_0083") + "$" + price;
	}

	private void Start()
	{
		UIManager.GetInstance().StartCoroutine(Initialize());
	}

	private IEnumerator Initialize()
	{
		_finishDialogPrefab = _buyFinishDialogPrefab;
		UIManager.GetInstance().createInSceneCenterLoading();
		bool oldBackKeyEnable = GameMgr.GetIns().GetInputMgr().isBackKeyEnable;
		GameMgr.GetIns().GetInputMgr().isBackKeyEnable = false;
		foreach (SpecialCrystalInfo item in Wizard.Data.Load.data._userCrystalCount.SpecialCrystalInfo)
		{
			string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(item.DialogBGImageFileName, ResourcesManager.AssetLoadPathType.SpecialCrystal);
			_assetList.Add(assetTypePath);
		}
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroup(_assetList, null));
		_ = Wizard.Data.SystemText;
		PaymentPC paymentImpl = PaymentPC.GetInstance();
		paymentImpl.initialize();
		paymentImpl.ProductListSucceeded += OnSuccessGetProductList;
		paymentImpl.ProductListFailed += OnFailedGetProductList;
		while (!_finishGetProduct)
		{
			yield return null;
		}
		_specialCrystalInfo = new List<SpecialCrystalInfo>();
		foreach (SpecialCrystalInfo item2 in Wizard.Data.Load.data._userCrystalCount.SpecialCrystalInfo)
		{
			if (paymentImpl.FormatProductPriceList.ContainsKey(item2.ProductId) && item2.RemainTime.Second > 0)
			{
				_specialCrystalInfo.Add(item2);
			}
		}
		UIManager.GetInstance().closeInSceneCenterLoading();
		_dialog.gameObject.SetActive(value: true);
		_dialog.SetDisp(inDisp: true);
		_dialog.SetObj(base.gameObject);
		_rootObject.SetActive(value: true);
		GameMgr.GetIns().GetInputMgr().isBackKeyEnable = oldBackKeyEnable;
		if (!string.IsNullOrEmpty(_defaultOpen) && !_specialCrystalInfo.Exists((SpecialCrystalInfo info) => info.Status == _defaultOpen))
		{
			CloseDialogOnErrorFinish();
			Dialog.Create(4305);
			yield break;
		}
		if (_specialCrystalInfo.Count == 0)
		{
			CloseDialogOnErrorFinish();
			BuyCrystal.InstantiateCrystalDialog(null, onlyInputBirthday: false, _saveOnCancel, _saveOnFinish);
			yield break;
		}
		UpdateRemainTimeLabel();
		UIEventListener.Get(_arrowLeftButton.gameObject).onClick = delegate
		{
			OnClickButtonLeft();
		};
		UIEventListener.Get(_arrowRightButton.gameObject).onClick = delegate
		{
			OnClickButtonRight();
		};
		if (_specialCrystalInfo.Count <= 1)
		{
			_arrowLeftButton.gameObject.SetActive(value: false);
			_arrowRightButton.gameObject.SetActive(value: false);
		}
		_jpnOnlyRoot.gameObject.SetActive(value: false);
		_dragScrollView.enabled = false;
		InitializeDragEvent();
		int page = 0;
		for (int num = 0; num < _specialCrystalInfo.Count; num++)
		{
			if (_specialCrystalInfo[num].Status == _defaultOpen)
			{
				page = num;
				break;
			}
		}
		ChangeSelectSpecialCrystal(page);
	}

	private void CloseDialogOnErrorFinish()
	{
		base.gameObject.SetActive(value: false);
		_dialog.SetDisp(inDisp: false);
		_dialog.Close();
		_specialCrystalInfo = null;
	}

	private void InitializeDragEvent()
	{
		if (_specialCrystalInfo.Count <= 1)
		{
			return;
		}
		UIEventListener.Get(_dragObject).onDragStart = delegate
		{
			_isDragging = true;
		};
		UIEventListener.Get(_dragObject).onDragEnd = delegate
		{
			_isDragging = false;
		};
		UIEventListener.Get(_dragObject).onDrag = delegate(GameObject obj, Vector2 delta)
		{
			if (delta.x >= 70f)
			{
				if (_isDragging)
				{
					OnClickButtonLeft();
					_isDragging = false;
				}
			}
			else if (delta.x <= -70f && _isDragging)
			{
				OnClickButtonRight();
				_isDragging = false;
			}
		};
		UIEventListener.Get(_dragObject).onScroll = delegate(GameObject obj, float delta)
		{
			if (delta > 0f)
			{
				if (_isDragging)
				{
					OnClickButtonLeft();
					_isDragging = false;
				}
			}
			else if (_isDragging)
			{
				OnClickButtonRight();
				_isDragging = false;
			}
		};
	}

	private void UpdateRemainTimeLabel()
	{
		if (_specialCrystalInfo != null && _selectIndex <= _specialCrystalInfo.Count)
		{
			SpecialCrystalInfo specialCrystalInfo = _specialCrystalInfo[_selectIndex];
			_timeLimitLabel.text = specialCrystalInfo.RemainTime.GetShowText();
		}
	}

	private void Update()
	{
		UpdateRemainTimeLabel();
	}

	private void OnSuccessGetProductList()
	{
		_finishGetProduct = true;
		ClearPaymentHandler();
	}

	private void OnFailedGetProductList()
	{
		_finishGetProduct = true;
		ClearPaymentHandler();
		BuyCrystal.PaymentListErrorDialog();
		_dialog.Close();
	}

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

	private void OnClickFundSettingButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		UIManager.GetInstance().WebViewHelper.OpenWebView(WebViewHelper.WebViewType.FUND_SETTLEMENT);
	}

	private void OnClickLegalButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		UIManager.GetInstance().WebViewHelper.OpenWebView(WebViewHelper.WebViewType.LEGALTEXT);
	}

	private void OnDestroy()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_assetList);
		_assetList.Clear();
	}

	private void OnClickBuyButton()
	{
		if (PlayerStaticData.IsLootBoxRegulation(PlayerStaticData.LootBoxType.SPECIAL_CRYSTAL))
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			LootBoxDialogUtility.CreateLootBoxRegulationDialog(PlayerStaticData.LootBoxType.SPECIAL_CRYSTAL);
		}
		else if (CreateItemList.IsBirthdayNotInput())
		{
			ShowBirthdayInputDialog();
		}
		else
		{
			ShowPayment(_selectIndex);
		}
	}

	private void ShowBirthdayInputDialog()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		Action onFinish = delegate
		{
			OnDecideBirthday(_selectIndex);
		};
		bool onlyInputBirthday = true;
		BuyCrystal.CreateBuyCrystal(isPlaySe: false, null, onlyInputBirthday, OnCancelBirthdayDialog, onFinish, isSpecialCrystal: true);
	}

	private static void OnCancelBirthdayDialog()
	{
		Create(_originalPrefab, saveScrollToProductId, _saveOnCancel, _saveOnFinish);
	}

	private static void OnDecideBirthday(int index)
	{
		ShowPayment(index);
	}

	private static void ShowPayment(int index)
	{
		SpecialCrystalInfo info = _specialCrystalInfo[index];
		Action value = delegate
		{
			OnCunsumePurchageSuccess(info);
		};
		if (!PaymentPC.GetInstance().ProductPriceList.ContainsKey(info.ProductId))
		{
			Dialog.Create(4305);
			return;
		}
		PaymentPC.GetInstance().purchaceStart(info.ProductId);
		PaymentPC.GetInstance().ConsumePurchaseSucceeded += value;
	}

	private static void OnCunsumePurchageSuccess(SpecialCrystalInfo info)
	{
		OnFinishPayment(info);
	}

	private static void OnPurchageFinishSuccess(SpecialCrystalInfo info)
	{
		OnFinishPayment(info);
	}

	private static void OnFinishPayment(SpecialCrystalInfo info)
	{
		UIManager.GetInstance().StartCoroutine(OnFinishPaymentCoroutine(info));
	}

	private static IEnumerator OnFinishPaymentCoroutine(SpecialCrystalInfo info)
	{
		UIManager.GetInstance().OpenNotTouch();
		SpecialCrystalTaskInfo task = new SpecialCrystalTaskInfo();
		bool taskSuccess = false;
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			taskSuccess = true;
		}));
		while (!taskSuccess)
		{
			yield return null;
		}
		UIManager.GetInstance().offNotTouch();
		ShowBuyFinishDialog(info);
	}

	private static void ShowBuyFinishDialog(SpecialCrystalInfo info)
	{
		Action value = delegate
		{
			OnPurchageFinishSuccess(info);
		};
		PaymentImpl.GetInstance().ConsumePurchaseSucceeded -= value;
		if (!PaymentImpl.GetInstance().IsRefundedReceipt)
		{
			SpecialCrystalBuyFinishDialog.Create(_finishDialogPrefab, info, _productName);
		}
	}

	private void OnClickButtonLeft()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		_selectIndex--;
		if (_selectIndex < 0)
		{
			_selectIndex = _specialCrystalInfo.Count - 1;
		}
		ChangeSelectSpecialCrystal(_selectIndex);
	}

	private void OnClickButtonRight()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		_selectIndex++;
		if (_selectIndex >= _specialCrystalInfo.Count)
		{
			_selectIndex = 0;
		}
		ChangeSelectSpecialCrystal(_selectIndex);
	}

	private void ChangeSelectSpecialCrystal(int page)
	{
		_selectIndex = page;
		PaymentPC instance = PaymentPC.GetInstance();
		SpecialCrystalInfo specialCrystalInfo = _specialCrystalInfo[_selectIndex];
		Dictionary<string, string> formatProductPriceList = instance.FormatProductPriceList;
		_dialog.SetTitleLabel(specialCrystalInfo.DialogTitle);
		if (formatProductPriceList != null)
		{
			bool flag = false;
			foreach (KeyValuePair<string, string> item in formatProductPriceList)
			{
				if (item.Key == specialCrystalInfo.ProductId)
				{
					_priceLabel.text = CorrectPriceText(item.Value);
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				_priceLabel.text = string.Empty;
			}
			else
			{
				_productName = instance.ProductNameList[specialCrystalInfo.ProductId];
			}
			_purchaseCountLabel.text = Wizard.Data.SystemText.Get("MyPage_0060", specialCrystalInfo.AvailablePurchaseCount.ToString());
		}
		_tipsTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(specialCrystalInfo.DialogBGImageFileName, ResourcesManager.AssetLoadPathType.SpecialCrystal, isfetch: true)) as Texture;
	}
}
