using System;
using System.Collections.Generic;
using System.IO;
using Cute;
using LitJson;
using Wizard.AutoTest;
using Wizard.ErrorDialog;

namespace Wizard.Battle.Recovery;

public abstract class RecoveryRecordManagerBase : IRecoveryRecordManager
{
	protected OperationRecorderBase _recorder;

	protected GameMgr _gameMgr;

	protected DataMgr.BattleType _battleType;

	protected readonly string _recoveryFilePath;

	public const string DEFAULT_RECOVERY_SINGLE_FILE_NAME = "recovery_single.json";

	public const string DEFAULT_RECOVERY_NETWORK_FILE_NAME = "recovery_network.json";

	public const string DEFAULT_RECOVERY_AI_NETWORK_FILE_NAME = "recovery_ai_network.json";

	protected abstract string DefaultRecoveryFileName { get; }

	public RecoveryRecordManagerBase()
	{
		_recoveryFilePath = OperationRecorderBase.RecordDirectoryPath + DefaultRecoveryFileName;
	}

	public RecoveryRecordManagerBase(string recoveryFilePath)
	{
		_recoveryFilePath = OperationRecorderBase.RecordDirectoryPath + recoveryFilePath;
	}

	public void RecordSkillTarget(IEnumerable<BattleCardBase> targetCards)
	{
		_recorder.RecordSkillTargets(targetCards);
	}

	public virtual void SetupRecording(BattleManagerBase battleMgr, DataMgr.BattleType battleType, int randomSeed, int backGroundId, string bgmId = "NONE")
	{
		Directory.CreateDirectory(OperationRecorderBase.RecordDirectoryPath);
		_gameMgr = GameMgr.GetIns();
		_battleType = battleType;
		_recorder = CreateOperationRecorder();
		SetupRecorderEvents(_recorder, battleMgr);
	}

	public virtual void SetupMulliganStartTimeRecorderEvent(BattleManagerBase battleMgr)
	{
	}

	protected abstract OperationRecorderBase CreateOperationRecorder();

	protected virtual void SetupRecorderEvents(OperationRecorderBase operationRecorder, BattleManagerBase battleMgr)
	{
		battleMgr.OnStartOpening += operationRecorder.RecordStartTurnIsPlayer;
		battleMgr.BattlePlayer.OnMulliganEnd += operationRecorder.RecordPlayerMulliganReplaceCards;
		battleMgr.BattleEnemy.OnMulliganEnd += operationRecorder.RecordEnemyMulliganReplaceCards;
		battleMgr.OperateMgr.OnSetCardSuccess += operationRecorder.RecordPlay;
		battleMgr.OperateMgr.OnBeforeAttack += operationRecorder.RecordAttack;
		battleMgr.OperateMgr.OnEvolveSuccess += operationRecorder.RecordEvolve;
		battleMgr.OperateMgr.OnStartSelect += operationRecorder.RecordStartSelect;
		battleMgr.OperateMgr.OnSelect += operationRecorder.RecordSelect;
		battleMgr.OperateMgr.OnCompleteSelect += operationRecorder.RecordCompleteSelect;
		battleMgr.OperateMgr.OnStartChoice += operationRecorder.RecordStartChoice;
		battleMgr.OperateMgr.OnCompleteChoice += operationRecorder.RecordCompleteChoice;
		battleMgr.OperateMgr.OnCancelSelect += operationRecorder.RecordCancelSelect;
		battleMgr.OperateMgr.OnCancelChoice += operationRecorder.RecordCancelChoice;
		battleMgr.OperateMgr.OnStartFusion += operationRecorder.RecordStartFusion;
		battleMgr.OperateMgr.OnSelectFusionForRecovery += operationRecorder.RecordSelectFusion;
		battleMgr.OperateMgr.OnCancelFusion += operationRecorder.RecordCancelFusion;
		battleMgr.OperateMgr.OnBeforeFusion += operationRecorder.RecordCompleteFusionSelect;
		if (battleMgr is SingleBattleMgr singleBattleMgr && _gameMgr.GetDataMgr().BossRushBattleData != null)
		{
			singleBattleMgr.OnBattleRetire += operationRecorder.RecordRetire;
		}
	}

	public static bool IsExistsNetworkRecoveryFile()
	{
		return File.Exists(OperationRecorderBase.RecordDirectoryPath + "recovery_network.json");
	}

	public static bool IsExistsAINetworkRecoveryFile()
	{
		return File.Exists(OperationRecorderBase.RecordDirectoryPath + "recovery_ai_network.json");
	}

	public static bool IsExistsSingleRecoveryFile()
	{
		return File.Exists(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json");
	}

	public static bool IsBossRushBattleRetired()
	{
		if (!IsExistsSingleRecoveryFile())
		{
			return false;
		}
		JsonData jsonData;
		try
		{
			jsonData = RecoveryOperationInfo.ReadRecoveryFile(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json");
		}
		catch (Exception ex)
		{
			AbortSoloPlayRecoveryTask task = new AbortSoloPlayRecoveryTask();
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
			{
				Data.Load.data.InitRecoveryStatus();
				UIManager.GetInstance().isBattleRecovery = false;
				DeleteRecoveryFile();
				DialogBase dialogBase = Wizard.ErrorDialog.Dialog.Create(20001);
				dialogBase.SetButtonDelegate(delegate
				{
					SoftwareReset.exec();
				});
				dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_CANCEL_TRANS;
			}));
			throw ex;
		}
		if (!jsonData.Keys.Contains("operations"))
		{
			return false;
		}
		for (int num = jsonData["operations"].Count - 1; num >= 0; num--)
		{
			JsonData jsonData2 = jsonData["operations"][num];
			if (jsonData2.Keys.Contains("ope") && jsonData2["ope"].ToString() == "retire")
			{
				return true;
			}
		}
		return false;
	}

	public static void DeleteTempAINetworkRecoveryFile()
	{
		if (File.Exists(OperationRecorderBase.RecordDirectoryPath + "temp_recovery_ai_network.json"))
		{
			File.Delete(OperationRecorderBase.RecordDirectoryPath + "temp_recovery_ai_network.json");
		}
	}

	public static DataMgr.BattleType GetRecoveryBattleType()
	{
		if (IsExistsNetworkRecoveryFile() || IsExistsAINetworkRecoveryFile())
		{
			if (IsExistsAINetworkRecoveryFile())
			{
				GameMgr.GetIns().IsAINetwork = true;
			}
			return Data.BattleRecoveryInfo.BattleType;
		}
		if (IsExistsSingleRecoveryFile())
		{
			JsonData jsonData = RecoveryOperationInfo.ReadRecoveryFile(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json");
			return (DataMgr.BattleType)jsonData.ToIntOrDefault("battle_type", 100);
		}
		return DataMgr.BattleType.None;
	}

	public static void DeleteRecoveryFile()
	{
		if (File.Exists(OperationRecorderBase.RecordDirectoryPath + "recovery_network.json"))
		{
			File.Delete(OperationRecorderBase.RecordDirectoryPath + "recovery_network.json");
		}
		if (File.Exists(OperationRecorderBase.RecordDirectoryPath + "recovery_ai_network.json"))
		{
			File.Delete(OperationRecorderBase.RecordDirectoryPath + "recovery_ai_network.json");
		}
		if (IsExistsSingleRecoveryFile())
		{
			File.Delete(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json");
		}
	}
}
