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

namespace Wizard;

public class GachaResultBuyCardPackDialog : MonoBehaviour
{
	private const int PANEL_DEPTH_RESULT_DIALOG = 500;

	private const int PANEL_ORDER_RESULT_DIALOG = 2;

	[SerializeField]
	private GachaResultBuyCardPack _prefabGachaResultBuyCardPack;

	private GachaResultBuyCardPack _objGachaResultBuyCardPack;

	private List<string> _loadedAssetPathListResultDialog;

	public DialogBase Dialog { get; private set; }

	public bool IsLoading => _objGachaResultBuyCardPack.IsLoading;

	public bool IsFlickEnable
	{
		set
		{
			if (_objGachaResultBuyCardPack != null)
			{
				_objGachaResultBuyCardPack.IsFlickEnable = value;
			}
		}
	}

	public void Create(PackOpenDetail packOpenData, CardDetailUI cardDetailDialog)
	{
		Dialog = UIManager.GetInstance().CreateDialogClose();
		Dialog.gameObject.layer = LayerMask.NameToLayer("MyPage");
		Dialog.SetBackViewLayer(LayerMask.NameToLayer("MyPage"));
		Dialog.SetPanelSortingOrder(2);
		Dialog.SetPanelDepth(500);
		Dialog.SetSize(DialogBase.Size.XL);
		Dialog.SetTitleLabel(Data.SystemText.Get("Shop_0010"));
		Dialog.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		Dialog.OpenSe = Se.TYPE.NONE;
		Dialog.OnCloseStart = OnCloseStartResultDialog;
		Dialog.OnClose = OnCloseResultDialog;
		_objGachaResultBuyCardPack = UnityEngine.Object.Instantiate(_prefabGachaResultBuyCardPack);
		Dialog.SetObj(_objGachaResultBuyCardPack.gameObject);
		_objGachaResultBuyCardPack.GetComponent<UIPanel>().sortingOrder = 2;
		_objGachaResultBuyCardPack.Init(packOpenData.pack_list, cardDetailDialog);
		StartCoroutine(_objGachaResultBuyCardPack.ViewCardList(1));
	}

	public void AddOnCloseStart(Action onCloseStart)
	{
		DialogBase dialog = Dialog;
		dialog.OnCloseStart = (Action)Delegate.Combine(dialog.OnCloseStart, onCloseStart);
	}

	public void AddOnClose(Action onClose)
	{
		DialogBase dialog = Dialog;
		dialog.OnClose = (Action)Delegate.Combine(dialog.OnClose, onClose);
	}

	private void OnCloseStartResultDialog()
	{
		_objGachaResultBuyCardPack.IsDialogCloseStart = true;
		_loadedAssetPathListResultDialog = _objGachaResultBuyCardPack._loadedAssetPathList;
	}

	private void OnCloseResultDialog()
	{
		StartCoroutine(RemoveResultDialogResources());
	}

	private IEnumerator RemoveResultDialogResources()
	{
		UIBase_CardManager cardManager = UIManager.GetInstance().getUIBase_CardManager();
		while (!cardManager.getCreateEndFlag() || !cardManager.isAssetAllReady)
		{
			yield return null;
		}
		List<UIBase_CardManager.CardObjData> cardList2DObjs = UIManager.GetInstance().getCardList2DObjs();
		if (cardList2DObjs.Count > 0)
		{
			cardList2DObjs.ForEach(delegate(UIBase_CardManager.CardObjData g)
			{
				UnityEngine.Object.Destroy(g.CardObj);
			});
		}
		if (Toolbox.ResourcesManager.CardListAssetPathList.Count > 0)
		{
			_loadedAssetPathListResultDialog.AddRange(Toolbox.ResourcesManager.CardListAssetPathList);
			Toolbox.ResourcesManager.CardListAssetPathList.Clear();
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedAssetPathListResultDialog);
		_loadedAssetPathListResultDialog.Clear();
	}
}
