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

public class MatchingNetworkConnectChecker : MatchingIntervalActionBase
{
	public Action OnConnectError;

	public Action OnTimeoutInitNetwork;

	private int _connectFailCount;

	private string _doMatchingBattleId;

	private int _initNetworkSendNum;

	private long _initNetworkTimer;

	private bool _isConnectOnly;

	private bool _isNotInitBattle;

	private bool _isBeforeBattleStartRetry;

	private int _phase;

	private const int NETWORKOBJECT_CREATE_PHASE = 0;

	private const int CONNECT_NETWORK_PHASE = 1;

	private const int WAIT_OPEN_NETWORK_PHASE = 2;

	private const int SEND_INIT_NETWORK_PHASE = 3;

	private const float INIT_NETWORK_EMIT_INTERVAL = 3f;

	private const float RECEIVE_WAIT_INIT_NETWORK_TIMER = 15f;

	private const float MAX_INIT_NETWORK_EMIT_NUM = 5f;

	public static int InitNetworkErrorNum;

	private const int INIT_NETWORK_ERROR_NUM_TO_IPV_DIALOG_OPEN = 2;

	public MatchingNetworkConnectChecker(Matching matching)
		: base(matching)
	{
	}

	public void SetBattleId(string battleId)
	{
		_doMatchingBattleId = battleId;
	}

	public void SetConnectOnly()
	{
		_isConnectOnly = true;
	}

	public void SetNotInitBattle()
	{
		_isNotInitBattle = true;
	}

	public void SetBeforeBattleStartRetry()
	{
		_isBeforeBattleStartRetry = true;
	}

	public override void Update()
	{
		if (!_isActive)
		{
			return;
		}
		switch (_phase)
		{
		case 0:
			if (Global.IS_LOAD_ALLDONE || !(ToolboxGame.RealTimeNetworkAgent != null))
			{
				_matching.SafeStartBattleCoroutine(ToolboxGame.CreateRealTimeNetworkBattleAgent(_matching));
				_phase = 1;
			}
			break;
		case 1:
			if (!(ToolboxGame.RealTimeNetworkAgent == null))
			{
				if (GameMgr.GetIns().IsAINetwork)
				{
					RealTimeNetworkAgent.FinishTaskBase = _matching.GetBattleFinishTask();
				}
				_connectFailCount = 0;
				ToolboxGame.RealTimeNetworkAgent.Connect(PlayerStaticData.UserViewerID, _doMatchingBattleId, delegate
				{
					_connectFailCount++;
				}, _matching, _isBeforeBattleStartRetry);
				_phase = 2;
			}
			break;
		case 2:
			if (_connectFailCount >= 4)
			{
				LocalLog.AccumulateTraceLog("FailConnectNodejs");
				InitNetworkErrorNum++;
				OnConnectError.Call();
				_isActive = false;
			}
			else if (!(ToolboxGame.RealTimeNetworkAgent == null) && ToolboxGame.RealTimeNetworkAgent.IsOpen())
			{
				if (_isConnectOnly)
				{
					_isActive = false;
					break;
				}
				OnConnectError = null;
				_phase = 3;
				LocalLog.AccumulateLastTraceLog("MatchingNetworkConnectChecker_StartInitNetwork");
				ToolboxGame.RealTimeNetworkAgent.EmitMsgPack(NetworkBattleDefine.NetworkBattleURI.InitNetwork, new Dictionary<string, object>(), null, isGetableAck: false, -1, isStockData: false);
				_initNetworkTimer = TimeUtil.GetAbsoluteTime().Ticks;
			}
			break;
		case 3:
			if (ToolboxGame.RealTimeNetworkAgent.IsInitNetworkSuccess())
			{
				OnTimeoutInitNetwork = null;
				_isActive = false;
				if (!_isNotInitBattle)
				{
					_matching.MatchingInitBattle();
				}
				_matching.SettingRealTimeNetworkEvent();
				ToolboxGame.RealTimeNetworkAgent.StartGungnir();
			}
			else if ((float)NetworkUtility.GetTimeSpanSecond(_initNetworkTimer) >= 3f)
			{
				_initNetworkTimer = TimeUtil.GetAbsoluteTime().Ticks;
				_initNetworkSendNum++;
				if ((float)_initNetworkSendNum >= 5f)
				{
					_isActive = false;
					LocalLog.AccumulateTraceLog("FailInitNetwork");
					OnTimeoutInitNetwork.Call();
				}
				else
				{
					Dictionary<string, object> dictionary = new Dictionary<string, object>();
					dictionary["try"] = _initNetworkSendNum;
					LocalLog.AccumulateLastTraceLog("MatchingNetworkConnectChecker_ResendInitNetwork");
					ToolboxGame.RealTimeNetworkAgent.EmitMsgPack(NetworkBattleDefine.NetworkBattleURI.InitNetwork, dictionary, null, isGetableAck: false, -1, isStockData: false);
				}
			}
			break;
		}
	}

	public static bool IsOpenIpv6Dialog()
	{
		if (InitNetworkErrorNum >= 2 && PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.IS_SELECT_IPV6))
		{
			return true;
		}
		return false;
	}
}
