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

namespace Wizard;

public class BattlePassPurchaseDialog : MonoBehaviour
{
	private const string PATH_DIALOG_PREFAB = "UI/layoutParts/BattlePass/DialogBattlePassPurchase";

	[SerializeField]
	private UILabel _descriptionLabel;

	[SerializeField]
	private UILabel _haveCrystalNumLabel;

	[SerializeField]
	private BattlePassPurchaseProductView _productViewOriginal;

	[SerializeField]
	private UIGrid _productViewGrid;

	[SerializeField]
	private PurchaseConfirm _prefabPurchaseConfirmDialog;

	private DialogBase _dialogBattlePassPurchase;

	private List<string> _loadedResourceList;

	private Action _battlePassViewUpdate;

	public static void Create(Action battlePassViewUpdate = null)
	{
		BattlePassPurchaseInfoTask task = new BattlePassPurchaseInfoTask();
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
			dialogBase.SetSize(DialogBase.Size.XL);
			dialogBase.SetTitleLabel(Data.SystemText.Get("BattlePass_0002"));
			GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/BattlePass/DialogBattlePassPurchase")) as GameObject;
			dialogBase.SetObj(gameObject);
			gameObject.GetComponent<BattlePassPurchaseDialog>().Initialize(task.PurchaseInfo, dialogBase, battlePassViewUpdate);
		}));
	}

	public void Initialize(BattlePassPurchaseInfo battlePassPurchaseInfo, DialogBase dialog, Action battlePassViewUpdate)
	{
		_battlePassViewUpdate = battlePassViewUpdate;
		_dialogBattlePassPurchase = dialog;
		_productViewOriginal.gameObject.SetActive(value: false);
		_loadedResourceList = new List<string>();
		SetViewUI(battlePassPurchaseInfo);
	}

	private void OnDestroy()
	{
		UnloadResources();
	}

	private void SetViewUI(BattlePassPurchaseInfo battlePassPurchaseInfo)
	{
		_descriptionLabel.text = battlePassPurchaseInfo.Description;
		_haveCrystalNumLabel.text = PlayerStaticData.UserCrystalCount.ToString();
		LoadResources(battlePassPurchaseInfo, delegate
		{
			foreach (BattlePassProduct product in battlePassPurchaseInfo.ProductList)
			{
				GameObject obj = NGUITools.AddChild(_productViewGrid.gameObject, _productViewOriginal.gameObject);
				obj.GetComponent<BattlePassPurchaseProductView>().SetView(product, OnClickPoster, OnClickBuyButton);
				obj.SetActive(value: true);
			}
			_productViewGrid.Reposition();
		});
	}

	private void LoadResources(BattlePassPurchaseInfo purchaseInfo, Action onFinishLoad)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		List<string> loadPassList = new List<string>();
		foreach (BattlePassProduct product in purchaseInfo.ProductList)
		{
			loadPassList.Add(product.GetPosterTexturePath(isFetch: false));
		}
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPassList, delegate
		{
			_loadedResourceList.AddRange(loadPassList);
			UIManager.GetInstance().closeInSceneCenterLoading();
			onFinishLoad.Call();
		}));
	}

	private void UnloadResources()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
		_loadedResourceList = null;
	}

	private void OnClickPoster(BattlePassProduct product)
	{
		BattlePassProductDetailDialog.Create(product);
	}

	private void OnClickBuyButton(BattlePassProduct product)
	{
		if (PlayerStaticData.UserCrystalCount < product.PriceInCrystal)
		{
			ShopCommonUtility.CreateCrystalShortagePopup();
		}
		else
		{
			CreateBuyConfirmDialog(product);
		}
	}

	private void CreateBuyConfirmDialog(BattlePassProduct product)
	{
		DialogBase dialogBase = ShopCommonUtility.CreateBasePopupPurchaseConfirm(new EventDelegate(delegate
		{
			ConnectBuyBattlePass(product);
		}));
		PurchaseConfirm purchaseConfirm = UnityEngine.Object.Instantiate(_prefabPurchaseConfirmDialog);
		dialogBase.SetObj(purchaseConfirm.gameObject);
		dialogBase.SetTitleLabel(Data.SystemText.Get("BattlePass_0004"));
		string purchaseText = Data.SystemText.Get("Shop_0101", product.Name);
		purchaseConfirm.SetClystalConfirmDialog(product.PriceInCrystal, purchaseText, PlayerStaticData.UserCrystalCount, product.ExpirtyInfo);
		_dialogBattlePassPurchase.Close();
	}

	private void ConnectBuyBattlePass(BattlePassProduct product)
	{
		BattlePassBuyTask task = new BattlePassBuyTask();
		task.SetParameter(product.SeasonId, product.Id);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			OnSuccessBuyBattlePass(product, task.RewardList);
		}));
	}

	private void OnSuccessBuyBattlePass(BattlePassProduct product, List<ReceivedReward> rewardList)
	{
		MyPageMenu.Instance.UpdateCrystalCount();
		DialogBase dialogBase = ShopCommonUtility.CreatePurchaseSuccess(product.Name);
		_battlePassViewUpdate.Call();
		if (rewardList != null && rewardList.Count > 0)
		{
			dialogBase.OnClose = delegate
			{
				OpenRewardReceiveDialog(rewardList);
			};
		}
		if (UIManager.GetInstance().IsCurrentScene(UIManager.ViewScene.MyPage))
		{
			MyPageMenu.Instance.HomeMenu.HideAndRepositionSubBanner("battle_pass_buy");
		}
	}

	private void OpenRewardReceiveDialog(List<ReceivedReward> rewardList)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogCreator.CreateRewardReceiveDialog(rewardList).SetTitleLabel(Data.SystemText.Get("BattlePass_0005"));
	}
}
