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

namespace Wizard;

public class ChatReplayListDialog : MonoBehaviour
{
	[SerializeField]
	private SimpleScrollViewUI _replayListScrollView;

	[SerializeField]
	private UILabel _labelNoReplay;

	private List<ReplayInfoItem> _listReplayInfoItem;

	private Action<ReplayInfoItem> _onClickReplaySendBtn;

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

	public void Init(List<ReplayInfoItem> listReplayInfoItem, Action<ReplayInfoItem> onClickReplaySendBtn)
	{
		_listReplayInfoItem = listReplayInfoItem;
		_onClickReplaySendBtn = onClickReplaySendBtn;
		CreateReplayList();
	}

	private void CreateReplayList()
	{
		bool flag = _listReplayInfoItem.Count <= 0;
		_replayListScrollView.SetVisiable(!flag);
		_labelNoReplay.gameObject.SetActive(flag);
		if (!flag)
		{
			LoadResources(delegate
			{
				_replayListScrollView.CreateScrollView(_listReplayInfoItem.Count, OnInitializeContent);
			});
		}
	}

	private void OnInitializeContent(int index, GameObject plate)
	{
		ReplayInfoItem replayInfo = _listReplayInfoItem[index];
		plate.GetComponent<ChatReplayListDialogContent>().SetData(replayInfo, delegate
		{
			_onClickReplaySendBtn.Call(replayInfo);
		});
	}

	private void LoadResources(Action callBack = null)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		List<string> list = new List<string>();
		foreach (ReplayInfoItem item in _listReplayInfoItem)
		{
			list.Add(Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentEmblemId, ResourcesManager.AssetLoadPathType.Emblem_S));
			if (!string.IsNullOrEmpty(item.OpponentCountryCode))
			{
				list.Add(Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentCountryCode, ResourcesManager.AssetLoadPathType.Country_S));
			}
		}
		List<string> loadPathList = list.Distinct().Except(_loadedResourceList).ToList();
		if (loadPathList.Count > 0)
		{
			StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPathList, delegate
			{
				_loadedResourceList.AddRange(loadPathList);
				UIManager.GetInstance().closeInSceneCenterLoading();
				callBack.Call();
			}));
		}
	}

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

	private void OnDestroy()
	{
		UnloadResources();
	}
}
