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

public class Matching_Room : Matching
{
	public enum GAME_STATE
	{
		WAIT,
		LOADING,
		BATTLE,
		RESULT
	}

	private BattleParameter _battleParameter;

	private bool _isConvention;

	private bool _isGathering;

	private bool _isInitRoomBattle;

	private GAME_STATE _gameState;

	public const int RESULT_CODE_ROOM_NOT_FOUND = 1751;

	public const int RESULT_CODE_GATHERING_NOT_JOIN = 5304;

	public const int RESULT_CODE_GATHERING_EXPIRE = 5311;

	public bool IsStopChangeScene { get; set; }

	public override void FirstSetting(int classId, int selDeck, bool isRecovery = false)
	{
		_isInitRoomBattle = false;
		errorDialogReturnText = Wizard.Data.SystemText.Get("Battle_0202");
		base.FirstSetting(classId, selDeck, isRecovery);
	}

	public void SetBattleParameter(BattleParameter battleParameter)
	{
		_battleParameter = battleParameter;
	}

	public Matching_Room(bool isConvention, bool isGathering)
	{
		_isConvention = isConvention;
		_isGathering = isGathering;
		errorDialogReturnText = Wizard.Data.SystemText.Get("Battle_0434");
		isDisplayCancelButton = false;
		IsStopChangeScene = false;
	}

	public override void DoMatching(Action onFinished, int init, DO_MATCHING_LOG log)
	{
		base.DoMatching(onFinished, 0, log);
		RoomBattleDoMatchingTask roomBattleDoMatchingTask = ((_battleParameter.DeckFormat == Format.Hof) ? new RoomBattleDoMatchingTaskHOF() : ((_battleParameter.DeckFormat == Format.Windfall) ? new RoomBattleDoMatchingTaskWindFall() : ((_battleParameter.DeckFormat == Format.Avatar) ? new RoomBattleDoMatchingTaskAvatar(_isGathering) : ((!RoomConnectController.IsNormalMatchingAPI(_battleParameter)) ? new RoomBattle2PickDoMatchingTask(_battleParameter.TwoPickFormat, _battleParameter.Rule) : new RoomBattleDoMatchingTask(_isConvention, _isGathering)))));
		roomBattleDoMatchingTask.SetParameter(selectDeckID, 0, (int)log, includeCardMasterHash: true);
		ConnectAPI(roomBattleDoMatchingTask, delegate
		{
			if (Wizard.Data.RoomBattleMatching.data.matching_state != 3009)
			{
				_gameState = (GAME_STATE)Wizard.Data.RoomBattleMatching.data.battle_state;
				_doMatchingResultKind = (DoMatchingResult)Wizard.Data.RoomBattleMatching.data.matching_state;
				onFinished.Call();
				RoomBase.IsMatchingFinish = true;
			}
		});
	}

	protected override void OnFinishedDoMatching()
	{
		if (ToolboxGame.RealTimeNetworkAgent == null)
		{
			return;
		}
		switch (_doMatchingResultKind)
		{
		case DoMatchingResult.RC_BATTLE_MATCHING_NOT_JOINED_GATHERING:
			OpenErrorDialog((int)_doMatchingResultKind, UIManager.ViewScene.MyPage);
			return;
		case DoMatchingResult.RC_BATTLE_MATCHING_GATHERING_BATTLE_END:
			OpenErrorDialog((int)_doMatchingResultKind, UIManager.ViewScene.Gathering);
			return;
		case DoMatchingResult.RC_BATTLE_MATCHING_NOT_ROOM_ID:
			OpenErrorDialog((int)_doMatchingResultKind, UIManager.ViewScene.Gathering);
			return;
		}
		switch (_gameState)
		{
		case GAME_STATE.LOADING:
			ErrorDialogWithReturn(Wizard.Data.SystemText.Get("Battle_0401"), Wizard.Data.SystemText.Get("Battle_0411"), Wizard.Data.SystemText.Get("Common_0132"));
			break;
		case GAME_STATE.BATTLE:
			ErrorDialogWithReturn(Wizard.Data.SystemText.Get("Battle_0402"), Wizard.Data.SystemText.Get("Battle_0411"), Wizard.Data.SystemText.Get("Common_0132"));
			break;
		case GAME_STATE.RESULT:
			ErrorDialogWithReturn(Wizard.Data.SystemText.Get("Battle_0403"), Wizard.Data.SystemText.Get("Battle_0411"), Wizard.Data.SystemText.Get("Common_0132"));
			break;
		default:
			MatchingInitBattle();
			break;
		}
	}

	protected override void SettingOwnerToMatchingState()
	{
		_isInitRoomBattle = true;
		errorDialogReturnText = Wizard.Data.SystemText.Get("Battle_0434");
		base.SettingOwnerToMatchingState();
		switch (_doMatchingResultKind)
		{
		case DoMatchingResult.RC_BATTLE_MATCHING_SUCCEEDED_OWNER:
			isOwner = true;
			break;
		case DoMatchingResult.RC_BATTLE_MATCHING_SUCCEEDED:
			isOwner = false;
			break;
		default:
			ErrorDialogWithReturn(Wizard.Data.SystemText.Get("Battle_0404"), Wizard.Data.SystemText.Get("Error_0002"), Wizard.Data.SystemText.Get("Common_0132"));
			break;
		case DoMatchingResult.RC_BATTLE_MATCHING_IN_BATTLE_PHASE:
			break;
		}
	}

	protected override NetworkBattleDefine.NetworkBattleURI GetInitBattleUri()
	{
		return NetworkBattleDefine.NetworkBattleURI.InitRoomBattle;
	}

	protected override void GotoHomeScene()
	{
		RoomBase.IsMatchingFinish = false;
		bool isInitRoomBattle = _isInitRoomBattle;
		if (IsStopChangeScene)
		{
			return;
		}
		UIManager.ChangeViewSceneParam changeViewSceneParam = new UIManager.ChangeViewSceneParam();
		changeViewSceneParam.OnChange = delegate
		{
			UIManager.GetInstance().CloseInSceneLoadingBattle();
		};
		if (isInitRoomBattle)
		{
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Room, changeViewSceneParam);
			return;
		}
		changeViewSceneParam.IsCutCardMotion = true;
		changeViewSceneParam.OnFinishChangeView = delegate
		{
			UIManager.GetInstance().CloseInSceneLoadingMatching();
			UIManager.GetInstance().CloseInSceneLoadingBattle();
		};
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.MyPage, changeViewSceneParam);
	}

	protected override void GotoDeckSelectScene()
	{
		GotoHomeScene();
	}

	public override FinishTaskBase GetBattleFinishTask()
	{
		if (_battleParameter.DeckFormat == Format.Hof)
		{
			return new RoomBattleFinishTaskHOF();
		}
		if (_battleParameter.DeckFormat == Format.Windfall)
		{
			return new RoomBattleFinishTaskWindFall();
		}
		if (_battleParameter.DeckFormat == Format.Avatar)
		{
			return new RoomBattleFinishTaskAvatar(_isGathering);
		}
		if (RoomConnectController.IsNormalMatchingAPI(_battleParameter))
		{
			return new RoomBattleFinishTask(_isConvention, _isGathering);
		}
		return new RoomBattle2PickFinishTask(_battleParameter.TwoPickFormat, _battleParameter.Rule);
	}

	protected override void ErrorDialog(string text, string title, bool isFailedInitNetwork)
	{
		DialogBase dialogBase = CreateDialogBase();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(title);
		dialogBase.SetText(text);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetButtonText(errorDialogReturnText);
		dialogBase.SetFadeButtonEnabled(flag: false);
		dialogBase.SetPanelDepth(5400);
		dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_CANCEL_TRANS;
		EventDelegate method_btn = new EventDelegate(base.ReturnScene);
		dialogBase.SetButtonDelegate(method_btn);
	}

	public override void TimeOutAction(string log, bool isInitNetworkFailed)
	{
		if (!_goTitleOnTimeout && _isGathering)
		{
			ErrorDialogGoToTitle(Wizard.Data.SystemText.Get("Battle_0508"), Wizard.Data.SystemText.Get("Battle_0412"));
			return;
		}
		if (_goTitleOnTimeout && !_receivedMatchingTimeout)
		{
			ErrorDialogGoToTitle(Wizard.Data.SystemText.Get("Battle_0508"), Wizard.Data.SystemText.Get("Battle_0412"));
			return;
		}
		LocalLog.AccumulateLastTraceLog("TimeOutAction " + log);
		ErrorDialogGoToTitle(Wizard.Data.SystemText.Get("Error_0006"), Wizard.Data.SystemText.Get("ErrorHeader_0006"));
	}

	public override void GotoBattle()
	{
		base.GotoBattle();
		RoomConnectController.IsAlreadyStartBattle = true;
	}

	private void OnPushErrorDialogButton(UIManager.ViewScene scene)
	{
		bool matchingDataReady = GetMatchingDataReady();
		if (!matchingDataReady && isOffViewAllEnable)
		{
			UIManager.GetInstance().OffViewAll();
		}
		ToolboxGame.DestroyNetworkAgent();
		if (matchingDataReady && GameMgr.GetIns().GetBattleCtrl() != null)
		{
			UIManager.GetInstance().StartCoroutine(BattleEndCoroutin(delegate
			{
				UIManager.GetInstance().ChangeViewScene(scene);
			}));
		}
		else
		{
			UIManager.GetInstance().ChangeViewScene(scene);
		}
	}

	private void OpenErrorDialog(int errorCode, UIManager.ViewScene scene)
	{
		DialogBase dialogBase = CreateDialogBase();
		Dialog.Setup(dialogBase, errorCode.ToString());
		dialogBase.SetPanelDepth(5400);
		dialogBase.onPushButton1 = delegate
		{
			OnPushErrorDialogButton(scene);
		};
		_matchingTimeCheker.Stop();
	}
}
