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

namespace Wizard;

public class ReplayDialog : MonoBehaviour
{
	[SerializeField]
	private GameObject _replayDialogContent;

	[SerializeField]
	private GameObject _noReplayLabel;

	[SerializeField]
	private UIWrapContent _wrapContent;

	[SerializeField]
	private UIScrollView _scrollView;

	public Action OnClickReplayButton;

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

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

	private LoadQueue _loadQueue = new LoadQueue();

	private List<ReplayInfoItem> _items;

	public static void Create()
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(UIManager.GetInstance().ReplayDialogPrefab);
		ReplayDialog script = gameObject.GetComponent<ReplayDialog>();
		DialogBase dialog = UIManager.GetInstance().CreateDialogClose();
		dialog.SetSize(DialogBase.Size.XL);
		dialog.SetTitleLabel(Data.SystemText.Get("OtherTop_0033"));
		dialog.SetObj(gameObject);
		dialog.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialog.SetDisp(inDisp: false);
		script.SetupContents(Data.ReplayInfo.Items, delegate
		{
			dialog.SetDisp(inDisp: true);
			script.OnClickReplayButton = delegate
			{
				dialog.Close();
			};
		});
	}

	public void SetupContents(List<ReplayInfoItem> items, Action onLoaded)
	{
		_items = items;
		_wrapContent.onInitializeItem = OnInitializeItem;
		_noReplayLabel.SetActive(items.Count == 0);
		_wrapContent.minIndex = -(items.Count - 1);
		_wrapContent.maxIndex = 0;
		int num = (int)(_scrollView.panel.height / (float)_wrapContent.itemSize) + 1;
		bool active = num <= items.Count;
		num = Math.Min(num, items.Count);
		for (int i = 0; i < num; i++)
		{
			string emblemTexturePath = GetEmblemTexturePath(_items[i], isfetch: false);
			string countryTexturePath = GetCountryTexturePath(_items[i], isfetch: false);
			if (!_initTextures.Contains(emblemTexturePath))
			{
				_initTextures.Add(emblemTexturePath);
			}
			if (!string.IsNullOrEmpty(countryTexturePath) && !_initTextures.Contains(countryTexturePath))
			{
				_initTextures.Add(countryTexturePath);
			}
		}
		List<string> list = new List<string>();
		for (int j = num; j < items.Count; j++)
		{
			string emblemTexturePath2 = GetEmblemTexturePath(_items[j], isfetch: false);
			string countryTexturePath2 = GetCountryTexturePath(_items[j], isfetch: false);
			if (!_initTextures.Contains(emblemTexturePath2) && !list.Contains(emblemTexturePath2))
			{
				list.Add(emblemTexturePath2);
			}
			if (!string.IsNullOrEmpty(countryTexturePath2) && !_initTextures.Contains(countryTexturePath2) && !list.Contains(countryTexturePath2))
			{
				list.Add(countryTexturePath2);
			}
		}
		for (int k = 0; k < list.Count; k++)
		{
			string path = list[k];
			_loadQueue.AddToLast(path, new List<string> { path }, null, delegate
			{
				_loadedTextures.Add(path);
			});
		}
		if (num == 1)
		{
			GameObject gameObject = AddContent(_scrollView.gameObject);
			StartCoroutine(gameObject.GetComponent<ReplayDialogContent>().Setup(_items[0], _loadedTextures));
		}
		else
		{
			for (int num2 = 0; num2 < num; num2++)
			{
				AddContent(_wrapContent.gameObject);
			}
		}
		_scrollView.ResetPosition();
		_scrollView.enabled = active;
		_scrollView.verticalScrollBar.gameObject.SetActive(active);
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_initTextures, delegate
		{
			_loadedTextures.AddRange(_initTextures);
			_loadQueue.StartLoad();
			onLoaded.Call();
		}));
	}

	private void OnInitializeItem(GameObject go, int wrapIndex, int realIndex)
	{
		ReplayInfoItem item = _items[-realIndex];
		StartCoroutine(go.GetComponent<ReplayDialogContent>().Setup(item, _loadedTextures));
	}

	private string GetEmblemTexturePath(ReplayInfoItem item, bool isfetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentEmblemId, ResourcesManager.AssetLoadPathType.Emblem_S, isfetch);
	}

	private string GetCountryTexturePath(ReplayInfoItem item, bool isfetch)
	{
		if (string.IsNullOrEmpty(item.OpponentCountryCode))
		{
			return "";
		}
		return Toolbox.ResourcesManager.GetAssetTypePath(item.OpponentCountryCode, ResourcesManager.AssetLoadPathType.Country_S, isfetch);
	}

	private void OnDestroy()
	{
		UIManager.GetInstance().StartCoroutine(UnloadTextures());
	}

	private IEnumerator UnloadTextures()
	{
		_loadQueue.Clear();
		while (_loadQueue.IsClearing)
		{
			yield return null;
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedTextures);
	}

	private GameObject AddContent(GameObject gameObject)
	{
		GameObject obj = NGUITools.AddChild(gameObject, _replayDialogContent);
		ReplayDialogContent component = obj.GetComponent<ReplayDialogContent>();
		component.GetComponent<UIDragScrollView>().scrollView = _scrollView;
		component.OnClickReplayButton = delegate
		{
			OnClickReplayButton.Call();
		};
		return obj;
	}
}
