using System;
using System.Collections.Generic;
using LitJson;
using Wizard.Battle.Recovery;

namespace Wizard;

public class BossRushFinishTask : BaseTask
{
	public class BossRushFinishTaskParam : BaseParam
	{
		public int current_life;

		public int max_life;

		public int quest_stage_id;

		public int deck_no;

		public bool is_win;

		public int deck_format;

		public bool is_prebuild_deck;

		public bool is_trial_deck;

		public int total_turn;

		public int turn_state;

		public Dictionary<string, int> mission;

		public string recovery_data;

		public string[] prosessing_time_data;
	}

	private bool _isWin;

	public BossRushFinishTask()
	{
		base.type = ApiType.Type.BossRushFinish;
	}

	public void SetParameter(int currentLife, int maxLife, int quest_stage_id, int deck_no, bool is_win, Format format, bool isPreBuildDeck, bool isTrialDeck, bool isFirst, int totalTurn)
	{
		_isWin = is_win;
		BossRushFinishTaskParam bossRushFinishTaskParam = new BossRushFinishTaskParam();
		bossRushFinishTaskParam.current_life = Math.Max(0, currentLife);
		bossRushFinishTaskParam.max_life = Math.Max(0, maxLife);
		bossRushFinishTaskParam.quest_stage_id = quest_stage_id;
		bossRushFinishTaskParam.deck_no = deck_no;
		bossRushFinishTaskParam.is_win = is_win;
		bossRushFinishTaskParam.deck_format = Data.FormatConvertApi(format);
		bossRushFinishTaskParam.is_prebuild_deck = isPreBuildDeck;
		bossRushFinishTaskParam.is_trial_deck = isTrialDeck;
		bossRushFinishTaskParam.turn_state = ((!isFirst) ? 1 : 0);
		bossRushFinishTaskParam.total_turn = totalTurn;
		BattleManagerBase ins = BattleManagerBase.GetIns();
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		BattlePlayerPair battlePlayerPair = ins.GetBattlePlayerPair(isPlayer: true);
		BattleCardBase selfClass = ins.GetBattlePlayer(isPlayer: true).Class;
		if (dataMgr.RecoveryData == null)
		{
			dataMgr.SetRecoveryData(RecoveryOperationInfo.ReadRecoveryFile(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json"));
		}
		bossRushFinishTaskParam.recovery_data = dataMgr.RecoveryData.ToJson();
		bossRushFinishTaskParam.mission = dataMgr.MissionNecessaryInformation.GetMissionNecessaryInfo(battlePlayerPair, selfClass);
		base.Params = bossRushFinishTaskParam;
	}

	protected override int Parse()
	{
		GameMgr.GetIns().GetDataMgr().ClearSpecialBattleSettingInfo();
		int num = base.Parse();
		if (num != 1)
		{
			DeleteRecoveryFileIfBattleAlreadyEnded(num);
			return num;
		}
		Data.QuestFinish.data = new QuestFinishDetail();
		Data.QuestFinish.data._responseData = base.ResponseData;
		JsonData jsonData = base.ResponseData["data"];
		if (base.type == ApiType.Type.QuestPuzzleFinish)
		{
			Data.QuestFinish.data.PuzzleQuestInfo = new PuzzleQuestInfo(jsonData);
		}
		Data.QuestFinish.data.get_class_chara_experience = jsonData["get_class_experience"].ToInt();
		Data.QuestFinish.data.class_chara_experience = jsonData["class_experience"].ToInt();
		Data.QuestFinish.data.class_chara_level = jsonData["class_level"].ToInt();
		Data.QuestFinish.data.CurrentPoint = jsonData["current_point"].ToInt();
		Data.QuestFinish.data.AddPoint = jsonData["add_point"].ToInt();
		Data.QuestFinish.data.ClassBonusPoint = jsonData["class_bonus_point"].ToInt();
		Data.QuestFinish.data.FormatBonusPoint = jsonData["format_bonus_point"].ToInt();
		Data.QuestFinish.data.CommonMissionClearInfoList = new List<QuestFinishDetail.MissionClearInfo>();
		Data.QuestFinish.data.CharacterMissionClearInfoList = new List<QuestFinishDetail.MissionClearInfo>();
		Data.QuestFinish.data.WinBonusPoint = jsonData.GetValueOrDefault("win_bonus_point", 0);
		Data.QuestFinish.data.WinCount = jsonData.GetValueOrDefault("win_count", 0);
		Data.QuestFinish.data.WinCountForWinBonusPoint = jsonData.GetValueOrDefault("required_win_count_for_win_bonus_point", 0);
		Data.QuestFinish.data.WinBonusPointStatus = (QuestFinishDetail.WinBonusStatus)jsonData.GetValueOrDefault("win_bonus_point_status", 0);
		Data.QuestFinish.data.NecessaryPointList = new List<int>();
		int num2 = 0;
		for (int i = 0; i < jsonData["point_reward_list"].Count; i++)
		{
			int num3 = jsonData["point_reward_list"][i]["point"].ToInt();
			Data.QuestFinish.data.NecessaryPointList.Add(num3 - num2);
			num2 = num3;
		}
		Data.QuestFinish.data.CurrentLife = jsonData["current_life"].ToInt();
		Data.QuestFinish.data.MaxLife = jsonData["max_life"].ToInt();
		Data.QuestFinish.data.NecessaryPointList.Add(-1);
		Data.QuestFinish.data.IsSpecialResult = jsonData.GetValueOrDefault("is_special_result", defaultValue: false);
		Data.QuestFinish.data.IsSpecialEffect = jsonData.GetValueOrDefault("is_special_effect", defaultValue: false);
		int val = 9999;
		Data.QuestFinish.data.BossRushShortestTurn = Math.Min(jsonData.GetValueOrDefault("shortest_clear_turns", 0), val);
		Data.QuestFinish.data.BossRushTotalTurn = Math.Min(jsonData.GetValueOrDefault("total_turns", 0), val);
		if (_isWin)
		{
			Data.QuestFinish.data.IsEnableBossRushShortestTurn = true;
		}
		else
		{
			Data.QuestFinish.data.IsEnableBossRushShortestTurn = Data.QuestFinish.data.BossRushShortestTurn != 0;
		}
		if (Data.QuestFinish.data.BossRushShortestTurn == 0 && _isWin)
		{
			Data.QuestFinish.data.IsBossRushNewRecord = true;
		}
		else if (_isWin && Data.QuestFinish.data.BossRushTotalTurn < Data.QuestFinish.data.BossRushShortestTurn)
		{
			Data.QuestFinish.data.IsBossRushNewRecord = true;
			Data.QuestFinish.data.BossRushShortestTurn = Data.QuestFinish.data.BossRushTotalTurn;
		}
		if (Data.QuestFinish.data.BossRushTotalTurn > 0 && Data.QuestFinish.data.BossRushShortestTurn == 0)
		{
			Data.QuestFinish.data.BossRushShortestTurn = Data.QuestFinish.data.BossRushTotalTurn;
		}
		JsonData data = base.ResponseData["data"]["achieved_info"];
		Data.QuestFinish.data.AchievedInfo.Read(data);
		Data.QuestFinish.data.HomeDialogData = new MyPageHomeDialogData(jsonData, "battle_dialog_list");
		PlayerStaticData.UpdateHaveUserGoodsNumByJsonData(base.ResponseData["data"]["reward_list"]);
		return num;
	}
}
