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

namespace Wizard;

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

	[SerializeField]
	private UILabel _descriptionLabel;

	[SerializeField]
	private UITexture _posterTexture;

	private List<string> _loadedResourceList;

	public static void Create(BattlePassProduct product)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetPanelDepth(100);
		dialogBase.SetTitleLabel(Data.SystemText.Get("BattlePass_0003"));
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/BattlePass/DialogBattlePassProductDetail")) as GameObject;
		dialogBase.SetObj(gameObject);
		gameObject.GetComponent<BattlePassProductDetailDialog>().Initialize(product);
	}

	public void Initialize(BattlePassProduct product)
	{
		_descriptionLabel.text = product.Description;
		_loadedResourceList = new List<string>();
		LoadResources(product, delegate
		{
			_posterTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(product.GetDetailPosterTexturePath(isFetch: true)) as Texture;
		});
	}

	private void OnDestroy()
	{
		UnloadResources();
	}

	private void LoadResources(BattlePassProduct product, Action onFinishLoad)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		List<string> loadPassList = new List<string>();
		loadPassList.Add(product.GetDetailPosterTexturePath(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;
	}
}
