using System;
using LitJson;
using UnityEngine;

namespace Wizard;

public class ColosseumEntryInfoTask : BaseTask
{
	public ColosseumEntryInfoTask()
	{
		base.type = ApiType.Type.ColosseumEntryInfo;
	}

	protected override int Parse()
	{
		int num = base.Parse();
		if (num != 1)
		{
			return num;
		}
		JsonData jsonData = base.ResponseData["data"];
		JsonData colosseumOwnStatus = jsonData["colosseum_status"];
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		if (jsonData.Keys.Contains("is_unfinished_entry_exists"))
		{
			colosseumData.isJoin = jsonData["is_unfinished_entry_exists"].ToBoolean();
		}
		else
		{
			colosseumData.isJoin = false;
		}
		if (jsonData.Keys.Contains("is_allowed_free_entry"))
		{
			colosseumData.IsFreeEntry = jsonData["is_allowed_free_entry"].ToBoolean();
		}
		else
		{
			colosseumData.IsFreeEntry = false;
		}
		if (jsonData.Keys.Contains("fee_list"))
		{
			colosseumData.rupyCost = jsonData["fee_list"]["rupy_cost"].ToInt();
			colosseumData.ticketCost = jsonData["fee_list"]["ticket_cost"].ToInt();
			colosseumData.crystalCost = jsonData["fee_list"]["crystal_cost"].ToInt();
		}
		if (jsonData.Keys.Contains("deck_format") || !colosseumData.isJoin)
		{
			colosseumData.IsDeckEntry = false;
		}
		else
		{
			colosseumData.IsDeckEntry = true;
		}
		if (jsonData.Keys.Contains("is_able_to_join_round_3") && !jsonData["is_able_to_join_round_3"].ToBoolean())
		{
			colosseumData.Round = ArenaColosseum.eRound.Lose;
			colosseumData.NextRound = ArenaColosseum.eRound.Lose;
		}
		if (jsonData.Keys.Contains("is_already_entry_final_round"))
		{
			colosseumData.IsFinalRoundTry = jsonData["is_already_entry_final_round"].ToBoolean();
		}
		else
		{
			colosseumData.IsFinalRoundTry = false;
		}
		SetColosseumInfo(base.ResponseData);
		SetColosseumOwnStatus(colosseumOwnStatus);
		if (jsonData.Keys.Contains("is_deck_deleted"))
		{
			colosseumData.IsDeckDeleted = true;
		}
		else
		{
			colosseumData.IsDeckDeleted = false;
		}
		if (colosseumData.IsTwoPickRule && !colosseumData.IsDeckEntry)
		{
			if (jsonData.Keys.Contains("two_pick_status"))
			{
				colosseumData.EntryStatus = (ArenaColosseum.eEntryStatus)jsonData["two_pick_status"].ToInt();
			}
			else
			{
				colosseumData.EntryStatus = ArenaColosseum.eEntryStatus.TwoPickClassSelect;
			}
		}
		else
		{
			colosseumData.EntryStatus = ArenaColosseum.eEntryStatus.SetUpComplete;
		}
		return num;
	}

	public static void SetColosseumInfo(JsonData responseData)
	{
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		JsonData jsonData = responseData["data"]["colosseum_info"];
		colosseumData.IsColosseumPeriod = jsonData["is_colosseum_period"].ToBoolean();
		if (colosseumData.IsColosseumPeriod)
		{
			colosseumData.ApiRuleParseAndSet(jsonData["deck_format"].ToInt());
			colosseumData.IsNormalTwoPick = jsonData["is_normal_two_pick"].ToString() == "1";
			colosseumData.Name = jsonData["colosseum_name"].ToString();
			colosseumData.IsRoundPeriod = jsonData["is_round_period"].ToBoolean();
			colosseumData.ColorCodeId = jsonData["is_special_mode"].ToString();
			if (jsonData.TryGetValue("card_pool_name", out var value))
			{
				colosseumData.CardPool = value.ToString();
			}
			if (colosseumData.IsRoundPeriod)
			{
				colosseumData.StageNo = (ArenaColosseum.eStageNo)jsonData["now_round"].ToInt();
				colosseumData.RemainingUnixTime = ConvertTime.DateTimeToUnixTime(DateTime.Parse(jsonData["end_time"].ToString()));
				string text = ConvertTime.ToLocal(DateTime.Parse(jsonData["start_time"].ToString()));
				string text2 = ConvertTime.ToLocal(DateTime.Parse(jsonData["end_time"].ToString()));
				colosseumData.NowRoundTimeText = Data.SystemText.Get("Colosseum_0033", text, text2);
			}
			else
			{
				colosseumData.RemainingUnixTime = ConvertTime.DateTimeToUnixTime(DateTime.Parse(jsonData["start_time"].ToString()));
				colosseumData.Name = jsonData["colosseum_name"].ToString();
				colosseumData.StageNo = (ArenaColosseum.eStageNo)jsonData["next_round"].ToInt();
				string text3 = ConvertTime.ToLocal(DateTime.Parse(jsonData["start_time"].ToString()));
				colosseumData.NowRoundTimeText = Data.SystemText.Get("Colosseum_0063", text3);
			}
			colosseumData.RemainingSinceTime = Time.realtimeSinceStartup;
			colosseumData.RemainingServerUnixTime = responseData["data_headers"]["servertime"].ToDouble();
			colosseumData.NeedsFirstTips = jsonData.GetValueOrDefault("is_display_tips", 0) == 1;
			colosseumData.ColosseumId = jsonData.GetValueOrDefault("colosseum_id", 0);
			colosseumData.ChaoseTipsId = jsonData.GetValueOrDefault("tips_id", 0);
			colosseumData.CanUseNonPossessionCard = jsonData.GetValueOrDefault("is_all_card_enabled", 0) == 1;
			colosseumData.ExpirtyInfo = new ShopExpirtyInfo(jsonData["sales_period_info"]);
			if (jsonData.TryGetValue("strategy_pick_num", out var value2))
			{
				colosseumData.ChaosNum = value2.ToInt();
				Data.Master.LoadColosseumChaosBattleInfo(colosseumData.ChaosNum);
				Data.Master.SetColosseumClassInfomationOrder(colosseumData.ChaosNum);
			}
		}
	}

	public static void SetColosseumOwnStatus(JsonData status)
	{
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		if (status.Count != 0)
		{
			if (status.Keys.Contains("rest_entry_num"))
			{
				if (status["rest_entry_num"].ToInt() != 0)
				{
					colosseumData.IsRetry = true;
					colosseumData.RetryRemainingNum = status["rest_entry_num"].ToInt();
				}
				else
				{
					colosseumData.IsRetry = false;
					colosseumData.RetryRemainingNum = 0;
				}
			}
			else
			{
				colosseumData.IsRetry = false;
			}
			if (status.Keys.Contains("now_round_id"))
			{
				colosseumData.Round = (ArenaColosseum.eRound)status["now_round_id"].ToInt();
				colosseumData.ServerRoundId = status["now_round_id"].ToInt();
			}
			if (status.Keys.Contains("is_last_day"))
			{
				colosseumData.IsLastDay = status["is_last_day"].ToBoolean();
			}
			else
			{
				colosseumData.IsLastDay = false;
			}
			if (status.Keys.Contains("next_round_id"))
			{
				colosseumData.NextRound = (ArenaColosseum.eRound)status["next_round_id"].ToInt();
			}
			if (colosseumData.NextRound == ArenaColosseum.eRound.FinalNotAdvance)
			{
				if (colosseumData.IsRoundPeriod)
				{
					colosseumData.NextRound = ArenaColosseum.eRound.Undecided;
				}
				else
				{
					colosseumData.NextRound = ArenaColosseum.eRound.Lose;
				}
			}
			if (status.Keys.Contains("is_champion"))
			{
				colosseumData.IsClear = status["is_champion"].ToBoolean();
				if (colosseumData.IsClear)
				{
					colosseumData.Name = status["colosseum_name"].ToString();
				}
			}
		}
		else
		{
			colosseumData.IsRetry = false;
		}
	}
}
