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

namespace Wizard;

public class GatheringMemberList : MonoBehaviour
{
	private List<string> _loadedResourceList = new List<string>();

	[SerializeField]
	private SimpleScrollViewUI _memberListScrollView;

	[SerializeField]
	private GameObject _userDataDialog;

	[SerializeField]
	private GameObject _kickConfirmPrefab;

	[SerializeField]
	private UILabel _memberCount;

	[SerializeField]
	private GameObject _memberNoneObject;

	private GatheringJoining _parent;

	private GatheringGetSelfInfoTask _task;

	public void Show(GatheringJoining parent)
	{
		_parent = parent;
		GatheringGetSelfInfoTask task = new GatheringGetSelfInfoTask(isDependGatheringInfo: true);
		_memberCount.gameObject.SetActive(value: false);
		_memberNoneObject.SetActive(value: false);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			_memberCount.gameObject.SetActive(value: true);
			OnReceiveInfo(task);
		}));
	}

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

	private void OnReceiveInfo(GatheringGetSelfInfoTask task)
	{
		_parent.CheckChangeStatus(task.Info);
		_task = task;
		UpdateMemberList(task);
		GatheringInfo info = task.Info;
		_memberCount.text = Data.SystemText.Get("Guild_0005", info.CurrentMemberCount.ToString(), info.Rule.MaxMember.ToString());
		_memberNoneObject.SetActive(info.CurrentMemberCount == 0);
	}

	private void UpdateMemberList(GatheringGetSelfInfoTask task)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadImages(task, delegate
		{
			_memberListScrollView.CreateScrollView(task.Info.MemberList.Count, InitializePlate);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private IEnumerator LoadImages(GatheringGetSelfInfoTask task, Action callBack = null)
	{
		List<GatheringUserInfo> memberList = task.Info.MemberList;
		List<string> list = new List<string>();
		for (int i = 0; i < memberList.Count; i++)
		{
			GatheringUserInfo gatheringUserInfo = memberList[i];
			list.AddRange(gatheringUserInfo.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 InitializePlate(int index, GameObject plate)
	{
		GatheringMemberPlate component = plate.GetComponent<GatheringMemberPlate>();
		component.Initialize(_task.Info, _task.Info.MemberList[index]);
		component.OnClickFriendRequest = OnClickFriendRequest;
		component.OnClickKick = OnClickKickButton;
		component.OnClickDropOut = OnClickDropOutButton;
	}

	private DialogBase CreateMemberActionDialog(GatheringUserInfo member, string title, string discription, string decideText, Action onDecide)
	{
		GuildUserDataDialog component = UnityEngine.Object.Instantiate(_userDataDialog).GetComponent<GuildUserDataDialog>();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetTitleLabel(title);
		dialogBase.SetObj(component.gameObject);
		component.SetUserData(member);
		component.SetDiscriptionLabel(discription);
		dialogBase.SetButtonText(decideText);
		dialogBase.onPushButton1 = delegate
		{
			onDecide();
		};
		return dialogBase;
	}

	private void OnClickFriendRequest(GatheringUserInfo member)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		Action onDecide = delegate
		{
			ApplyFriend(member.ViewerId);
		};
		SystemText systemText = Data.SystemText;
		string title = systemText.Get("Guild_Profile_0014");
		string discription = systemText.Get("Guild_Profile_0015");
		string decideText = systemText.Get("OtherFriend_0032");
		CreateMemberActionDialog(member, title, discription, decideText, onDecide);
	}

	private void OnClickKickButton(GatheringUserInfo member)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		Action<bool> onKickDecide = delegate(bool kickEternal)
		{
			Kick(member, kickEternal);
		};
		GatheringKickConfirm.Create(_kickConfirmPrefab, member, onKickDecide);
	}

	private void Kick(GatheringUserInfo member, bool kickEternal)
	{
		GatheringKickTask gatheringKickTask = new GatheringKickTask();
		gatheringKickTask.SetParameter(member.ViewerId.ToString(), kickEternal);
		StartCoroutine(Toolbox.NetworkManager.Connect(gatheringKickTask, delegate
		{
			OnKickSuccess();
		}));
	}

	private void OnKickSuccess()
	{
		UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("Gathering_Member_0006")).OnCloseStart = delegate
		{
			_parent.ReOpenCurrentCategory();
		};
	}

	private void OnClickDropOutButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		SystemText systemText = Data.SystemText;
		string titleLabel = systemText.Get("Gathering_Member_0011");
		string message = systemText.Get("Gathering_Member_0008");
		string text_btn = systemText.Get("Gathering_Member_0009");
		DialogBase dialogBase = UIManager.GetInstance().CreateConfirmationDialog(message);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.RedBtn_CancelBtn);
		dialogBase.SetButtonText(text_btn);
		dialogBase.SetTitleLabel(titleLabel);
		dialogBase.onPushButton1 = delegate
		{
			DropOut();
		};
	}

	private void DropOut()
	{
		GatheringLeaveTask task = new GatheringLeaveTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			string message = Data.SystemText.Get("Gathering_Member_0010");
			UIManager.GetInstance().CreateConfirmationDialog(message).OnCloseStart = delegate
			{
				Gathering.BackToMyPageForDrop();
			};
		}));
	}

	private void ApplyFriend(int viewerId)
	{
		FriendApplySendTask friendApplySendTask = new FriendApplySendTask();
		friendApplySendTask.SetParameter(viewerId);
		StartCoroutine(Toolbox.NetworkManager.Connect(friendApplySendTask, OnSuccessApplyFriend));
	}

	private void OnSuccessApplyFriend(NetworkTask.ResultCode code)
	{
		UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("Guild_Profile_0016"));
		_parent.ReOpenCurrentCategory();
	}
}
