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

namespace Wizard;

public class UserListView : MonoBehaviour
{
	[SerializeField]
	private SimpleScrollViewUI _scrollView;

	[SerializeField]
	private UIPanel _panel;

	private List<UserInfoBase> _userList;

	private string _actionButtonLabel;

	private Dictionary<string, List<string>> _loadQueueDict = new Dictionary<string, List<string>>();

	private LoadQueue _loadQueue = new LoadQueue();

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

	public Action<UserListViewPlate, UserInfoBase> PlateCustomize { get; set; }

	public int PanelDepth
	{
		set
		{
			_panel.depth = value;
		}
	}

	public Action<UserInfoBase> OnSelect { get; set; }

	public DialogBase Dialog { get; private set; }

	public static UserListView CreateDialog(GameObject prefab, List<UserInfoBase> userList, Action<UserInfoBase> actionButtonEvent, string actionButtonLabel)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		UserListView component = UnityEngine.Object.Instantiate(prefab).GetComponent<UserListView>();
		dialogBase.SetObj(component.gameObject);
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		component.Initialize(dialogBase, userList, actionButtonEvent, actionButtonLabel);
		return component;
	}

	public static UserListView CreateView(GameObject prefab, GameObject parent, List<UserInfoBase> userList, Action<UserInfoBase> actionButtonEvent, string actionButtonLabel)
	{
		UserListView component = NGUITools.AddChild(parent, prefab).GetComponent<UserListView>();
		component.Initialize(null, userList, actionButtonEvent, actionButtonLabel);
		return component;
	}

	private void Initialize(DialogBase dialog, List<UserInfoBase> userList, Action<UserInfoBase> actionButton, string actionButtonLabel)
	{
		_actionButtonLabel = actionButtonLabel;
		_userList = userList;
		OnSelect = actionButton;
		Dialog = dialog;
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadFriendUserImages(userList, delegate
		{
			_scrollView.CreateScrollView(userList.Count, InitializeInviteFriendPlate);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private void OnDestroy()
	{
		UnloadImages();
	}

	private void UnloadImages()
	{
		if (_loadedResourceList.Count > 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
			_loadedResourceList.Clear();
		}
	}

	private void InitializeInviteFriendPlate(int index, GameObject plateObject)
	{
		UserListViewPlate component = plateObject.GetComponent<UserListViewPlate>();
		component.Initialize(_userList[index], _actionButtonLabel);
		component.SetUnderLine(index < _userList.Count - 1);
		component.OnAction = delegate(UserInfoBase userInfo)
		{
			OnSelect.Call(userInfo);
		};
		PlateCustomize.Call(component, _userList[index]);
	}

	private IEnumerator LoadFriendUserImages(List<UserInfoBase> userList, Action callBack = null)
	{
		List<string> loadPathList = new List<string>();
		List<string> list = new List<string>();
		_loadQueueDict.Clear();
		for (int i = 0; i < userList.Count; i++)
		{
			List<string> list2 = new List<string>(userList[i].GetUserAssetPathList().Except(list).Except(_loadedResourceList));
			if (i < _scrollView.ScrollObjectNum)
			{
				loadPathList.AddRange(list2);
			}
			else
			{
				string text = i.ToString();
				_loadQueueDict.Add(text, list2);
				LoadQueue.Callback onEnd = delegate(string id)
				{
					_loadedResourceList.AddRange(_loadQueueDict[id]);
				};
				_loadQueue.AddToLast(text, list2, null, onEnd);
			}
			list.AddRange(list2);
		}
		if (loadPathList.Count > 0)
		{
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPathList, null));
			_loadedResourceList.AddRange(loadPathList);
		}
		_loadQueue.StartLoad();
		callBack.Call();
	}
}
