using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.ErrorDialog;
using Wizard.RoomMatch;
using Wizard.UIFriend;

public class RoomInviteReceiveDialog : MonoBehaviour
{
	[SerializeField]
	protected UIWrapContent WrapContents;

	[SerializeField]
	protected WrapContentsScrollBarSize WrapScrollbarSize;

	[SerializeField]
	protected GameObject ScrollViewObject;

	[SerializeField]
	protected GameObject ScrollBarObject;

	[SerializeField]
	protected GameObject ScrollAreaObject;

	protected List<Friend.PlayerData> _playerList;

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

	protected List<string> _loadBookingList = new List<string>();

	protected List<string> _loadedList = new List<string>();

	protected LoadQueue _loadQueue = new LoadQueue();

	protected int _choicePlayerIndex;

	protected DialogBase _dialog;

	protected bool _isJoinRoom;

	private const int LOAD_MIN = 6;

	public MyPageMenu MyPageClass { get; set; }

	public void SetFriendList()
	{
		_playerList = new List<Friend.PlayerData>();
		Friend.PlayerData item = default(Friend.PlayerData);
		foreach (UserFriend friend in Wizard.Data.FriendInfo.data.friendList)
		{
			item.Copy(friend);
			_playerList.Add(item);
		}
		StartCoroutine(LoadCoroutine());
	}

	private IEnumerator LoadCoroutine()
	{
		if (_playerList.Count == 0)
		{
			_dialog = UIManager.GetInstance().CreateDialogClose();
			_dialog.SetTitleLabel(Wizard.Data.SystemText.Get("RoomBattle_0068"));
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
			_dialog.SetText(Wizard.Data.SystemText.Get("RoomBattle_0067"));
			ReturnTopFromError(_playerList.Count);
		}
		else
		{
			_loadedList.Clear();
			_loadBookingList.Clear();
			_loadQueueDict.Clear();
			List<string> list = new List<string>();
			for (int i = 0; i < _playerList.Count; i++)
			{
				string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(_playerList[i].EmblemId.ToString(), ResourcesManager.AssetLoadPathType.Emblem_S);
				string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(_playerList[i].Rank.ToString("00"), ResourcesManager.AssetLoadPathType.RankIcon_S);
				string text = (string.IsNullOrEmpty(_playerList[i].Country) ? null : Toolbox.ResourcesManager.GetAssetTypePath(_playerList[i].Country, ResourcesManager.AssetLoadPathType.Country_S));
				List<string> list2 = new List<string>();
				if (!_loadedList.Contains(assetTypePath) && !list.Contains(assetTypePath))
				{
					list.Add(assetTypePath);
					list2.Add(assetTypePath);
				}
				foreach (string degreeResource in DegreeHelper.GetDegreeResourceList(_playerList[i].DegreeId, DegreeHelper.DegreeType.SMALL, isFetch: false))
				{
					if (!_loadedList.Contains(degreeResource) && !list.Contains(degreeResource))
					{
						list.Add(degreeResource);
						list2.Add(degreeResource);
					}
				}
				if (!_loadedList.Contains(assetTypePath2) && !list.Contains(assetTypePath2))
				{
					list.Add(assetTypePath2);
					list2.Add(assetTypePath2);
				}
				if (text != null && !_loadedList.Contains(text) && !list.Contains(text))
				{
					list.Add(text);
					list2.Add(text);
				}
				if (6 > i)
				{
					_loadBookingList.AddRange(list2);
					continue;
				}
				_loadQueueDict.Add(i, list2);
				LoadQueue.Callback onEnd = delegate(string id)
				{
					_loadedList.AddRange(_loadQueueDict[int.Parse(id)]);
				};
				_loadQueue.AddToLast(i.ToString(), list2, null, onEnd);
			}
			bool loadEndFlg = false;
			StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadBookingList, delegate
			{
				loadEndFlg = true;
			}));
			while (!loadEndFlg)
			{
				yield return null;
			}
			_loadedList.AddRange(_loadBookingList);
			_loadQueue.StartLoad();
			_dialog = UIManager.GetInstance().CreateDialogClose();
			_dialog.SetTitleLabel(Wizard.Data.SystemText.Get("RoomBattle_0069"));
			_dialog.SetObj(base.gameObject);
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
			_dialog.SetSize(DialogBase.Size.M);
			_dialog.OnClose = delegate
			{
				UIManager.GetInstance().StartCoroutine(InviteDialogClose());
			};
			Init();
		}
		UIManager.GetInstance().closeInSceneCenterLoading();
	}

	protected IEnumerator InviteDialogClose()
	{
		if (!_isJoinRoom)
		{
			UIManager.GetInstance().createInSceneCenterLoading();
		}
		_loadQueue.Clear();
		while (_loadQueue.IsClearing)
		{
			yield return null;
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedList);
		if (!_isJoinRoom)
		{
			UIManager.GetInstance().closeInSceneCenterLoading();
		}
	}

	public void Init()
	{
		WrapContents.onInitializeItem = _OnInitializeItem;
		WrapContents.maxIndex = 0;
		ScrollBarObject.GetComponent<UIScrollBarWrapContent>().m_WrapContents = WrapContents;
		WrapContents.minIndex = -(_playerList.Count - 1);
		WrapScrollbarSize.ContentUpdate();
		WrapContents.SortBasedOnScrollMovement();
		UIPanel component = ScrollViewObject.GetComponent<UIPanel>();
		int count = _playerList.Count;
		if (component.height > (float)(count * WrapContents.itemSize))
		{
			ScrollBarObject.SetActive(value: false);
			ScrollAreaObject.SetActive(value: false);
			ScrollViewObject.GetComponent<UIScrollView>().ResetPosition();
		}
		else
		{
			ScrollBarObject.SetActive(value: true);
			ScrollAreaObject.SetActive(value: true);
		}
		ScrollViewObject.GetComponent<UIScrollView>().ResetPosition();
		ScrollBarObject.GetComponent<UIScrollBar>().value = 0f;
	}

	protected void _OnInitializeItem(GameObject go, int wrapIndex, int realIndex)
	{
		int num = realIndex * -1;
		go.name = num.ToString();
		Transform[] componentsInChildren = go.GetComponentsInChildren<Transform>(includeInactive: true);
		Transform[] array;
		if (num >= _playerList.Count || num < 0)
		{
			array = componentsInChildren;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].gameObject.SetActive(value: false);
			}
			return;
		}
		array = componentsInChildren;
		for (int i = 0; i < array.Length; i++)
		{
			array[i].gameObject.SetActive(value: true);
		}
		go.transform.GetComponent<RoomInviteFriendColum>().SetPlayerData(_playerList[num], _loadedList);
		EventDelegate eventDelegate = new EventDelegate(this, "RoomJoin");
		eventDelegate.parameters[0].value = num;
		RoomInviteFriendColum component = go.GetComponent<RoomInviteFriendColum>();
		component.ButtonObject.onClick.Clear();
		component.ButtonObject.onClick.Add(eventDelegate);
		if (num + 1 == _playerList.Count)
		{
			component.UnderLineObject.SetActive(value: false);
		}
		else
		{
			component.UnderLineObject.SetActive(value: true);
		}
	}

	protected void RoomJoin(int inPlayerIndex)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		InviteAcceptTask inviteAcceptTask = new InviteAcceptTask();
		inviteAcceptTask.SetParameter(_playerList[inPlayerIndex].ViewerId);
		inviteAcceptTask.SkipAllCuteResultCodeCheckErrorPopup();
		StartCoroutine(Toolbox.NetworkManager.Connect(inviteAcceptTask, AcceptSuccess, null, ErrorDialog));
	}

	private void AcceptSuccess(NetworkTask.ResultCode inResultCode)
	{
		if (!(_dialog == null))
		{
			_dialog.Close();
			_isJoinRoom = true;
			RoomConnectController.InitializeParameter param = new RoomConnectController.InitializeParameter(RoomConnectController.PositionMode.VISITOR, new BattleParameter(NetworkDefine.ServerBattleType.OpenRoom, Format.Max, TwoPickFormat.None, Wizard.Data.InviteFriendBattle.BattleParameterInstance.Rule, isOpenDeckRoom: false), Wizard.Data.InviteFriendBattle.RoomId);
			UIManager.GetInstance().StartCoroutine(MyPageMenu.Instance.BattleMenu.JoinRoom(param, isInvite: true));
		}
	}

	protected void ErrorDialog(int inErrorNo)
	{
		ReturnTopFromError(Wizard.Data.Load.data._receiveInviteCount);
		_dialog.Close();
		Dialog.Create(inErrorNo);
	}

	protected void ReturnTopFromError(int inInviteCount)
	{
		if (inInviteCount <= 1)
		{
			InviteReset();
		}
	}

	protected void InviteReset()
	{
		Object.Destroy(base.gameObject);
	}
}
