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

namespace Wizard;

public class GatheringInvite : MonoBehaviour
{
	[SerializeField]
	private UIButton _userIdSearchButton;

	[SerializeField]
	private UIButton _friendInviteButton;

	[SerializeField]
	private GameObject _idInputDialogPrefab;

	[SerializeField]
	private GameObject _userDataDialogPrefab;

	[SerializeField]
	private GameObject _friendListDialogPrefab;

	[SerializeField]
	private UserListView _friendViewOriginal;

	[SerializeField]
	private GameObject _inviteNoneRoot;

	[SerializeField]
	private UIButton _twitterButton;

	[SerializeField]
	private UILabel _twitterButtonText;

	private GatheringInviteUserListTask _inviteUserListTask;

	private GatheringJoining _parent;

	private GatheringGetSelfInfoTask _selfInfoTask;

	private const int ID_INPUT_PANEL_DEPTH = 19;

	private const int FRIEND_SELECT_PANEL_DEPTH = 30;

	private const int FRIEND_INVITE_CONFIRM_PANEL_DEPTH = 30;

	private void Start()
	{
		_userIdSearchButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickUserIdSearch();
		}));
		_friendInviteButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickFriendInviteButton();
		}));
		_twitterButtonText.text = Data.SystemText.Get("RoomBattle_0185");
		_twitterButton.onClick.Add(new EventDelegate(delegate
		{
			CopyInviteText();
		}));
	}

	public void Show(GatheringJoining parent)
	{
		_parent = parent;
		_selfInfoTask = new GatheringGetSelfInfoTask(isDependGatheringInfo: true);
		StartCoroutine(Toolbox.NetworkManager.Connect(_selfInfoTask, delegate
		{
			if (!parent.CheckChangeStatus(_selfInfoTask.Info))
			{
				GatheringInviteUserListTask inviteListTask = new GatheringInviteUserListTask();
				StartCoroutine(Toolbox.NetworkManager.Connect(inviteListTask, delegate
				{
					OnReceiveInviteList(inviteListTask);
				}));
			}
		}));
	}

	private void OnReceiveInviteList(GatheringInviteUserListTask task)
	{
		_inviteUserListTask = task;
		_inviteNoneRoot.SetActive(task.InviteUserInfoList.Count == 0);
		string actionButtonLabel = Data.SystemText.Get("Guild_List_0014");
		List<UserInfoBase> list = new List<UserInfoBase>();
		foreach (GatheringInviteUserListTask.InviteUserInfo inviteUserInfo in task.InviteUserInfoList)
		{
			list.Add(inviteUserInfo.userInfo);
		}
		UserListView.CreateView(_friendViewOriginal.gameObject, base.gameObject, list, OnClickInviteCancel, actionButtonLabel);
	}

	private void OnClickInviteCancel(UserInfoBase userInfo)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		SystemText systemText = Data.SystemText;
		UserDataDialog userDataDialog = UserDataDialog.Create(_userDataDialogPrefab, userInfo, systemText.Get("Gathering_Invite_0002"));
		DialogBase dialog = userDataDialog.Dialog;
		userDataDialog.SetDiscriptionLabel(systemText.Get("Guild_List_0015"));
		dialog.SetButtonText(systemText.Get("Guild_List_0016"));
		dialog.SetPanelDepth(30);
		dialog.onPushButton1 = delegate
		{
			InviteCancel(userInfo as GatheringUserInfo);
		};
	}

	private void InviteCancel(GatheringUserInfo userInfo)
	{
		GatheringInviteCancelTask gatheringInviteCancelTask = new GatheringInviteCancelTask();
		gatheringInviteCancelTask.SetParameter(_inviteUserListTask.GetInviteIdFromViewerId(userInfo.ViewerId));
		StartCoroutine(Toolbox.NetworkManager.Connect(gatheringInviteCancelTask, delegate
		{
			_parent.ReOpenCurrentCategory();
		}));
	}

	private void OnClickFriendInviteButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		GatheringFriendListTask task = new GatheringFriendListTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			OnReceiveFriendList(task);
		}));
	}

	private void OnReceiveFriendList(GatheringFriendListTask task)
	{
		if (task.InviteFriendList.Count <= 0)
		{
			ShowNoFriendDialog();
			return;
		}
		List<UserInfoBase> list = new List<UserInfoBase>();
		foreach (GatheringFriendListTask.InviteFriendData inviteFriend in task.InviteFriendList)
		{
			list.Add(inviteFriend);
		}
		string actionButtonLabel = Data.SystemText.Get("Guild_List_0007");
		UserListView friendList = UserListView.CreateDialog(_friendListDialogPrefab, list, OnSelectInviteFriend, actionButtonLabel);
		friendList.Dialog.SetPanelDepth(30);
		friendList.Dialog.SetTitleLabel(Data.SystemText.Get("Guild_List_0006"));
		friendList.PanelDepth = 31;
		friendList.PlateCustomize = FriendListePlateCustomize;
		friendList.OnSelect = delegate(UserInfoBase userInfo)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			OnSelectInviteFriend(userInfo);
			friendList.Dialog.Close();
		};
	}

	private void FriendListePlateCustomize(UserListViewPlate friendListPlate, UserInfoBase userInfoBase)
	{
		GatheringFriendListTask.InviteFriendData inviteData = userInfoBase as GatheringFriendListTask.InviteFriendData;
		friendListPlate.gameObject.GetComponent<GatheringFriendInviteListPlate>().Initialize(inviteData);
	}

	private void OnSelectInviteFriend(UserInfoBase userInfo)
	{
		SystemText systemText = Data.SystemText;
		UserDataDialog userDataDialog = UserDataDialog.Create(_userDataDialogPrefab, userInfo, systemText.Get("Gathering_Invite_0002"));
		DialogBase dialog = userDataDialog.Dialog;
		userDataDialog.SetDiscriptionLabel(systemText.Get("Gathering_Invite_0003"));
		dialog.SetButtonText(Data.SystemText.Get("Guild_List_0011"));
		dialog.SetPanelDepth(30);
		dialog.onPushButton1 = delegate
		{
			Invite(userInfo as GatheringUserInfo);
		};
	}

	private void ShowNoFriendDialog()
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetText(Data.SystemText.Get("Gathering_Invite_0005"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetSize(DialogBase.Size.S);
	}

	private void OnClickUserIdSearch()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		GuildInputViewerIdDialog inputDialog = Object.Instantiate(_idInputDialogPrefab).GetComponent<GuildInputViewerIdDialog>();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(Data.SystemText.Get("Guild_List_0002"));
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.SetObj(inputDialog.gameObject);
		dialogBase.SetPanelDepth(19);
		inputDialog.InitializeDialog(dialogBase);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(Data.SystemText.Get("Guild_List_0005"));
		dialogBase.onPushButton1 = delegate
		{
			FindViewerIdUser(inputDialog.InputValue);
		};
	}

	private void FindViewerIdUser(int search_viewer_id)
	{
		FriendUserSearchTask friendUserSearchTask = new FriendUserSearchTask();
		friendUserSearchTask.SetParameter(search_viewer_id);
		StartCoroutine(Toolbox.NetworkManager.Connect(friendUserSearchTask, delegate
		{
			GatheringUserInfo userInfo = new GatheringUserInfo(Data.SearchUserInfo.data.user);
			SystemText systemText = Data.SystemText;
			UserDataDialog userDataDialog = UserDataDialog.Create(_userDataDialogPrefab, userInfo, systemText.Get("Gathering_Invite_0002"));
			DialogBase dialog = userDataDialog.Dialog;
			userDataDialog.SetDiscriptionLabel(systemText.Get("Gathering_Invite_0003"));
			dialog.SetButtonText(Data.SystemText.Get("Guild_List_0011"));
			dialog.onPushButton1 = delegate
			{
				Invite(userInfo);
			};
		}));
	}

	private void Invite(GatheringUserInfo user)
	{
		GatheringInviteTask gatheringInviteTask = new GatheringInviteTask();
		gatheringInviteTask.SetParameter(user.ViewerId.ToString());
		StartCoroutine(Toolbox.NetworkManager.Connect(gatheringInviteTask, delegate
		{
			OnSuccessInvite();
		}));
	}

	private void OnSuccessInvite()
	{
		UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("Gathering_Invite_0004")).OnClose = delegate
		{
			_parent.ReOpenCurrentCategory();
		};
	}

	private void CopyInviteText()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		CreateDialog(Data.SystemText.Get("RoomBattle_0186"), Data.SystemText.Get("Gathering_0044"));
		NativePluginWrapper.SetStringToClipboard(GetTweetText());
	}

	private string GetTweetText()
	{
		string hashTags = _selfInfoTask._hashTags;
		GatheringInfo info = _selfInfoTask.Info;
		string gatheringTypeString = GatheringUtility.GetGatheringTypeString(info);
		string text = GetFormatTexForHashTag(info.Rule.BattleParameterInstance.DeckFormat) + "/" + RoomRuleSetting.GetWinTypeString(info.Rule.BattleParameterInstance.Rule);
		return Data.SystemText.Get("Gathering_0043", hashTags, info.Id, gatheringTypeString, text, info.BattleStartTimeShort);
	}

	private string GetFormatTexForHashTag(Format deckFormat)
	{
		return deckFormat switch
		{
			Format.Hof => Data.SystemText.Get("RoomBattle_0188"), 
			Format.Windfall => Data.SystemText.Get("RoomBattle_0193"), 
			Format.Crossover => Data.SystemText.Get("RoomBattle_0194"), 
			Format.MyRotation => Data.SystemText.Get("RoomBattle_0205"), 
			Format.Avatar => Data.SystemText.Get("RoomBattle_0208"), 
			_ => FormatBehaviorManager.GetFormatName(deckFormat), 
		};
	}

	private void CreateDialog(string title, string msg)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(title);
		dialogBase.SetText(msg);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
	}
}
