using System;
using System.Collections.Generic;
using System.Linq;
using Wizard.Battle.Recovery;
using Wizard.Battle.UI;
using Wizard.Battle.View;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Phase;

public class RecoveryAfterMulliganPhase : IPhase
{
	private readonly IRecoveryManager _recoveryManager;

	private readonly BattleManagerBase _battleMgr;

	private readonly Func<IPhase> _createMainPhase;

	private readonly BattlePlayer _battlePlayer;

	private readonly BattleEnemy _battleEnemy;

	private readonly TouchControl _touchControl;

	private bool _isEndRecovery;

	public RecoveryAfterMulliganPhase(IRecoveryManager recoveryManager, BattleManagerBase battleMgr, Func<IPhase> createMainPhase)
	{
		_recoveryManager = recoveryManager;
		_battleMgr = battleMgr;
		_createMainPhase = createMainPhase;
		_battlePlayer = battleMgr.BattlePlayer;
		_battleEnemy = battleMgr.BattleEnemy;
		_touchControl = battleMgr.TouchControl;
	}

	public VfxBase Setup()
	{
		_recoveryManager.OnEndRecovery += delegate
		{
			_isEndRecovery = true;
		};
		return _recoveryManager.Recovery(_battleMgr.BattlePlayer, _battleMgr.BattleEnemy, BattleCoroutine.GetInstance().StartCoroutine);
	}

	public VfxWith<IPhase> Update(float dt)
	{
		_battleMgr.VfxMgr.Update(dt);
		VfxBase vfx = _recoveryManager.UpdateRecovery();
		if (_isEndRecovery)
		{
			return new VfxWith<IPhase>(vfx, _createMainPhase());
		}
		return new VfxWith<IPhase>(vfx, null);
	}

	public VfxBase Teardown()
	{
		if (_recoveryManager is SingleBattleRecoveryManager)
		{
			((SingleBattleRecoveryManager)_recoveryManager).CreateRecoveryFileFromTempFile();
			((SingleBattleRecoveryRecordManager)((SingleBattleMgr)BattleManagerBase.GetIns()).ContentsCreator.RecoveryRecordManager).ClearRecoderTempFilePath();
		}
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		_battleMgr.BattlePlayer.PlayerBattleView.PlayQueueView.ForceClearPlayQueue();
		_battleMgr.BattleEnemy.BattleEnemyView.PlayQueueView.ForceClearPlayQueue();
		if (_battleMgr.IsBattleEnd)
		{
			BattleLogManager.GetInstance().SetActiveShowButton(isActive: false);
		}
		if (_battleMgr.BattlePlayer.Class.IsDead && _battleMgr.BattleEnemy.Class.IsDead)
		{
			VfxBase vfx = (_battleMgr.BattlePlayer.IsSelfTurn ? _battleMgr.DeadClass(PlayerDead: true, BattleManagerBase.FINISH_TYPE.NORMAL) : _battleMgr.DeadClass(PlayerDead: false, BattleManagerBase.FINISH_TYPE.NORMAL));
			sequentialVfxPlayer.Register(vfx);
			sequentialVfxPlayer.Register(InstantVfx.Create(delegate
			{
				_battleMgr.InitiateGameEndSequence(!_battleMgr.BattlePlayer.IsSelfTurn);
			}));
		}
		else if (_battleMgr.BattlePlayer.Class.IsDead)
		{
			sequentialVfxPlayer.Register(_battleMgr.DeadClass(PlayerDead: true, BattleManagerBase.FINISH_TYPE.NORMAL));
			sequentialVfxPlayer.Register(InstantVfx.Create(delegate
			{
				_battleMgr.InitiateGameEndSequence(hasWon: false);
			}));
		}
		else if (_battleMgr.BattleEnemy.Class.IsDead)
		{
			sequentialVfxPlayer.Register(_battleMgr.DeadClass(PlayerDead: false, BattleManagerBase.FINISH_TYPE.NORMAL));
			sequentialVfxPlayer.Register(InstantVfx.Create(delegate
			{
				_battleMgr.InitiateGameEndSequence(hasWon: true);
			}));
		}
		if (_battlePlayer.PlayerBattleView.IsSelecting)
		{
			_touchControl.SelectCancelActCard();
		}
		_touchControl.Exit();
		if (_battlePlayer.IsSelfTurn)
		{
			for (int num = 0; num < _battlePlayer.HandCardList.Count; num++)
			{
				_battlePlayer.HandCardList[num].BattleCardView.UpdateMovability();
			}
		}
		VfxBase vfxBase = (_battleEnemy.IsSelfTurn ? _battleEnemy.CreateThinkingVfx(_battleMgr) : NullVfx.GetInstance());
		VfxBase vfxBase2 = RestoreUI(_battleMgr);
		return SequentialVfxPlayer.Create(InstantVfx.Create(delegate
		{
			_battleMgr.BackGround.PlayBgm();
		}), vfxBase2, InstantVfx.Create(delegate
		{
			_battleMgr.BattlePlayer.ClassInformationUIController.Recovery();
			_battleMgr.BattleEnemy.ClassInformationUIController.Recovery();
			_battleMgr.BattlePlayer.PlayerBattleView.TurnEndButtonUI.Recovery();
			_battleMgr.BattleResultControl.Recovery();
			if (!_battleMgr.IsBattleEnd)
			{
				IPlayerView playerBattleView = _battleMgr.BattlePlayer.PlayerBattleView;
				playerBattleView.TurnEndButtonUI._isButtonForcedOff = false;
				_battleMgr.BattlePlayer.TurnStartEffectEnd();
				playerBattleView.HideTurnEndPulseEffect();
				playerBattleView.ShowTurnEndButton();
			}
		}), new BattleLoadingEndVfx(_battleMgr), vfxBase, sequentialVfxPlayer);
	}

	public static VfxBase RestoreUI(BattleManagerBase battleMgr)
	{
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		sequentialVfxPlayer.Register((!battleMgr.BattlePlayer.NowTurnEvol) ? ((VfxBase)InstantVfx.Create(delegate
		{
			battleMgr.BattlePlayer.BattleView.EpIcon.GetComponent<UISprite>().spriteName = "battle_icon_evo_off";
		})) : ((VfxBase)NullVfx.GetInstance()));
		sequentialVfxPlayer.Register((!battleMgr.BattleEnemy.NowTurnEvol) ? ((VfxBase)InstantVfx.Create(delegate
		{
			battleMgr.BattleEnemy.BattleView.EpIcon.GetComponent<UISprite>().spriteName = "battle_icon_evo_off";
		})) : ((VfxBase)NullVfx.GetInstance()));
		sequentialVfxPlayer.Register(new DummyDeckChangeCardVfx(isPlayer: true, battleMgr.BattlePlayer.DeckCardList.Count));
		sequentialVfxPlayer.Register(new DummyDeckChangeCardVfx(isPlayer: false, battleMgr.BattleEnemy.DeckCardList.Count));
		sequentialVfxPlayer.Register(ParallelVfxPlayer.Create(battleMgr.BattlePlayer.Class.SkillApplyInformation.AllSkillEffectRestart(), battleMgr.BattleEnemy.Class.SkillApplyInformation.AllSkillEffectRestart()));
		foreach (BattleCardBase handCard in battleMgr.BattlePlayer.HandCardList)
		{
			sequentialVfxPlayer.Register(handCard.BattleCardView.ShowHandCardInfo(isRecovery: true));
		}
		List<BattleCardBase> list = battleMgr.BattlePlayer.InPlayCards.ToList();
		list.AddRange(battleMgr.BattleEnemy.InPlayCards.ToList());
		for (int num = 0; num < list.Count; num++)
		{
			sequentialVfxPlayer.Register(list[num].BattleCardView.InitializeBattleCardIcon(list[num], list[num].Skills));
			sequentialVfxPlayer.Register(list[num].BattleCardView.ShowBattleCardIcon());
		}
		return sequentialVfxPlayer;
	}

	public void Pause()
	{
	}
}
