using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.Battle.UI;
using Wizard.Battle.View.Vfx;

public class BattleControl : MonoBehaviour
{
	private BattleManagerBase m_BtlMgrIns;

	private int FirstAttack;

	public void Init()
	{
		m_BtlMgrIns = BattleManagerBase.GetIns();
		GameMgr.GetIns().GetInputMgr().SetLayerMask(512);
		LocalLog.AccumulateLastTraceLog("StartBattleCoroutine ");
		StartCoroutine(WaitLoadOpponentObjectToBattleStart(m_BtlMgrIns.LoadOpponentObjects()));
	}

	private IEnumerator WaitLoadOpponentObjectToBattleStart(VfxBase vfx)
	{
		if (GameMgr.GetIns().IsNetworkBattle)
		{
			FirstAttack = ToolboxGame.RealTimeNetworkAgent.GetIsFirstPlayer();
		}
		while (!vfx.IsEnd)
		{
			yield return null;
		}
		LocalLog.AccumulateLastTraceLog("DecideFirstUser End ");
		m_BtlMgrIns.StartOpening(FirstAttack);
	}

	public void BattleEnd(UIManager.ViewScene MoveTo, Action callback = null, Action<UIManager.ChangeViewSceneParam> paramCustomize = null, object sceneParam = null)
	{
		ToolboxGame.UIManager.gameObject.SetActive(value: true);
		UIManager.ChangeViewSceneParam changeViewSceneParam = new UIManager.ChangeViewSceneParam();
		changeViewSceneParam.OnBeforeChange = delegate
		{
			BattleManagerBase.GetIns().DisposeBattleGameObj();
		};
		changeViewSceneParam.OnChange = delegate
		{
			GameMgr.GetIns().GetEffectMgr().DestroyBattleEffectContainer();
			GameMgr.GetIns().GetDataMgr().ResetEnemyData();
			GameMgr.GetIns().DestroyBattleManagements();
			GameMgr.GetIns().GetGameObjMgr().GetUIContainer()
				.SetActive(value: false);
			if (callback != null)
			{
				callback();
			}
		};
		paramCustomize.Call(changeViewSceneParam);
		StartCoroutine(UnloadAllResources(MoveTo, changeViewSceneParam, null, sceneParam));
	}

	private IEnumerator UnloadAllResources(UIManager.ViewScene MoveTo = UIManager.ViewScene.None, UIManager.ChangeViewSceneParam param = null, Action callback = null, object sceneParam = null)
	{
		BattleLogManager.GetInstance().Clear();
		GameMgr.GetIns().GetEffectMgr().ClearLastCacheEffect();
		StopAllTweens();
		yield return Resources.UnloadUnusedAssets();
		GC.Collect();
		callback?.Invoke();
		if (MoveTo != UIManager.ViewScene.None)
		{
			UIManager.GetInstance().ChangeViewScene(MoveTo, param, sceneParam);
		}
	}

	public IEnumerator BattleEnd(Action callback = null)
	{
		BattleRelease();
		yield return Resources.UnloadUnusedAssets();
		GC.Collect();
		callback?.Invoke();
	}

	public void BattleRelease()
	{
		ToolboxGame.UIManager.gameObject.SetActive(value: true);
		GameMgr.GetIns().GetEffectMgr().DestroyBattleEffectContainer();
		GameMgr.GetIns().GetDataMgr().ResetEnemyData();
		if (BattleManagerBase.GetIns() != null)
		{
			BattleManagerBase.GetIns().DisposeBattleGameObj();
		}
		GameMgr.GetIns().DestroyBattleManagements();
		GameMgr.GetIns().GetGameObjMgr().GetUIContainer()
			.SetActive(value: false);
		BattleLogManager.GetInstance().Clear();
		GameMgr.GetIns().GetEffectMgr().ClearLastCacheEffect();
		StopAllTweens();
	}

	private void StopAllTweens()
	{
		HashSet<GameObject> hashSet = new HashSet<GameObject>();
		for (int i = 0; i < iTween.tweens.Count; i++)
		{
			if (iTween.tweens[i] != null)
			{
				GameObject gameObject = (GameObject)iTween.tweens[i]["target"];
				if (gameObject != null)
				{
					hashSet.Add(gameObject);
				}
			}
		}
		foreach (GameObject item in hashSet)
		{
			if (item != null)
			{
				iTween.Stop(item);
			}
		}
		iTween.tweens.Clear();
	}
}
