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

namespace Wizard;

public class GatheringRanking : MonoBehaviour
{
	[SerializeField]
	private GameObject _rankingPlateOriginal;

	[SerializeField]
	private UIGrid _grid;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private GameObject _rootObject;

	[SerializeField]
	private UILabel _ruleLabel;

	[SerializeField]
	private UILabel _timeLabel;

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

	private void Start()
	{
		_rankingPlateOriginal.SetActive(value: false);
		UIManager.GetInstance().AttachAtlas(base.gameObject);
	}

	public void Show(GatheringInfo info, GatheringJoining parent)
	{
		GatheringRule rule = info.Rule;
		_rootObject.SetActive(value: false);
		GatheringGetSelfInfoTask selfInfoTask = new GatheringGetSelfInfoTask(isDependGatheringInfo: true);
		StartCoroutine(Toolbox.NetworkManager.Connect(selfInfoTask, delegate
		{
			if (!parent.CheckChangeStatus(selfInfoTask.Info))
			{
				GatheringRankingTask task = new GatheringRankingTask();
				UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
				{
					_rootObject.SetActive(value: true);
					StartCoroutine(InitRanking(task));
				}));
			}
			_timeLabel.text = Data.SystemText.Get("Gathering_Information_0014", info.BattleStartTime, info.FinishTime);
			_ruleLabel.text = RoomRuleSetting.GetWinTypeString(rule.BattleParameterInstance.Rule) + "  " + FormatBehaviorManager.GetFormatName(rule.BattleParameterInstance.DeckFormat);
		}));
	}

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

	private IEnumerator LoadImages(GatheringRankingTask task)
	{
		List<GatheringUserInfo> list = new List<GatheringUserInfo>();
		foreach (GatheringRankingTask.RankingUserInfo ranking in task.RankingList)
		{
			list.Add(ranking.gatheringUserInfo);
		}
		List<string> list2 = new List<string>();
		for (int i = 0; i < list.Count; i++)
		{
			list2.AddRange(list[i].GetUserAssetPathList());
		}
		List<string> loadPathList = list2.Distinct().Except(_loadedResourceList).ToList();
		if (loadPathList.Count > 0)
		{
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPathList, null));
			_loadedResourceList.AddRange(loadPathList);
		}
	}

	private IEnumerator InitRanking(GatheringRankingTask task)
	{
		yield return LoadImages(task);
		foreach (GatheringRankingTask.RankingUserInfo ranking in task.RankingList)
		{
			GatheringRankingPlate component = NGUITools.AddChild(_grid.gameObject, _rankingPlateOriginal).GetComponent<GatheringRankingPlate>();
			component.gameObject.SetActive(value: true);
			component.Initialize(ranking);
		}
		_grid.Reposition();
		_scrollView.ResetPosition();
	}
}
