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

namespace Wizard;

public class GatheringChatAutoJoinRoomMatch : MonoBehaviour, IChatActionUI
{
	[SerializeField]
	private UIButton _buttonAutoJoinRoomMatch;

	[SerializeField]
	private UILabel _labelAutoJoinRoomMatchBtn;

	private ChatConnectController _chatConnectController;

	private GatheringChatSettings _gatheringChatSettings;

	private Action _startCreateRoomAction;

	private GatheringMatchedRoom _tournamentMatchedRoom;

	private const float RETRY_INTERVAL = 1f;

	private const int RETRY_COUNT_MAX = 5;

	private int _tournamentApiRetryCount;

	public void SetViewTournamentRoomMatch(GatheringMatchedRoom matchedRoom)
	{
		_tournamentMatchedRoom = matchedRoom;
		UIManager.SetObjectToGrey(_buttonAutoJoinRoomMatch.gameObject, !matchedRoom.IsMatched);
	}

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatConnectController = chatConnectController;
		_gatheringChatSettings = chatSettings as GatheringChatSettings;
		if (_gatheringChatSettings.SendRoomMatchUI != null)
		{
			_startCreateRoomAction = _gatheringChatSettings.SendRoomMatchUI.StartCreateRoom;
		}
		if (_gatheringChatSettings.GatheringInfo.Rule.IsTournament)
		{
			_labelAutoJoinRoomMatchBtn.text = Data.SystemText.Get("Gathering_Chat_0022");
		}
		else
		{
			_labelAutoJoinRoomMatchBtn.text = Data.SystemText.Get("Gathering_Chat_0002");
		}
		_buttonAutoJoinRoomMatch.onClick.Clear();
		_buttonAutoJoinRoomMatch.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			EnterAutoJoinRoomMatch();
		}));
	}

	public void EnterAutoJoinRoomMatch()
	{
		if (GatheringChat.IsMaintenance(Data.MaintenanceCodeList, _gatheringChatSettings.GatheringInfo.Rule))
		{
			Wizard.ErrorDialog.Dialog.Create(2030);
			return;
		}
		GatheringInfo gatheringInfo = _gatheringChatSettings.GatheringInfo;
		if (gatheringInfo.Rule.Type == GatheringRule.eType.FREE_BATTLE)
		{
			UIManager.GetInstance().StartCoroutine(EnterVacancyRoom(gatheringInfo));
		}
		else if (gatheringInfo.Rule.Type == GatheringRule.eType.TOURNAMENT)
		{
			if (_tournamentMatchedRoom == null || !_tournamentMatchedRoom.IsMatched)
			{
				DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
				Wizard.ErrorDialog.Dialog.Setup(dialogBase, 5315.ToString());
				dialogBase.SetPanelDepth(5400);
				dialogBase.onPushButton1 = delegate
				{
					UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.MyPage);
				};
			}
			else
			{
				EnterTournamentRoom(gatheringInfo);
			}
		}
		else
		{
			Debug.LogError("ユーザー大会タイプが不正です。:" + gatheringInfo.Rule.Type);
		}
	}

	private IEnumerator EnterVacancyRoom(GatheringInfo gatheringInfo)
	{
		bool isRoomReady = false;
		int retryCount = 0;
		UIManager.GetInstance().createInSceneCenterLoading(notBlack: true);
		_chatConnectController.PausePolling();
		GatheringAutoJoinTaskInfo gatheringAutoJoinTaskInfo = null;
		string battleId = "";
		while (!isRoomReady)
		{
			GatheringRoomEnterVacancyRoomTask enterVacancyRoomTask = new GatheringRoomEnterVacancyRoomTask(gatheringInfo);
			yield return UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(enterVacancyRoomTask));
			gatheringAutoJoinTaskInfo = enterVacancyRoomTask.GatheringAutoJoinTaskInfo;
			if (!enterVacancyRoomTask.IsResultSuccess)
			{
				_chatConnectController.ResetPolling();
				UIManager.GetInstance().closeInSceneCenterLoading();
				yield break;
			}
			battleId = enterVacancyRoomTask.BattleID;
			if (gatheringAutoJoinTaskInfo.NeedsRetry)
			{
				int num = retryCount + 1;
				retryCount = num;
				if (num > 5)
				{
					_chatConnectController.ResetPolling();
					UIManager.GetInstance().closeInSceneCenterLoading();
					_startCreateRoomAction.Call();
					yield break;
				}
				yield return new WaitForSeconds(1f);
			}
			else
			{
				isRoomReady = true;
			}
		}
		RoomConnectController.InitializeParameter initializeParameter = new RoomConnectController.InitializeParameter((!gatheringAutoJoinTaskInfo.IsOwner) ? RoomConnectController.PositionMode.VISITOR : RoomConnectController.PositionMode.OWNER, roomId: gatheringAutoJoinTaskInfo.IsOwner ? "" : gatheringAutoJoinTaskInfo.RoomId, battleParameter: gatheringInfo.Rule.BattleParameterInstance);
		initializeParameter.IsGathering = true;
		initializeParameter.GatheringAutoJoinTaskInfo = gatheringAutoJoinTaskInfo;
		UIManager.GetInstance().StartCoroutine(GatheringUtility.JoinRoom(initializeParameter, battleId));
	}

	private void EnterTournamentRoom(GatheringInfo gatheringInfo)
	{
		UIManager.GetInstance().createInSceneCenterLoading(notBlack: true);
		_chatConnectController.PausePolling();
		_tournamentApiRetryCount = 0;
		GatheringRoomEnterTournamentRoom(gatheringInfo);
	}

	private void GatheringRoomEnterTournamentRoom(GatheringInfo gatheringInfo)
	{
		GatheringRoomEnterTournamentRoomTask tournamentTask = new GatheringRoomEnterTournamentRoomTask(gatheringInfo);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(tournamentTask, delegate
		{
			UIManager.GetInstance().StartCoroutine(EnterTournamentSuccess(tournamentTask, gatheringInfo));
		}, null, delegate
		{
			_chatConnectController.ResetPolling();
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private IEnumerator EnterTournamentSuccess(GatheringRoomEnterTournamentRoomTask tournamentTask, GatheringInfo gatheringInfo)
	{
		GatheringAutoJoinTaskInfo gatheringAutoJoinTaskInfo = tournamentTask.GatheringAutoJoinTaskInfo;
		if (gatheringAutoJoinTaskInfo.NeedsRetry)
		{
			_tournamentApiRetryCount++;
			if (_tournamentApiRetryCount > 5)
			{
				_chatConnectController.ResetPolling();
				UIManager.GetInstance().closeInSceneCenterLoading();
				Wizard.ErrorDialog.Dialog.Create("TIMEOUT_NORETRY");
			}
			else
			{
				yield return new WaitForSeconds(1f);
				GatheringRoomEnterTournamentRoom(gatheringInfo);
			}
		}
		else
		{
			RoomConnectController.InitializeParameter initializeParameter = new RoomConnectController.InitializeParameter((!gatheringAutoJoinTaskInfo.IsOwner) ? RoomConnectController.PositionMode.VISITOR : RoomConnectController.PositionMode.OWNER, roomId: gatheringAutoJoinTaskInfo.IsOwner ? "" : gatheringAutoJoinTaskInfo.RoomId, battleParameter: gatheringInfo.Rule.BattleParameterInstance);
			initializeParameter.IsGathering = true;
			initializeParameter.GatheringAutoJoinTaskInfo = gatheringAutoJoinTaskInfo;
			UIManager.GetInstance().StartCoroutine(GatheringUtility.JoinRoom(initializeParameter, tournamentTask.BattleID));
		}
	}
}
