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

namespace Wizard;

public class UserDataDialog : MonoBehaviour
{
	[SerializeField]
	private GuildUserDataDialog _inviteView;

	private bool _isFinishLoadResource;

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

	public DialogBase Dialog { get; private set; }

	public static UserDataDialog Create(GameObject prefab, UserInfoBase user, string dialogTitle)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(dialogTitle);
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		UserDataDialog component = UnityEngine.Object.Instantiate(prefab).GetComponent<UserDataDialog>();
		_ = component.gameObject;
		dialogBase.SetObj(component.gameObject);
		component.Dialog = dialogBase;
		component.Initialize(user, dialogBase);
		return component;
	}

	private void Initialize(UserInfoBase user, DialogBase dialog)
	{
		UIManager uiManager = UIManager.GetInstance();
		uiManager.StartCoroutine(LoadResource(user, delegate
		{
			if (base.gameObject != null)
			{
				_inviteView.SetUserData(user);
			}
		}));
		dialog.OnClose = (Action)Delegate.Combine(dialog.OnClose, (Action)delegate
		{
			uiManager.StartCoroutine(UnloadImages(delegate
			{
				if (this != null)
				{
					UnityEngine.Object.Destroy(base.gameObject);
				}
			}));
		});
	}

	private IEnumerator LoadResource(UserInfoBase userData, Action callBack)
	{
		_isFinishLoadResource = false;
		List<string> resourcePathList = userData.GetUserAssetPathList();
		yield return UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(resourcePathList, delegate
		{
			_isFinishLoadResource = true;
		}));
		_loadedResourceList.AddRange(resourcePathList);
		callBack.Call();
	}

	private IEnumerator UnloadImages(Action callBack)
	{
		while (!_isFinishLoadResource)
		{
			yield return null;
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
		_loadedResourceList.Clear();
		callBack.Call();
	}

	public void SetDiscriptionLabel(string text)
	{
		_inviteView.SetDiscriptionLabel(text);
	}
}
