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

namespace Wizard;

public class QuestFinishTask : BaseTask
{
	public class QuestFinishTaskParam : BaseParam
	{
		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;
	}

	public class QuestFinishTaskParamForPuzzle : BaseParam
	{
		public int puzzle_id;

		public bool is_win;

		public int retry_count;
	}

	public QuestFinishTask(bool isPuzzle = false)
	{
		base.type = (isPuzzle ? ApiType.Type.QuestPuzzleFinish : ApiType.Type.QuestFinish);
	}

	public void SetParameter(int quest_stage_id, int deck_no, bool is_win, Format format, bool isPreBuildDeck, bool isTrialDeck, bool isFirst, int totalTurn)
	{
		QuestFinishTaskParam questFinishTaskParam = new QuestFinishTaskParam();
		questFinishTaskParam.quest_stage_id = quest_stage_id;
		questFinishTaskParam.deck_no = deck_no;
		questFinishTaskParam.is_win = is_win;
		questFinishTaskParam.deck_format = Data.FormatConvertApi(format);
		questFinishTaskParam.is_prebuild_deck = isPreBuildDeck;
		questFinishTaskParam.is_trial_deck = isTrialDeck;
		questFinishTaskParam.turn_state = ((!isFirst) ? 1 : 0);
		questFinishTaskParam.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"));
		}
		questFinishTaskParam.recovery_data = dataMgr.RecoveryData.ToJson();
		questFinishTaskParam.mission = dataMgr.MissionNecessaryInformation.GetMissionNecessaryInfo(battlePlayerPair, selfClass);
		base.Params = questFinishTaskParam;
	}

	public void SetParameterForPuzzle(int puzzleId, bool isWin)
	{
		base.Params = new QuestFinishTaskParamForPuzzle
		{
			puzzle_id = puzzleId,
			is_win = isWin,
			retry_count = (BattleManagerBase.GetIns() as PuzzleBattleManager).RetryCount
		};
	}

	protected override int Parse()
	{
		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();
		JsonData jsonData2 = jsonData["clear_mission_list"]["common_mission"];
		Data.QuestFinish.data.CommonMissionClearInfoList = new List<QuestFinishDetail.MissionClearInfo>();
		for (int i = 0; i < jsonData2.Count; i++)
		{
			Data.QuestFinish.data.CommonMissionClearInfoList.Add(new QuestFinishDetail.MissionClearInfo(jsonData2[i]["name"].ToString(), jsonData2[i]["point"].ToInt()));
		}
		JsonData jsonData3 = jsonData["clear_mission_list"]["character_mission"];
		Data.QuestFinish.data.CharacterMissionClearInfoList = new List<QuestFinishDetail.MissionClearInfo>();
		for (int j = 0; j < jsonData3.Count; j++)
		{
			Data.QuestFinish.data.CharacterMissionClearInfoList.Add(new QuestFinishDetail.MissionClearInfo(jsonData3[j]["name"].ToString(), jsonData3[j]["point"].ToInt()));
		}
		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.AddPoint = Data.QuestFinish.data.AddPoint - Data.QuestFinish.data.WinBonusPoint - Data.QuestFinish.data.GetTotalBonusPoint() - Data.QuestFinish.data.GetTotalCommonMissionClearPoint() - Data.QuestFinish.data.GetTotalCharacterMissionClearPoint();
		Data.QuestFinish.data.NecessaryPointList = new List<int>();
		int num2 = 0;
		for (int k = 0; k < jsonData["point_reward_list"].Count; k++)
		{
			int num3 = jsonData["point_reward_list"][k]["point"].ToInt();
			Data.QuestFinish.data.NecessaryPointList.Add(num3 - num2);
			num2 = num3;
		}
		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);
		JsonData data = base.ResponseData["data"]["achieved_info"];
		Data.QuestFinish.data.AchievedInfo.Read(data);
		Data.QuestFinish.data.HomeDialogData = new MyPageHomeDialogData(jsonData, "battle_dialog_list");
		Data.MyPageNotifications.ParseBadgeInfos(base.ResponseData);
		PlayerStaticData.UpdateHaveUserGoodsNumByJsonData(base.ResponseData["data"]["reward_list"]);
		return num;
	}
}
