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

namespace Wizard;

public class StoryFinishTask : BaseTask
{
	public class StoryFinishTaskParam : BaseParam
	{
		public int story_id;

		public int is_finish;

		public int evolve_count;

		public int total_turn;

		public int deck_no;

		public int use_build_deck;

		public int deck_format;

		public int class_id;

		public Dictionary<string, int> mission;

		public string recovery_data;

		public string[] prosessing_time_data;
	}

	public class StoryFinishTaskParamNoBattle : BaseParam
	{
		public int story_id;

		public int is_finish;

		public string selection_chapter_id;

		public bool is_select_another_end;
	}

	private readonly int _storyId;

	public StoryFinishTask(SelectedStoryInfo storyInfo)
	{
		base.type = GetApiType(storyInfo.StoryApiType);
		_storyId = storyInfo.FinishStoryId;
	}

	private static ApiType.Type GetApiType(StoryApiType storyType)
	{
		return storyType switch
		{
			StoryApiType.MainStory => ApiType.Type.MainStoryFinish, 
			StoryApiType.LimitedStory => ApiType.Type.LimitedStoryFinish, 
			StoryApiType.EventStory => ApiType.Type.EventStoryFinish, 
			_ => throw new NotImplementedException(), 
		};
	}

	public void SetParameter(int is_finish, int evolve_count, int total_turn, int deck_no, bool is_build_deck, Format format, int class_id)
	{
		StoryFinishTaskParam storyFinishTaskParam = new StoryFinishTaskParam();
		BattleManagerBase ins = BattleManagerBase.GetIns();
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		storyFinishTaskParam.story_id = _storyId;
		storyFinishTaskParam.is_finish = is_finish;
		storyFinishTaskParam.evolve_count = evolve_count;
		storyFinishTaskParam.total_turn = total_turn;
		storyFinishTaskParam.deck_no = deck_no;
		storyFinishTaskParam.use_build_deck = (is_build_deck ? 1 : 0);
		storyFinishTaskParam.deck_format = Data.FormatConvertApi(format);
		storyFinishTaskParam.class_id = class_id;
		if (RecoveryRecordManagerBase.IsExistsSingleRecoveryFile())
		{
			if (dataMgr.RecoveryData == null)
			{
				dataMgr.SetRecoveryData(RecoveryOperationInfo.ReadRecoveryFile(OperationRecorderBase.RecordDirectoryPath + "recovery_single.json"));
			}
			storyFinishTaskParam.recovery_data = dataMgr.RecoveryData.ToJson();
		}
		BattlePlayerPair battlePlayerPair = ins.GetBattlePlayerPair(isPlayer: true);
		BattleCardBase selfClass = ins.GetBattlePlayer(isPlayer: true).Class;
		storyFinishTaskParam.mission = dataMgr.MissionNecessaryInformation.GetMissionNecessaryInfo(battlePlayerPair, selfClass);
		base.Params = storyFinishTaskParam;
	}

	public void SetParameterNoBattle(bool isFinish, bool isSelectAnotherEnding, string chosenUnlockChapterId)
	{
		base.Params = new StoryFinishTaskParamNoBattle
		{
			story_id = _storyId,
			is_finish = (isFinish ? 1 : 0),
			is_select_another_end = isSelectAnotherEnding,
			selection_chapter_id = (isFinish ? chosenUnlockChapterId : null)
		};
	}

	protected override int Parse()
	{
		int num = base.Parse();
		if (num != 1)
		{
			DeleteRecoveryFileIfBattleAlreadyEnded(num);
			return num;
		}
		Data.StoryFinish.data = new StoryFinishDetail();
		Data.StoryFinish.data._responseData = base.ResponseData;
		Data.StoryFinish.data.class_chara_experience = 0;
		Data.StoryFinish.data.class_chara_level = 0;
		Data.StoryFinish.data.get_class_chara_experience = base.ResponseData["data"]["get_class_experience"].ToInt();
		Data.StoryFinish.data.class_chara_experience = base.ResponseData["data"]["class_experience"].ToInt();
		Data.StoryFinish.data.class_chara_level = base.ResponseData["data"]["class_level"].ToInt();
		Data.MyPageNotifications.ParseBadgeInfos(base.ResponseData);
		JsonData jsonData = base.ResponseData["data"]["achieved_info"];
		if (jsonData.GetJsonType() == JsonType.Object)
		{
			Data.StoryFinish.data.AchievedInfo.Read(jsonData);
		}
		Data.StoryFinish.data.StoryClearRewards = GetStoryClearRewards(base.ResponseData["data"]);
		PlayerStaticData.UpdateHaveUserGoodsNumByJsonData(base.ResponseData["data"]["reward_list"]);
		return num;
	}

	private static ReceivedReward[] GetStoryClearRewards(JsonData rootJsonData)
	{
		JsonData jsonData = rootJsonData["story_reward_list"];
		int count = jsonData.Count;
		ReceivedReward[] array = new ReceivedReward[count];
		for (int i = 0; i < count; i++)
		{
			JsonData jsonData2 = jsonData[i];
			array[i] = new ReceivedReward(jsonData2["reward_type"].ToInt(), jsonData2["reward_id"].ToLong(), jsonData2["reward_num"].ToInt());
		}
		return array;
	}
}
