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

namespace Wizard;

public class GatheringReceiveInviteList : MonoBehaviour
{
	[SerializeField]
	private SimpleScrollViewUI _inviteListScrollView;

	[SerializeField]
	private GameObject _infoDialogPrefab;

	[SerializeField]
	private GameObject _inviteNoneRoot;

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

	private GatheringGetReceiveInviteTask _receiveInviteTask;

	private GatheringEntry _parent;

	public void Show(GatheringEntry parent)
	{
		_parent = parent;
		_receiveInviteTask = new GatheringGetReceiveInviteTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(_receiveInviteTask, delegate
		{
			OnReceiveInfo(_receiveInviteTask);
		}));
	}

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

	private void OnReceiveInfo(GatheringGetReceiveInviteTask task)
	{
		_parent.UpdateInviteBadge(task);
		_inviteNoneRoot.SetActive(task.InviteList.Count == 0);
		UIManager.GetInstance()._Footer.UpdateArenaBadgeIcon();
		UpdateInviteList(task);
	}

	private void InitializePlate(int index, GameObject plate)
	{
		UserListViewPlate component = plate.GetComponent<UserListViewPlate>();
		component.Initialize(_receiveInviteTask.InviteList[index].GatherintUserInfo, Data.SystemText.Get("Gathering_Menu_0002"));
		component.OnAction = delegate
		{
			OnClickInviteGatheringInfoButton(_receiveInviteTask.InviteList[index]);
		};
	}

	private void UpdateInviteList(GatheringGetReceiveInviteTask task)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadImages(task, delegate
		{
			_inviteListScrollView.CreateScrollView(task.InviteList.Count, InitializePlate);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private IEnumerator LoadImages(GatheringGetReceiveInviteTask task, Action callBack = null)
	{
		List<GatheringGetReceiveInviteTask.UserInfo> inviteList = task.InviteList;
		List<string> list = new List<string>();
		for (int i = 0; i < inviteList.Count; i++)
		{
			GatheringUserInfo gatherintUserInfo = inviteList[i].GatherintUserInfo;
			list.AddRange(gatherintUserInfo.GetUserAssetPathList());
		}
		List<string> loadPathList = list.Distinct().Except(_loadedResourceList).ToList();
		if (loadPathList.Count > 0)
		{
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPathList, null));
			_loadedResourceList.AddRange(loadPathList);
		}
		callBack.Call();
	}

	private void OnClickInviteGatheringInfoButton(GatheringGetReceiveInviteTask.UserInfo userInfo)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		Action onReject = delegate
		{
			OnClickRejectInvite(userInfo);
		};
		GatheringReceiveInfoDialog.Create(_infoDialogPrefab, userInfo.GatheringId, onReject);
	}

	private void OnClickRejectInvite(GatheringGetReceiveInviteTask.UserInfo userInfo)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("Gathering_Join_0005"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.RedBtn_CancelBtn);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Gathering_Menu_0004"));
		dialogBase.SetButtonText(Data.SystemText.Get("Gathering_Join_0004"));
		dialogBase.onPushButton1 = delegate
		{
			InviteReject(userInfo);
		};
	}

	private void InviteReject(GatheringGetReceiveInviteTask.UserInfo userInfo)
	{
		GatheringInviteRejectTask gatheringInviteRejectTask = new GatheringInviteRejectTask();
		gatheringInviteRejectTask.SetParameter(userInfo.InviteId);
		StartCoroutine(Toolbox.NetworkManager.Connect(gatheringInviteRejectTask, delegate
		{
			UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("Gathering_Join_0006")).OnClose = delegate
			{
				_parent.ReOpenInvite();
			};
		}));
	}
}
