using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.Battle.Mulligan;
using Wizard.Battle.Operation;
using Wizard.Battle.View;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Recovery;

public class SingleBattleRecoveryManager : RecoveryManagerBase
{
	protected BattlePlayer _battlePlayer;

	protected BattleEnemy _battleEnemy;

	protected IEnumerable<IOperationCommand> _commands;

	protected bool _isBeforeFramePlayerTurn = true;

	private EnemyAI ai;

	public SingleBattleRecoveryManager(string filePath)
		: base(filePath)
	{
	}

	protected override List<int> CreateEnemyDeckIDList(BattleConditionEnemyInfo enemyInfo)
	{
		return enemyInfo.DeckCardInfos.Select((DeckCardInfo i) => i.CardId.Value).ToList();
	}

	public override VfxBase Recovery(BattlePlayer battlePlayer, BattleEnemy battleEnemy, Func<IEnumerator, Coroutine> startCoroutine)
	{
		BattleManagerBase ins = BattleManagerBase.GetIns();
		ins.IsPreRecovery = true;
		_battlePlayer = battlePlayer;
		_battleEnemy = battleEnemy;
		ai = BattleManagerBase.GetIns().EnemyAI as EnemyAI;
		_ = ai;
		_battleEnemy.EnableEnemyAI = false;
		_battlePlayer.Emotion.Enable = false;
		_battleEnemy.Emotion.Enable = false;
		BattleManagerBase.GetIns().SetupEvolCount(_operationInfo.SetupInfo.DidPlayerGoFirst);
		SingleMulliganMgr singleMulliganMgr = new SingleMulliganMgr();
		MulliganInfoControl component = GameMgr.GetIns().GetGameObjMgr().AddUIContainerChildPrefab("Prefab/UI/MulliganInfo")
			.GetComponent<MulliganInfoControl>();
		singleMulliganMgr.InitMulligan(component, battlePlayer.PlayerBattleView);
		StartRecoveryEvent.Call();
		if (!base.HasMulliganInfo)
		{
			EndDataRecoveryEvent.Call();
			EndRecoveryEvent.Call();
			return NullVfx.GetInstance();
		}
		SkillProcessor skillProcessor = new SkillProcessor();
		singleMulliganMgr.MulliganStartDraw(_operationInfo.SetupInfo.DidPlayerGoFirst, skillProcessor);
		foreach (int playerMulliganReplaceCard in _operationInfo.SetupInfo.PlayerMulliganReplaceCards)
		{
			int cardIndex = playerMulliganReplaceCard;
			BattleCardBase battleCardBase = battlePlayer.HandCardList.SingleOrDefault((BattleCardBase c) => c.Index == cardIndex);
			if (battleCardBase != null)
			{
				singleMulliganMgr.AbandonList.Add(battleCardBase);
			}
		}
		singleMulliganMgr.Submit(BattleManagerBase.GetIns());
		singleMulliganMgr.GetMulliganInfo().SetPlayerReady();
		_commands = _operationInfo.ActionCommands;
		_needUpdate = true;
		ins.IsPreRecovery = false;
		ins.IsRecovery = true;
		return NullVfx.GetInstance();
	}

	public override VfxBase UpdateRecovery()
	{
		if (!_needUpdate)
		{
			return NullVfx.GetInstance();
		}
		BattleManagerBase ins = BattleManagerBase.GetIns();
		if (_isBeforeFramePlayerTurn)
		{
			_ = _battleEnemy.IsSelfTurn;
		}
		if (_battleEnemy.IsSelfTurn)
		{
			foreach (BattleCardBase handCard in _battleEnemy.HandCardList)
			{
				handCard.SetOnDraw(draw: false);
			}
		}
		_isBeforeFramePlayerTurn = _battlePlayer.IsSelfTurn;
		IOperationCommand operationCommand = _commands.FirstOrDefault();
		if (operationCommand != null)
		{
			if (ai != null)
			{
				ai.UpdateAICurrentVirtualField();
			}
			operationCommand.Operation(ins);
		}
		_commands = _commands.Skip(1);
		if (_commands.Any() || _commands is TurnEndOperationCommand)
		{
			return NullVfx.GetInstance();
		}
		if (ins is SingleBattleMgr)
		{
			((SingleBattleMgr)ins).LifeZeroActivateLeonSkillIfNeeded();
		}
		if (ai != null)
		{
			ai.UpdateAICurrentVirtualField();
		}
		EndDataRecovery();
		RecoveryTurnStartPanel();
		_battlePlayer.PlayerBattleView.ClearPlayQueue();
		if (_battlePlayer.IsSelfTurn)
		{
			_battlePlayer.TurnStartEffectEnd();
			_battlePlayer._isPlayerActive = true;
		}
		_battleEnemy.BattleEnemyView.ClearPlayQueue();
		ParallelVfxPlayer parallelVfxPlayer = ParallelVfxPlayer.Create(InstantVfx.Create(delegate
		{
			GameMgr.GetIns().GetBattleCtrl().StartCoroutine(FontChanger.FontChange(null));
		}), _battlePlayer.BattleView.Recovery(_operationInfo.SetupInfo.DidPlayerGoFirst, _battlePlayer.HandCardList.Count > 0), _battleEnemy.BattleView.Recovery(!_operationInfo.SetupInfo.DidPlayerGoFirst), _battlePlayer.Recovery(), _battleEnemy.Recovery());
		string path = GameMgr.GetIns().GetDataMgr().GetPlayerSkinId()
			.ToString("00");
		string path2 = GameMgr.GetIns().GetDataMgr().GetEnemySkinId()
			.ToString("00");
		parallelVfxPlayer.Register(new WaitLoadResourceVfx(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.ClassCharaBase)));
		parallelVfxPlayer.Register(new WaitLoadResourceVfx(Toolbox.ResourcesManager.GetAssetTypePath(path2, ResourcesManager.AssetLoadPathType.ClassCharaBase)));
		IBattlePlayerView battlePlayerView = (_battleEnemy.IsSelfTurn ? _battleEnemy.BattleView : _battlePlayer.BattleView);
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		sequentialVfxPlayer.Register(parallelVfxPlayer);
		sequentialVfxPlayer.Register(HandViewBase.CreateHideCardMeshesVfx(_battleEnemy.HandCardList));
		sequentialVfxPlayer.Register(InstantVfx.Create(delegate
		{
			_battleEnemy.EnableEnemyAI = true;
			_battlePlayer.Emotion.Enable = true;
			_battleEnemy.Emotion.Enable = true;
			ResetLeaderAnimation(_battlePlayer, _battleEnemy);
			_battlePlayer.UpdateHandCardsPlayability();
			Dump(_battlePlayer, _battleEnemy);
			EndRecoveryEvent.Call();
		}));
		sequentialVfxPlayer.Register(battlePlayerView.RecoveryTurnStart());
		ins.IsRecovery = false;
		return sequentialVfxPlayer;
	}

	public SetupConditionInfo GetSetupConditionInfo()
	{
		if (_operationInfo == null)
		{
			return null;
		}
		return _operationInfo.SetupInfo;
	}

	public void CreateRecoveryFileFromTempFile()
	{
		if (SingleBattleRecoveryRecordManager.IsExistsTempSingleRecoveryFile())
		{
			string recordDirectoryPath = OperationRecorderBase.RecordDirectoryPath;
			string sourceFileName = recordDirectoryPath + "temp_recovery_single.json";
			string destFileName = recordDirectoryPath + "recovery_single.json";
			File.Copy(sourceFileName, destFileName, overwrite: true);
			SingleBattleRecoveryRecordManager.DeleteTempRecoveryFile();
		}
	}
}
