using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.ItemPurchase;

namespace Wizard;

public class ItemPurchase : MonoBehaviour
{
	private const int PRODUCT_SCROLL_OBJECT_NUM = 5;

	private const int DEPTH_PURCHASE_SUCCESS_DIALOG = 100;

	[SerializeField]
	private UILabel _redEtherLabel;

	[SerializeField]
	private UILabel _premierOrbOrbPieceLabel;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private UIWrapContent _wrapContent;

	[SerializeField]
	private UIScrollBarWrapContent _scrollBarWrapContent;

	[SerializeField]
	private WrapContentsScrollBarSize _wrapContentScrollBarSize;

	[SerializeField]
	private GameObject _itemPlateOriginal;

	[SerializeField]
	private PurchaseConfirm _prefabConfirmDialog;

	[SerializeField]
	private UILabel _labelNoItem;

	private List<ItemPurchaseData> _itemDataList;

	private List<ItemPurchasePlate> _plateList = new List<ItemPurchasePlate>();

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

	private ItemPurchaseData _purchaseItemData;

	public void Init(Action onInitFinCallBack)
	{
		_labelNoItem.gameObject.SetActive(value: false);
		_itemPlateOriginal.gameObject.SetActive(value: false);
		_scrollBarWrapContent.m_WrapContents = _wrapContent;
		StartConnectItemPurchaseInfo(delegate
		{
			_itemDataList = Data.ItemPurchaseInfo.itemPurchaseList;
			CreateItemView(onInitFinCallBack);
		});
		UserItemCountUpdate();
	}

	private void CreateItemView(Action onFinCallBack = null)
	{
		if (_itemDataList.Count <= 0)
		{
			_labelNoItem.gameObject.SetActive(value: true);
			_scrollView.gameObject.SetActive(value: false);
			_scrollBarWrapContent.gameObject.SetActive(value: false);
			onFinCallBack.Call();
			return;
		}
		_labelNoItem.gameObject.SetActive(value: false);
		_scrollView.gameObject.SetActive(value: true);
		_scrollBarWrapContent.gameObject.SetActive(value: true);
		StartCoroutine(LoadImages(delegate
		{
			StartCoroutine(CreateScrollView(onFinCallBack));
		}));
	}

	private void StartConnectItemPurchaseInfo(Action<NetworkTask.ResultCode> callbackOnSuccess)
	{
		ItemPurchaseInfoTask task = new ItemPurchaseInfoTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, callbackOnSuccess));
	}

	private IEnumerator LoadImages(Action callBack = null)
	{
		ResourcesManager resourcesManager = Toolbox.ResourcesManager;
		List<string> assetList = new List<string>();
		for (int i = 0; i < _itemDataList.Count; i++)
		{
			ItemPurchaseData itemPurchaseData = _itemDataList[i];
			string assetTypePath = resourcesManager.GetAssetTypePath(itemPurchaseData.textureName, ResourcesManager.AssetLoadPathType.Item);
			if (!assetList.Contains(assetTypePath) && !_loadedResourceList.Contains(assetTypePath))
			{
				assetList.Add(assetTypePath);
			}
			if (IsCardPackTicket(itemPurchaseData.require_item_type, itemPurchaseData.RequireUserGoodsId))
			{
				string assetTypePath2 = resourcesManager.GetAssetTypePath($"card_pack_{itemPurchaseData.RequireUserGoodsId}_icon", ResourcesManager.AssetLoadPathType.ShopCardPack);
				if (!assetList.Contains(assetTypePath2) && !_loadedResourceList.Contains(assetTypePath2))
				{
					assetList.Add(assetTypePath2);
				}
			}
		}
		if (assetList.Count > 0)
		{
			yield return StartCoroutine(resourcesManager.LoadAssetGroupAsync(assetList, null));
			_loadedResourceList.AddRange(assetList);
		}
		callBack.Call();
	}

	private void UnloadImages()
	{
		if (_loadedResourceList != null)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
			_loadedResourceList = null;
		}
	}

	private IEnumerator CreateScrollView(Action onFinishCallback = null)
	{
		_wrapContent.gameObject.SetActive(value: false);
		if (_plateList.Count > 0)
		{
			for (int i = 0; i < _plateList.Count; i++)
			{
				UnityEngine.Object.Destroy(_plateList[i].gameObject);
			}
			_plateList.Clear();
		}
		if (_itemDataList.Count == 1)
		{
			_wrapContent.enabled = false;
			ItemPurchasePlate component = NGUITools.AddChild(_scrollView.gameObject, _itemPlateOriginal).GetComponent<ItemPurchasePlate>();
			component._buyBtnCallBack = OnPushBuyButton;
			component.SetData(0);
			_plateList.Add(component);
		}
		else
		{
			_wrapContent.enabled = true;
			for (int j = 0; j < _itemDataList.Count && j < 5; j++)
			{
				ItemPurchasePlate component2 = NGUITools.AddChild(_wrapContent.gameObject, _itemPlateOriginal).GetComponent<ItemPurchasePlate>();
				component2._buyBtnCallBack = OnPushBuyButton;
				_plateList.Add(component2);
			}
		}
		yield return null;
		for (int k = 0; k < _plateList.Count; k++)
		{
			_plateList[k].gameObject.SetActive(value: true);
		}
		_wrapContent.minIndex = -(_itemDataList.Count - 1);
		_wrapContent.maxIndex = 0;
		_wrapContent.onInitializeItem = OnInitializeItem;
		_wrapContent.gameObject.SetActive(value: true);
		ResetScrollView();
		onFinishCallback.Call();
	}

	private void ResetScrollView()
	{
		_wrapContentScrollBarSize.ContentUpdate();
		_wrapContent.SortBasedOnScrollMovement();
		_scrollView.ResetPosition();
		_wrapContent.WrapContent();
	}

	private void OnInitializeItem(GameObject go, int wrapIndex, int realIndex)
	{
		int num = -realIndex;
		if (num >= _itemDataList.Count || num < 0)
		{
			go.SetActive(value: false);
			return;
		}
		go.SetActive(value: true);
		go.GetComponent<ItemPurchasePlate>().SetData(num);
	}

	private void OnDestroy()
	{
		UnloadImages();
	}

	private void OnPushBuyButton(ItemPurchaseData itemData)
	{
		if (itemData != null)
		{
			DialogBase dialogBase = ShopCommonUtility.CreateBasePopupPurchaseConfirm(new EventDelegate(delegate
			{
				_purchaseItemData = itemData;
				ItemPurchaseBuyTask itemPurchaseBuyTask = new ItemPurchaseBuyTask();
				itemPurchaseBuyTask.SetParameter(itemData.purchase_id, itemData.rest);
				StartCoroutine(Toolbox.NetworkManager.Connect(itemPurchaseBuyTask, OnSuccessItemPurchaseBuy));
			}));
			PurchaseConfirm purchaseConfirm = UnityEngine.Object.Instantiate(_prefabConfirmDialog);
			dialogBase.SetObj(purchaseConfirm.gameObject);
			dialogBase.SetTitleLabel(Data.SystemText.Get("Shop_0135"));
			string purchaseText = Data.SystemText.Get("Shop_0101", itemData.purchase_name);
			int haveUserGoods = PlayerStaticData.GetHaveUserGoods(itemData.require_item_type, itemData.RequireUserGoodsId);
			if (IsCardPackTicket(itemData.require_item_type, itemData.RequireUserGoodsId))
			{
				Texture texture = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath($"card_pack_{itemData.RequireUserGoodsId}_icon", ResourcesManager.AssetLoadPathType.ShopCardPack, isfetch: true)) as Texture;
				purchaseConfirm.SetConfirmDialog(itemData.require_item_num, purchaseText, haveUserGoods, itemData.require_item_type, itemData.RequireUserGoodsId, null, texture);
			}
			else
			{
				purchaseConfirm.SetConfirmDialog(itemData.require_item_num, purchaseText, haveUserGoods, itemData.require_item_type, itemData.RequireUserGoodsId, null);
			}
		}
	}

	private bool IsCardPackTicket(UserGoods.Type itemType, long userGoodsId)
	{
		if (itemType != UserGoods.Type.Item)
		{
			return false;
		}
		if (Item.GetItemType(itemType, (int)userGoodsId) == Item.Type.CardPackTicket)
		{
			return true;
		}
		return false;
	}

	private void OnSuccessItemPurchaseBuy(NetworkTask.ResultCode e)
	{
		MyPageMenu.Instance.UpdateCrystalCount();
		MyPageMenu.Instance.UpdateRupyCount();
		ShopCommonUtility.CreatePurchaseSuccess(_purchaseItemData.purchase_name).SetPanelDepth(100);
		List<ItemPurchaseData> oldItemDataList = new List<ItemPurchaseData>(_itemDataList);
		StartConnectItemPurchaseInfo(delegate
		{
			bool flag = false;
			if (oldItemDataList.Count != _itemDataList.Count)
			{
				flag = true;
			}
			else
			{
				for (int i = 0; i < _itemDataList.Count; i++)
				{
					if (oldItemDataList[i].purchase_id != _itemDataList[i].purchase_id)
					{
						flag = true;
						break;
					}
				}
			}
			if (flag)
			{
				CreateItemView();
			}
			else
			{
				for (int j = 0; j < _plateList.Count; j++)
				{
					_plateList[j].UpdatePlate();
				}
			}
			UserItemCountUpdate();
		});
	}

	private void UserItemCountUpdate()
	{
		_redEtherLabel.text = PlayerStaticData.UserRedEtherCount.ToString();
		_premierOrbOrbPieceLabel.text = PlayerStaticData.GetItemNum(1001).ToString();
	}
}
