using System.Collections.Generic;
using System.IO;
using LitJson;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Recovery;

public class NetworkBattleOperationRecorder : OperationRecorderBase
{
	public NetworkBattleOperationRecorder(string filePath)
		: base(filePath)
	{
		if (File.Exists(filePath))
		{
			JsonData operationDataList = RecoveryOperationInfo.ReadRecoveryFile(filePath);
			SetOperationDataList(operationDataList);
		}
		else
		{
			WriteJsonData();
		}
	}

	public override void RecordBattleType(DataMgr.BattleType battleType)
	{
	}

	public override void RecordRandomSeed(int randomSeed)
	{
	}

	public override void RecordBackGroundId(int backGroundId)
	{
	}

	public override void RecordBgmId(string bgmId)
	{
	}

	public override void RecordClass(string playerName, int clanType)
	{
	}

	public override void RecordSubClass(string playerName, int clanType)
	{
	}

	public override void RecordMyRotationId(string playerName, string myRotationId)
	{
	}

	public override void RecordSleeve(string playerName, long sleeveId)
	{
	}

	public override void RecordChara(string playerName, int charaId)
	{
	}

	public override void RecordDeck(string playerName, char indexHeadChar, IEnumerable<int> cardIds)
	{
	}

	public override void RecordEnemyAIDifficulty(int difficulty)
	{
	}

	public override void RecordEnemyAILogicLevel(int level)
	{
	}

	public override void RecordEnemyAIMaxLife(int life)
	{
	}

	public override void RecordEnemyAIDeckId(int deckId)
	{
	}

	public override void RecordEnemyAIStyleId(int styleId)
	{
	}

	public override void RecordEnemyAIEmoteId(int emoteId)
	{
	}

	public override void RecordEnemyAIUseInnerEmote(bool useInnerEmote)
	{
	}

	public override void RecordStartTurnIsPlayer(bool startTurnIsPlayer)
	{
	}

	public override void RecordPracticeDifficultyDegreeId(int degreeId)
	{
	}

	public override void RecordIsPreBuildDeck(bool isPreBuildDeck)
	{
	}

	public override void RecordIsTrialDeck(bool isTrialDeck)
	{
	}

	public override void RecordIsDefaultDeck(bool isDefaultDeck)
	{
	}

	public override void RecordQuestStageId(int id)
	{
	}

	public override void RecordQuestEnemyAiId(int id)
	{
	}

	public override void RecordQuestEnemyEmblemId(int id)
	{
	}

	public override void RecordQuestEnemyDegreeId(int id)
	{
	}

	public override void RecordQuestEnemyEmotionOverride(int id)
	{
	}

	public override void RecordQuestPlayerEmotionOverride(int id)
	{
	}

	public override void RecordQuestRecoveryPoint(int recoveryPoint)
	{
	}

	public override void RecordQuestPlayerSkillList(List<BossRushSpecialSkill> skills)
	{
	}

	public override void RecordQuestEnemySkill(BossRushSpecialSkill skill)
	{
	}

	public override void RecordQuestMaxBattleCount(int maxBattleCount)
	{
	}

	public override void RecordQuestCurrentWinCount(int currentWinCount)
	{
	}

	public override void RecordQuestIsExtra(bool isExtra)
	{
	}

	public override void RecordQuestIsMockBattle(bool isMockBattle)
	{
	}

	public override void RecordQuestExtraDeckScheduleId(int id)
	{
	}

	public override void RecordMissionNecessaryInformation(BattleManagerBase.MissionNecessaryInformation missionNecessaryInformation)
	{
	}

	public override void RecordMulliganStart()
	{
	}

	public override void RecordPractice3DFieldId(int id)
	{
	}

	public override void RecordPlayerMulliganReplaceCards(IEnumerable<BattleCardBase> replaceCards, IEnumerable<int> completeCards)
	{
		JsonData jsonData = new JsonData();
		JsonData jsonData2 = new JsonData();
		jsonData2.SetJsonType(JsonType.Array);
		foreach (BattleCardBase replaceCard in replaceCards)
		{
			jsonData2.Add(replaceCard.GetName());
		}
		jsonData["operation"] = "mulligan";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["abandoned_cards"] = jsonData2;
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordEnemyMulliganReplaceCards(IEnumerable<BattleCardBase> replaceCards, IEnumerable<int> completeCards)
	{
	}

	public override void RecordPlay(BattleCardBase card, BattleCardBase originalCard, IEnumerable<BattleCardBase> selectedCards)
	{
		if (card.IsPlayer)
		{
			JsonData jsonData = new JsonData();
			jsonData["operation"] = "play";
			jsonData["seq"] = GetCurrentSequenceNumber();
			jsonData["index"] = CardToIndexName(card);
			jsonData["is_choice_brave"] = (originalCard.IsChoiceBraveSkillCard ? 1 : 0);
			WriteSkillTargetInfoToJson(selectedCards, jsonData);
			_operationJsonDataList.Add(jsonData);
			WriteJsonData();
		}
	}

	public override VfxBase RecordAttack(BattleCardBase attackCard, BattleCardBase targetCard, SkillProcessor skillProcessor)
	{
		if (!attackCard.IsPlayer)
		{
			return NullVfx.GetInstance();
		}
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "attack";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(attackCard);
		jsonData["target"] = CardToIndexName(targetCard);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
		return NullVfx.GetInstance();
	}

	public override void RecordEvolve(BattleCardBase originalCard, BattleCardBase card, IEnumerable<BattleCardBase> selectedCards)
	{
		if (card.IsPlayer)
		{
			JsonData jsonData = new JsonData();
			jsonData["operation"] = "evolve";
			jsonData["seq"] = GetCurrentSequenceNumber();
			jsonData["index"] = CardToIndexName(card);
			WriteSkillTargetInfoToJson(selectedCards, jsonData);
			_operationJsonDataList.Add(jsonData);
			WriteJsonData();
		}
	}

	public override void RecordCompleteFusionSelect(BattleCardBase fusionCard, IEnumerable<BattleCardBase> ingredientCards)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "comp_fusion";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(fusionCard);
		WriteSkillTargetInfoToJson(ingredientCards, jsonData);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordStartFusion(BattleCardBase fusionCard, List<BattleCardBase> selectableCards)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "start_fusion";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(fusionCard);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordSelectFusion(BattleCardBase card)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "select_fusion";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordCancelFusion(BattleCardBase card)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "cancel_fusion";
		jsonData["seq"] = GetCurrentSequenceNumber();
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordStartSelect(BattleCardBase card, bool isEvolve, List<BattleCardBase> selectableCards, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "start_select";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		jsonData["bool"] = (isEvolve ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordSelect(BattleCardBase card, bool isEvolve, BattleCardBase actCard, bool isBurialRite, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "select";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		jsonData["bool"] = (isEvolve ? 1 : 0);
		jsonData["is_burial_rite"] = (isBurialRite ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordCompleteSelect(BattleCardBase card, bool isEvolve, BattleCardBase actCard, bool isChoiceBrave, bool isBurialRite)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "comp_select";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		jsonData["bool"] = (isEvolve ? 1 : 0);
		jsonData["is_burial_rite"] = (isBurialRite ? 1 : 0);
		jsonData["is_choice_brave"] = (isChoiceBrave ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordCancelSelect(BattleCardBase actCard, bool isEvolve, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "cancel_select";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["bool"] = (isEvolve ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordStartChoice(BattleCardBase card, bool isEvolve, List<BattleCardBase> choiceCards, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "start_choice";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		jsonData["bool"] = (isEvolve ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordCompleteChoice(BattleCardBase card, bool isEvolve, List<BattleCardBase> chosenCardList, BattleCardBase actCard, List<int> chosenCardIndexList, bool hasSelectionSkill, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "comp_choice";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["index"] = CardToIndexName(card);
		jsonData["bool"] = (isEvolve ? 1 : 0);
		WriteSkillTargetInfoToJson(chosenCardList, jsonData);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordCancelChoice(BattleCardBase card, bool isEvolve, bool isChoiceBrave)
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "cancel_choice";
		jsonData["seq"] = GetCurrentSequenceNumber();
		jsonData["bool"] = (isEvolve ? 1 : 0);
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordTurnStart()
	{
	}

	public override void RecordTurnEnd()
	{
		JsonData jsonData = new JsonData();
		jsonData["operation"] = "turn_end";
		jsonData["seq"] = GetCurrentSequenceNumber();
		_operationJsonDataList.Add(jsonData);
		WriteJsonData();
	}

	public override void RecordRetire()
	{
	}

	public override void RecordSkillTargets(IEnumerable<BattleCardBase> targetCards)
	{
	}

	protected override void WriteJsonData()
	{
		JsonData jsonData = new JsonData();
		jsonData["operations"] = new JsonData();
		jsonData["operations"].SetJsonType(JsonType.Array);
		foreach (JsonData operationJsonData in _operationJsonDataList)
		{
			jsonData["operations"].Add(operationJsonData);
		}
		WriteJsonDataToFile(jsonData);
	}

	private void SetOperationDataList(JsonData recoveryLogData)
	{
		if (recoveryLogData.Keys.Contains("operations"))
		{
			JsonData jsonData = recoveryLogData["operations"];
			for (int i = 0; i < jsonData.Count; i++)
			{
				_operationJsonDataList.Add(jsonData[i]);
			}
		}
		else
		{
			LocalLog.AccumulateTraceLog("operations key is not found in recovery file");
		}
	}

	private int GetCurrentSequenceNumber()
	{
		if (ToolboxGame.RealTimeNetworkAgent != null)
		{
			return ToolboxGame.RealTimeNetworkAgent.GetCurrentSequenceNumber() + 1;
		}
		return 0;
	}
}
