using System;
using Wizard.Battle.Mulligan;

namespace Wizard.Battle.Recovery;

public class AINetworkBattleRecoveryRecordManager : RecoveryRecordManagerBase
{
	protected override string DefaultRecoveryFileName => "recovery_ai_network.json";

	public AINetworkBattleRecoveryRecordManager()
	{
	}

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

	public override void SetupRecording(BattleManagerBase battleMgr, DataMgr.BattleType battleType, int randomSeed, int backGroundId, string bgmId = "NONE")
	{
		base.SetupRecording(battleMgr, battleType, randomSeed, backGroundId, bgmId);
		RecordAINetworkBattleSettings(_recorder, battleType, randomSeed, backGroundId, bgmId);
	}

	protected override OperationRecorderBase CreateOperationRecorder()
	{
		return new AINetworkBattleOperationRecorder(_recoveryFilePath);
	}

	protected override void SetupRecorderEvents(OperationRecorderBase operationRecorder, BattleManagerBase battleMgr)
	{
		base.SetupRecorderEvents(operationRecorder, battleMgr);
		BattlePlayer battlePlayer = battleMgr.BattlePlayer;
		battlePlayer.OnTurnStartComplete = (Action)Delegate.Combine(battlePlayer.OnTurnStartComplete, new Action(operationRecorder.RecordTurnStart));
		BattleEnemy battleEnemy = battleMgr.BattleEnemy;
		battleEnemy.OnTurnStartComplete = (Action)Delegate.Combine(battleEnemy.OnTurnStartComplete, new Action(operationRecorder.RecordTurnStart));
		battleMgr.OperateMgr.OnTurnEnd += operationRecorder.RecordTurnEnd;
	}

	public override void SetupMulliganStartTimeRecorderEvent(BattleManagerBase battleMgr)
	{
		PlayerMulliganCtrl playerMlgCtrl = battleMgr.MulliganMgr.PlayerMlgCtrl;
		playerMlgCtrl.OnMulliganLaunchComplete = (Action)Delegate.Combine(playerMlgCtrl.OnMulliganLaunchComplete, new Action(_recorder.RecordMulliganStart));
	}

	protected void RecordAINetworkBattleSettings(OperationRecorderBase operationRecorder, DataMgr.BattleType battleType, int randomSeed, int backGroundId, string bgmId)
	{
		DataMgr dataMgr = _gameMgr.GetDataMgr();
		operationRecorder.RecordBattleType(battleType);
		operationRecorder.RecordRandomSeed(randomSeed);
		operationRecorder.RecordBackGroundId(backGroundId);
		operationRecorder.RecordBgmId(bgmId);
		operationRecorder.RecordClass("player", dataMgr.GetPlayerClassId());
		operationRecorder.RecordSubClass("player", dataMgr.GetPlayerSubClassId());
		if (dataMgr.TryGetPlayerMyRotationInfo(out var myRotationInfo))
		{
			operationRecorder.RecordMyRotationId("player", myRotationInfo.Id);
		}
		operationRecorder.RecordClass("enemy", dataMgr.GetEnemyClassId());
		operationRecorder.RecordSubClass("enemy", dataMgr.GetEnemySubClassId());
		if (dataMgr.TryGetEnemyMyRotationInfo(out var myRotationInfo2))
		{
			operationRecorder.RecordMyRotationId("enemy", myRotationInfo2.Id);
		}
		operationRecorder.RecordChara("player", dataMgr.GetPlayerCharaId());
		operationRecorder.RecordChara("enemy", dataMgr.GetEnemyCharaId());
		operationRecorder.RecordSleeve("player", dataMgr.GetPlayerSleeveId());
		operationRecorder.RecordSleeve("enemy", dataMgr.GetEnemySleeveId());
		operationRecorder.RecordDeck("player", 'p', dataMgr.GetCurrentDeckData());
		operationRecorder.RecordDeck("enemy", 'e', dataMgr.GetCurrentEnemyDeckData());
		operationRecorder.RecordEnemyAIDifficulty(dataMgr.m_EnemyAIDifficulty);
		operationRecorder.RecordEnemyAILogicLevel(dataMgr.m_EnemyAILogicLevel);
		operationRecorder.RecordEnemyAIMaxLife(dataMgr.m_EnemyAIMaxLife);
		operationRecorder.RecordEnemyAIDeckId(dataMgr.m_EnemyAIDeckId);
		operationRecorder.RecordEnemyAIStyleId(dataMgr.m_EnemyAIStyleId);
		operationRecorder.RecordEnemyAIEmoteId(dataMgr.m_EnemyAIEmoteId);
		operationRecorder.RecordEnemyAIUseInnerEmote(dataMgr.m_EnemyAIUseInnerEmote);
		operationRecorder.RecordPracticeDifficultyDegreeId(dataMgr.PracticeDifficultyDegreeId);
		operationRecorder.RecordIsPreBuildDeck(dataMgr.IsLastSelectDeckAttributeType(DeckAttributeType.BuildDeck));
		operationRecorder.RecordIsTrialDeck(dataMgr.IsLastSelectDeckAttributeType(DeckAttributeType.TrialDeck));
	}

	public void RecordChangeAI(string logicName, int operationQueueCount)
	{
		if (_recorder is SingleBattleOperationRecorder singleBattleOperationRecorder)
		{
			singleBattleOperationRecorder.RecordChangeAI(logicName, operationQueueCount);
		}
	}
}
