using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.Lottery;

public class ResultAnimationAgent : MonoBehaviour
{
	private class DialogReward
	{
		public DialogBase _dialog;

		public RewardBase _reward;

		public DialogReward(DialogBase dia, RewardBase reward)
		{
			_dialog = dia;
			_reward = reward;
		}
	}

	protected BattleCamera m_BattleCamera;

	private const float DIALOG_AUTO_CLOSE_TIME = 5f;

	private string _loadPath = string.Empty;

	private GameObject _boxOpenEffectObj;

	private GameObject _treasureEffectObj;

	private GameObject _treasureCpBoxTextPanel;

	public void SetBattleCamera(BattleCamera battleCamera)
	{
		m_BattleCamera = battleCamera;
	}

	public virtual IEnumerator RunUI(BattleResultUIController battleResultControl, INextSceneSelector nextSceneSelector, bool isWin)
	{
		yield break;
	}

	protected bool ShowRewardDialog(BattleResultUIController battleResultControl, bool addStoryReward = false, bool hasSkippedStorySpecialBattleResult = false, bool isNoBattleStoryResult = false)
	{
		if (GameMgr.GetIns().IsWatchBattle)
		{
			return false;
		}
		DialogReward dialogReward = HandleVictoryRewards(battleResultControl);
		if (dialogReward != null)
		{
			dialogReward._dialog.OnClose = delegate
			{
				DialogReward dialogReward2 = HandleStoryAndMissionRewards(battleResultControl, addStoryReward, hasSkippedStorySpecialBattleResult, isNoBattleStoryResult);
				if (dialogReward2 != null)
				{
					dialogReward2._dialog.OnClose = battleResultControl.RewardCheck;
				}
				else
				{
					battleResultControl.RewardCheck();
				}
			};
		}
		else
		{
			dialogReward = HandleStoryAndMissionRewards(battleResultControl, addStoryReward, hasSkippedStorySpecialBattleResult, isNoBattleStoryResult);
			if (dialogReward != null)
			{
				dialogReward._dialog.OnClose = battleResultControl.RewardCheck;
			}
		}
		if (dialogReward != null)
		{
			battleResultControl.IsRewardWait = true;
		}
		return dialogReward != null;
	}

	protected void HideEmotionMessage()
	{
		BattleManagerBase.GetIns().BattlePlayer.PlayerBattleView.HideAlertDialogue(PanelMgr.BattleAlertType.SelectedEmotionIcon);
		Transform[] componentsInChildren = BattleManagerBase.GetIns().BattleUIContainer.gameObject.GetComponentsInChildren<Transform>(includeInactive: false);
		foreach (Transform transform in componentsInChildren)
		{
			if (transform.name.Contains("EmotionMessage"))
			{
				transform.gameObject.SetActive(value: false);
			}
		}
	}

	private DialogReward HandleVictoryRewards(BattleResultUIController battleResultControl)
	{
		DialogReward dialogReward = null;
		List<ReceivedReward> list = ((battleResultControl.ResultReporter != null) ? battleResultControl.ResultReporter.VictoryRewards : null);
		if (list != null && list.Count > 0)
		{
			SystemText systemText = Data.SystemText;
			dialogReward = CreateDialogContent(systemText.Get("Battle_0477"));
			for (int i = 0; i < list.Count; i++)
			{
				dialogReward._reward.AddReward(list[i]);
				if (!string.IsNullOrEmpty(list[i].reward_message))
				{
					dialogReward._dialog.SetTitleLabel(list[i].reward_message);
				}
			}
			dialogReward._reward.EndCreate();
		}
		return dialogReward;
	}

	private DialogReward HandleStoryAndMissionRewards(BattleResultUIController battleResultControl, bool addStoryReward = false, bool hasSkippedStorySpecialBattleResult = false, bool isNoBattleStoryResult = false)
	{
		DialogReward dialogReward = null;
		if (addStoryReward)
		{
			IReadOnlyList<ReceivedReward> storyClearRewards = Data.StoryFinish.data.StoryClearRewards;
			if (storyClearRewards.Count > 0)
			{
				dialogReward = CreateDialogContent(Data.SystemText.Get("Story_0029"));
				foreach (ReceivedReward item in storyClearRewards)
				{
					dialogReward._reward.AddReward(item);
				}
			}
		}
		if ((hasSkippedStorySpecialBattleResult || isNoBattleStoryResult) && Data.StoryFinish.data.Rewards.Count > 0)
		{
			if (dialogReward == null)
			{
				SystemText systemText = Data.SystemText;
				dialogReward = CreateDialogContent(systemText.Get("Story_0029"));
			}
			foreach (ReceivedReward reward in Data.StoryFinish.data.Rewards)
			{
				dialogReward._reward.AddReward(reward);
			}
		}
		List<ReceivedReward> list = ((battleResultControl.ResultReporter != null) ? battleResultControl.ResultReporter.MissionRewards : null);
		if (list != null && list.Count > 0)
		{
			if (dialogReward == null)
			{
				SystemText systemText2 = Data.SystemText;
				dialogReward = CreateDialogContent(systemText2.Get("Story_0029"));
				if (GameMgr.GetIns().GetDataMgr().IsRoomBattleType())
				{
					dialogReward._dialog.AutoClose(5f);
				}
			}
			for (int i = 0; i < list.Count; i++)
			{
				dialogReward._reward.AddReward(list[i]);
			}
		}
		dialogReward?._reward.EndCreate();
		return dialogReward;
	}

	private DialogReward CreateRewardDialog(List<ReceivedReward> rewardList)
	{
		SystemText systemText = Data.SystemText;
		DialogReward dialogReward = CreateDialogContent(systemText.Get("Story_0029"));
		if (GameMgr.GetIns().GetDataMgr().IsRoomBattleType())
		{
			dialogReward._dialog.AutoClose(5f);
		}
		foreach (ReceivedReward reward in rewardList)
		{
			dialogReward._reward.AddReward(reward);
		}
		dialogReward._reward.EndCreate();
		return dialogReward;
	}

	public IEnumerator ShowRewardDialog(List<ReceivedReward> rewardList)
	{
		if (rewardList != null && rewardList.Count != 0)
		{
			bool isFinishDialog = false;
			CreateRewardDialog(rewardList)._dialog.OnClose = delegate
			{
				isFinishDialog = true;
			};
			while (!isFinishDialog)
			{
				yield return null;
			}
		}
	}

	private DialogReward CreateDialogContent(string title)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetLayer("MyPage");
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(title);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetPanelDepth(105);
		RewardBase component = NGUITools.AddChild(dialogBase.gameObject, UIManager.GetInstance().GetRewardDialogPrefab().gameObject).GetComponent<RewardBase>();
		component.SetActiveMyPageLayerCamera(isActive: true);
		return new DialogReward(dialogBase, component);
	}

	protected IEnumerator LoadLotteryImage(LotteryApplyData data)
	{
		string assetName = (_loadPath = LotteryApplyDialog.GetLotteryTexturePath(data, isFetch: false));
		yield return Toolbox.ResourcesManager.LoadAssetAsync(assetName, null);
	}

	private void DisposeLotteryImage()
	{
		if (!string.IsNullOrEmpty(_loadPath))
		{
			Toolbox.ResourcesManager.RemoveAsset(_loadPath);
			_loadPath = string.Empty;
		}
	}

	protected IEnumerator CreateLotteryDialog(LotteryApplyData data)
	{
		DialogBase dialogBase = LotteryApplyDialog.Create(data);
		bool finishLotteryDialog = false;
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			finishLotteryDialog = true;
		});
		while (!finishLotteryDialog)
		{
			yield return null;
		}
		DisposeLotteryImage();
	}

	public void PlayWinVoice()
	{
		List<string> battleListAssetPathList = Toolbox.ResourcesManager.BattleListAssetPathList;
		string voiceId = string.Empty;
		bool isSkinEvolved = BattleManagerBase.GetIns().BattlePlayer.IsSkinEvolved;
		if (BattleManagerBase.GetIns().IsPuzzleMgr)
		{
			voiceId = (BattleManagerBase.GetIns() as PuzzleBattleManager).PuzzleQuestData.ClearVoiceId;
		}
		GameMgr.GetIns().GetSoundMgr().PlayVoice(ClassCharaPrm.EmotionType.WIN, GameMgr.GetIns().GetDataMgr().GetPlayerSkinId(), battleListAssetPathList, voiceId, isSkinEvolved);
	}

	protected IEnumerator TreasureBoxCpOpenBoxAnimation(BattleResultUIController battleResultUIController, int grade)
	{
		_boxOpenEffectObj = NGUITools.AddChild(battleResultUIController.gameObject, Resources.Load("UI/layoutParts/RankWinnerRewardResultPanel") as GameObject);
		_treasureCpBoxTextPanel = NGUITools.AddChild(battleResultUIController.gameObject, Resources.Load("UI/layoutParts/TreasureCpBoxTextPanel") as GameObject);
		_treasureCpBoxTextPanel.SetLayer(LayerMask.NameToLayer("SystemUI"), isSetChildren: true);
		NguiObjs component = _boxOpenEffectObj.GetComponent<NguiObjs>();
		UISprite bg = component.sprites[0];
		UISprite obj = component.sprites[1];
		UISprite uISprite = component.sprites[2];
		UISprite treasureCpBoxText = _treasureCpBoxTextPanel.GetComponent<TreasureCpBoxTextPanel>()._treasureCpBoxText;
		treasureCpBoxText.atlas = UIManager.GetInstance().GetAtlasList().FirstOrDefault((UIAtlas s) => s.name == "BattleLang");
		treasureCpBoxText.spriteName = TreasureBoxCp.GetTreasureSpriteName(grade);
		UIManager.GetInstance().AttachAtlas(uISprite.gameObject);
		UILabel label = component.labels[0];
		UIPanel component2 = _boxOpenEffectObj.GetComponent<UIPanel>();
		label.text = string.Empty;
		bg.alpha = 0f;
		uISprite.alpha = 0f;
		label.alpha = 0f;
		component2.alpha = 1f;
		treasureCpBoxText.alpha = 0f;
		treasureCpBoxText.gameObject.SetActive(value: true);
		obj.gameObject.SetActive(value: false);
		string loadEffectName = $"cmn_treasure_gradeup_gp_{grade}";
		List<string> list = new List<string>();
		list.Add(Toolbox.ResourcesManager.GetAssetTypePath(loadEffectName, ResourcesManager.AssetLoadPathType.Effect2D));
		yield return UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(list, null));
		TweenAlpha.Begin(bg.gameObject, 0.5f, 0.65f);
		TweenAlpha.Begin(label.gameObject, 0.5f, 1f);
		_treasureEffectObj = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(loadEffectName, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true)) as GameObject);
		_treasureEffectObj.transform.parent = _boxOpenEffectObj.transform;
		_treasureEffectObj.SetActive(value: false);
		bool particleShaderLoaded = false;
		GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(_treasureEffectObj, delegate
		{
			particleShaderLoaded = true;
		});
		_treasureEffectObj.SetLayer(LayerMask.NameToLayer("FrontUI"), isSetChildren: true);
		_treasureEffectObj.transform.localPosition = Vector3.zero;
		_treasureEffectObj.transform.localScale = Vector3.one * 320f * 1.75f;
		yield return new WaitForSeconds(0.8f);
		while (!particleShaderLoaded)
		{
			yield return null;
		}
		_treasureEffectObj.SetActive(value: true);
		GameMgr.GetIns().GetSoundMgr().PlaySe(TreasureBoxCp.GetGradeSE(grade));
		TweenAlpha.Begin(treasureCpBoxText.gameObject, 0.5f, 1f);
		treasureCpBoxText.transform.localPosition = new Vector3(0f, -192f, 0f);
		iTween.MoveTo(treasureCpBoxText.gameObject, iTween.Hash("y", -172, "time", 0.5f, "islocal", true, "easetype", iTween.EaseType.easeOutQuad));
		yield return new WaitForSeconds(TreasureBoxCp.OPEN_TREASURE_BOX_TIME[grade - 1] + 0.5f);
		TweenAlpha.Begin(treasureCpBoxText.gameObject, 0.5f, 0f);
		UIManager.GetInstance().StartCoroutine(RunHideBoxEffect(0.5f));
	}

	private IEnumerator RunHideBoxEffect(float delayTime)
	{
		yield return new WaitForSeconds(delayTime);
		_treasureEffectObj.SetActive(value: false);
		_boxOpenEffectObj.SetActive(value: false);
		_treasureCpBoxTextPanel.SetActive(value: false);
		UnityEngine.Object.Destroy(_boxOpenEffectObj);
		UnityEngine.Object.Destroy(_treasureCpBoxTextPanel);
		UnityEngine.Object.Destroy(_treasureEffectObj);
		UIManager.GetInstance().offNotTouch();
	}

	protected IEnumerator CreateTreasureBoxCpRewardDialog(TreasureBoxCpResultInfo info)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetLayer("MyPage");
		dialogBase.SetPanelDepth(105, isSetBackViewDepthImmediately: true);
		dialogBase.SetTitleLabel(Data.SystemText.Get("TreasureBoxCp_0026"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		bool dialogClosed = false;
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			dialogClosed = true;
		});
		RewardBase component = NGUITools.AddChild(dialogBase.gameObject, Resources.Load<GameObject>("UI/layoutParts/StoryRewardPanel")).GetComponent<RewardBase>();
		for (int num = 0; num < info.RewardDataList.Count; num++)
		{
			component.AddReward(info.RewardDataList[num]);
		}
		component.SetActiveMyPageLayerCamera(isActive: true);
		component.EndCreate();
		while (!dialogClosed)
		{
			yield return null;
		}
	}
}
