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

public class RoomMatchResultAnimationAgent : ResultAnimationAgent
{
	private const float RED_ETHER_WAIT_TIME_MAX = 5f;

	public override IEnumerator RunUI(BattleResultUIController battleResultControl, INextSceneSelector nextSceneSelector, bool isWin)
	{
		bool needLottery = false;
		if (!GameMgr.GetIns().IsWatchBattle)
		{
			needLottery = battleResultControl.ResultReporter.LotteryData.IsEnable;
		}
		if (needLottery)
		{
			yield return LoadLotteryImage(battleResultControl.ResultReporter.LotteryData);
		}
		m_BattleCamera.m_CutInCamera.gameObject.SetActive(value: false);
		if (battleResultControl.IsDraw)
		{
			battleResultControl.TitleWin.gameObject.SetActive(value: false);
			battleResultControl.TitleLose.gameObject.SetActive(value: false);
			battleResultControl.TitleDraw.gameObject.SetActive(value: true);
			battleResultControl.TitleDraw.transform.localScale = Vector3.one * 10f;
			battleResultControl.TitleDraw.alpha = 0f;
			battleResultControl.Bg.color = new Color32(0, 48, 16, 0);
			battleResultControl.ResultTitle.spriteName = "result_top_lose";
		}
		else if (isWin)
		{
			battleResultControl.TitleWin.transform.localScale = Vector3.one * 10f;
			battleResultControl.TitleWin.gameObject.SetActive(value: true);
			battleResultControl.TitleLose.gameObject.SetActive(value: false);
			battleResultControl.TitleDraw.gameObject.SetActive(value: false);
			battleResultControl.TitleWin.alpha = 0f;
			battleResultControl.Bg.color = new Color32(32, 24, 0, 0);
			battleResultControl.ResultTitle.spriteName = "result_top_win";
		}
		else
		{
			battleResultControl.TitleLose.transform.localScale = Vector3.one * 10f;
			battleResultControl.TitleWin.gameObject.SetActive(value: false);
			battleResultControl.TitleLose.gameObject.SetActive(value: true);
			battleResultControl.TitleDraw.gameObject.SetActive(value: false);
			battleResultControl.TitleLose.alpha = 0f;
			battleResultControl.Bg.color = new Color32(0, 24, 48, 0);
			battleResultControl.ResultTitle.spriteName = "result_top_lose";
		}
		battleResultControl.MainPanel.alpha = 1f;
		yield return new WaitForSeconds(0.1f);
		if (battleResultControl.IsDraw)
		{
			TweenAlpha.Begin(battleResultControl.TitleDraw.gameObject, 0.2f, 1f);
			iTween.ScaleTo(battleResultControl.TitleDraw.gameObject, iTween.Hash("scale", Vector3.one, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeInQuad));
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_YOULOSE);
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_JINGLE_LOSE);
		}
		else
		{
			battleResultControl.PrepareAchievementLog();
			if (isWin)
			{
				TweenAlpha.Begin(battleResultControl.TitleWin.gameObject, 0.2f, 1f);
				iTween.ScaleTo(battleResultControl.TitleWin.gameObject, iTween.Hash("scale", Vector3.one, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeInQuad));
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_YOUWIN);
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_JINGLE_WIN);
			}
			else
			{
				TweenAlpha.Begin(battleResultControl.TitleLose.gameObject, 0.2f, 1f);
				iTween.ScaleTo(battleResultControl.TitleLose.gameObject, iTween.Hash("scale", Vector3.one, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeInQuad));
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_YOULOSE);
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_JINGLE_LOSE);
			}
		}
		TweenAlpha.Begin(battleResultControl.Bg.gameObject, 0.5f, 0.75f);
		yield return new WaitForSeconds(0.2f);
		TweenAlpha.Begin(battleResultControl.ArcaneIn.gameObject, 0.5f, 1f);
		TweenAlpha.Begin(battleResultControl.ArcaneOut.gameObject, 0.5f, 1f);
		iTween.ScaleTo(battleResultControl.ArcaneIn.gameObject, iTween.Hash("scale", Vector3.one, "time", 2f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
		iTween.ScaleTo(battleResultControl.ArcaneOut.gameObject, iTween.Hash("scale", Vector3.one, "time", 2f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
		if (battleResultControl.IsDraw)
		{
			GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_RESULT_TITLE_3, Vector3.zero);
			battleResultControl.TitleDraw.transform.localScale = Vector3.one;
			iTween.ScaleTo(battleResultControl.TitleDraw.gameObject, iTween.Hash("scale", Vector3.one * 1.1f, "time", 2f, "islocal", true, "easetype", iTween.EaseType.linear));
		}
		else if (isWin)
		{
			GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_RESULT_TITLE_1, Vector3.zero);
			battleResultControl.TitleWin.transform.localScale = Vector3.one;
			iTween.ScaleTo(battleResultControl.TitleWin.gameObject, iTween.Hash("scale", Vector3.one * 1.1f, "time", 2f, "islocal", true, "easetype", iTween.EaseType.linear));
		}
		else
		{
			GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_RESULT_TITLE_2, Vector3.zero);
			battleResultControl.TitleLose.transform.localScale = Vector3.one;
			iTween.ScaleTo(battleResultControl.TitleLose.gameObject, iTween.Hash("scale", Vector3.one * 1.1f, "time", 2f, "islocal", true, "easetype", iTween.EaseType.linear));
		}
		HideEmotionMessage();
		if (battleResultControl.ResultMsgWindowFlag)
		{
			yield return StartCoroutine(battleResultControl.ShowSpecialResultInfo());
		}
		else
		{
			yield return new WaitForSeconds(2f);
		}
		if (needLottery)
		{
			yield return CreateLotteryDialog(battleResultControl.ResultReporter.LotteryData);
		}
		if (ShowRewardDialog(battleResultControl))
		{
			while (battleResultControl.IsRewardWait)
			{
				yield return null;
			}
		}
		if (Data.RoomMatchFinish.data != null && !GameMgr.GetIns().IsReplayBattle && !GameMgr.GetIns().IsWatchBattle)
		{
			TreasureBoxCpResultInfo treasureBoxCpResultInfo = Data.RoomMatchFinish.data.TreasureBoxCpResultInfo;
			if (treasureBoxCpResultInfo.IsPlayGradeUpAnimation())
			{
				yield return TreasureBoxCpOpenBoxAnimation(battleResultControl, treasureBoxCpResultInfo.AfterGrade);
			}
			if (treasureBoxCpResultInfo.IsBoxOpened())
			{
				yield return CreateTreasureBoxCpRewardDialog(treasureBoxCpResultInfo);
			}
		}
		bool isFinishBattlePass = false;
		battleResultControl.SetBattlePassGauge(delegate
		{
			battleResultControl.FinishResult();
			isFinishBattlePass = true;
		});
		while (!isFinishBattlePass)
		{
			yield return null;
		}
		if (Data.RedEtherCampaignResultData != null)
		{
			bool isFinishRedEther = false;
			RedEtherCampaignPanel.Create(battleResultControl.gameObject, Data.RedEtherCampaignResultData, battleResultControl, delegate
			{
				isFinishRedEther = true;
			});
			float time = 0f;
			while (!isFinishRedEther)
			{
				time += Time.deltaTime;
				if (time >= 5f)
				{
					break;
				}
				yield return null;
			}
			yield return ShowRewardDialog(Data.RedEtherCampaignResultData.RewardList);
		}
		battleResultControl.GreySpriteBGVisible = false;
		returnRoom();
	}

	private void returnRoom()
	{
		if (GameMgr.GetIns().IsWatchBattle)
		{
			if (GameMgr.GetIns().IsReplayBattle)
			{
				UIManager.GetInstance().StartCoroutine(GameMgr.GetIns().GetBattleCtrl().BattleEnd(delegate
				{
					UIManager.ViewScene replayEndBackScene = Data.ReplayBattleInfo.ReplayEndBackScene;
					UIManager.ChangeViewSceneParam replayEndBackSceneParam = Data.ReplayBattleInfo.ReplayEndBackSceneParam;
					UIManager.GetInstance().ChangeViewScene(replayEndBackScene, replayEndBackSceneParam);
				}));
			}
			else
			{
				PlayerControllerForWatching playerControllerForWatching = RoomBase.ConnectController.OwnCtrl as PlayerControllerForWatching;
				PlayerControllerForWatching playerControllerForWatching2 = RoomBase.ConnectController.OppoCtrl as PlayerControllerForWatching;
				playerControllerForWatching.ResetWatchHandler();
				playerControllerForWatching2.ResetWatchHandler();
				StartCoroutine(StartCheckState(playerControllerForWatching, playerControllerForWatching2));
			}
			return;
		}
		if (RoomBase.IsConnectControllerActive())
		{
			RoomBase.ConnectController.TotalBattleNum++;
			GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.Room);
			return;
		}
		PrepareRoom((!Data.BattleRecoveryInfo.is_owner) ? RoomConnectController.PositionMode.VISITOR : RoomConnectController.PositionMode.OWNER, Data.BattleRecoveryInfo.BattleParameterInstance, Data.BattleRecoveryInfo.roomId, PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.ROOM_MATCH_FRIEND_WATCH_PERMIT), PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.ROOM_MATCH_GUILD_WATCH_PERMIT), Data.BattleRecoveryInfo.ConventionInfo, Data.BattleRecoveryInfo.IsGatheringRoom, Data.BattleRecoveryInfo.CanUseNonPossessionCard, delegate
		{
			RoomBase.OnAlreadySetup();
			RoomBase.ConnectController.TotalBattleNum++;
			RoomBase.ConnectController.OppoCtrl.Target.ViewerId = Data.BattleRecoveryInfo.opponent_viewer_id;
			GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.Room, delegate
			{
				RoomBase.GetInstance().IsRecoveryRoom = true;
			});
		});
	}

	public void PrepareRoom(RoomConnectController.PositionMode mode, BattleParameter battleParameter, string roomId, bool isPermitFriendWatch, bool isPermitGuildWatch, ConventionInfo conventionInfo, bool isGathering, bool canUseNonPossessionCard, Action callback)
	{
		UIManager.GetInstance().createInSceneCenterLoading(notBlack: true);
		RoomConnectController.InitializeParameter initializeParameter = new RoomConnectController.InitializeParameter(mode, battleParameter, roomId);
		initializeParameter.IsPermitFriendWatch = isPermitFriendWatch;
		initializeParameter.IsPermitGuildWatch = isPermitGuildWatch;
		initializeParameter.ConventionInfo = conventionInfo;
		initializeParameter.IsGathering = isGathering;
		if (conventionInfo != null)
		{
			initializeParameter.IsEnableTurnSelect = conventionInfo.IsSelectableTurn;
		}
		RoomConnectController roomConnectController = new RoomConnectController(initializeParameter);
		roomConnectController.InitPlayerController();
		roomConnectController.IsEnableGuildInviteButton = Data.BattleRecoveryInfo.IsEnableGuildInviteButton;
		if (conventionInfo != null)
		{
			roomConnectController.DisplayRoomID = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.ROOM_MATCH_DISPLAY_ID);
		}
		if (isGathering)
		{
			roomConnectController.DisplayRoomID = ((roomId.Length > 5) ? roomId.Substring(roomId.Length - 5, 5) : roomId);
		}
		roomConnectController.CanUseNonPossessionCard = canUseNonPossessionCard;
		UIManager.GetInstance().closeInSceneCenterLoading();
		callback();
	}

	private IEnumerator StartCheckState(PlayerControllerForWatching pcontroller, PlayerControllerForWatching ocontroller)
	{
		while (RoomBase.ConnectController.TotalWatchBattleNum == RoomBase.ConnectController.TotalBattleNum && !RoomBase.ConnectController.IsRelease)
		{
			yield return null;
		}
		RoomBase.ConnectController.TotalWatchBattleNum = RoomBase.ConnectController.TotalBattleNum;
		pcontroller.ResetReceiveEvent();
		ocontroller.ResetReceiveEvent();
		RoomBase.ConnectController.RemoveWatchEventDispath(isOwn: true);
		RoomBase.ConnectController.RemoveWatchEventDispath(isOwn: false);
		GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.Room);
	}
}
