using System;
using System.Collections.Generic;
using System.Linq;
using LitJson;

namespace Wizard;

public class PackInfoTask : BaseTask
{
	public class PackFirstTransition
	{
		private int _serialId;

		public int PackId { get; private set; }

		public PackFirstTransition(int serialId, int packId)
		{
			_serialId = serialId;
			PackId = packId;
		}

		public bool IsUpdate()
		{
			int value = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.PACK_FIRST_TRANSITION_SERIAL_ID);
			return _serialId != value;
		}

		public void SaveViewed()
		{
			PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.PACK_FIRST_TRANSITION_SERIAL_ID, _serialId);
		}
	}

	public class PackInfoTaskParam : BaseParam
	{
	}

	public bool isNewCardPack;

	public bool NeedsFooterBadgeIcon;

	public PackFirstTransition PackFirstTransitionData { get; private set; }

	public List<NotificatonAnimation.Param> NotificatonAnimationParams { get; private set; }

	public AchievedInfo AchievedInfo { get; private set; }

	public PackInfoTask(ApiType.Type apiType)
	{
		base.type = apiType;
	}

	public void SetParameter()
	{
		PackInfoTaskParam packInfoTaskParam = new PackInfoTaskParam();
		base.Params = packInfoTaskParam;
	}

	protected override int Parse()
	{
		int num = base.Parse();
		if (num != 1)
		{
			return num;
		}
		Data.PackInfo.dataList.Clear();
		JsonData jsonData = base.ResponseData["data"]["pack_config_list"];
		for (int i = 0; i < jsonData.Count; i++)
		{
			JsonData jsonData2 = jsonData[i];
			PackConfig packConfig = new PackConfig();
			packConfig.PackId = jsonData2["parent_gacha_id"].ToInt();
			packConfig.BasePackId = jsonData2["base_pack_id"].ToInt();
			packConfig.GachaType = jsonData2["gacha_type"].ToInt();
			packConfig.ExpirtyInfo = new ShopExpirtyInfo(jsonData2["sales_period_info"]);
			if (jsonData2["gacha_detail"] != null)
			{
				string text = jsonData2["gacha_detail"].ToString();
				packConfig.Description = text.Replace("\\n", "\n");
			}
			packConfig.Category = (PackCategory)Enum.ToObject(typeof(PackCategory), jsonData2["pack_category"].ToInt());
			packConfig.RemainTime = new RemainTime(jsonData2["complete_date"].ToString(), base.ResponseData["data_headers"]["servertime"].ToDouble());
			packConfig.IsHideNotEnoughBuy = jsonData2["is_hide"].ToBoolean();
			packConfig.OpenCount = jsonData2["open_count"].ToInt();
			packConfig.OpenCountLimit = jsonData2["open_count_limit"].ToInt();
			if (jsonData2.TryGetValue("selected_class_id", out var value))
			{
				packConfig.SelectedClassId = value.ToInt();
			}
			if (jsonData2.TryGetValue("set_number", out var value2))
			{
				packConfig.RemainStepupOpenCount = value2.ToInt();
			}
			if (jsonData2.TryGetValue("total_set_number", out var value3))
			{
				packConfig.TotalStepupOpenCount = value3.ToInt();
			}
			int num2 = ((packConfig.IsSpecialCardPack && packConfig.PackId >= 90001 && !GachaUI.IsTsStepupPackId(packConfig.PackId)) ? 90002 : packConfig.PackId);
			packConfig.Title = Data.Master.CardSetNameMgr.Get(num2.ToString()).LongName;
			if (jsonData2.Keys.Contains("sleeve_id"))
			{
				packConfig.SleeveId = jsonData2["sleeve_id"].ToInt();
			}
			else
			{
				packConfig.SleeveId = 3000011;
			}
			packConfig.SpecialSleeveId = (jsonData2.Keys.Contains("special_sleeve_id") ? jsonData2["special_sleeve_id"].ToInt() : 3000011);
			if (jsonData2["override_draw_effect_pack_id"] != null)
			{
				packConfig.OverrideDrawEffectPackId = jsonData2["override_draw_effect_pack_id"].ToInt();
			}
			if (jsonData2["override_ui_effect_pack_id"] != null)
			{
				packConfig.OverrideUIEffectPackId = jsonData2["override_ui_effect_pack_id"].ToInt();
			}
			packConfig.TsExchangePosterId = jsonData2.GetValueOrDefault("poster_type", 0);
			JsonData jsonData3 = jsonData2["cardpack_banner_list"];
			for (int j = 0; j < jsonData3.Count; j++)
			{
				PackBannerData packBannerData = new PackBannerData();
				packBannerData.BannerFileName = jsonData3[j]["banner_name"].ToString();
				packBannerData.BannerTitle = Data.SystemText.Get(jsonData3[j]["dialog_title"].ToString());
				packConfig.ListPackBanner.Add(packBannerData);
			}
			if (jsonData2["gacha_point"] != null)
			{
				packConfig.GachaPointData = new GachaPointData(jsonData2["gacha_point"]);
			}
			packConfig.IsNew = jsonData2["is_new"].ToBoolean();
			JsonData jsonData4 = jsonData2["child_gacha_info"];
			for (int k = 0; k < jsonData4.Count; k++)
			{
				JsonData jsonData5 = jsonData4[k];
				PackChildGachaInfo packChildGachaInfo = new PackChildGachaInfo();
				packChildGachaInfo.GachaId = jsonData5["gacha_id"].ToInt();
				packChildGachaInfo.PackType = (GachaUI.CardPackType)jsonData5["type_detail"].ToInt();
				packChildGachaInfo.Cost = jsonData5["cost"].ToInt();
				if (jsonData5.Keys.Contains("item_id"))
				{
					packChildGachaInfo.UserGoodsId = jsonData5["item_id"].ToLong();
				}
				if (jsonData5.Keys.Contains("is_daily_single"))
				{
					packChildGachaInfo.IsDailySingle = jsonData5["is_daily_single"].ToBoolean();
				}
				else
				{
					packChildGachaInfo.IsDailySingle = false;
				}
				packChildGachaInfo.PackCountBuyPer = jsonData5.GetValueOrDefault("daily_free_gacha_count", 0);
				packChildGachaInfo.CampaignName = jsonData5.GetValueOrDefault("campaign_name", "");
				packChildGachaInfo.AvailableCount = jsonData5.GetValueOrDefault("purchase_limit_count", 0);
				packChildGachaInfo.OverrideIncreaseGachaPoint = jsonData5.GetValueOrDefault("override_increase_gacha_point", 0);
				if (jsonData5.Keys.Contains("free_gacha_campaign_id"))
				{
					packChildGachaInfo.FreeGachaCampaignId = jsonData5["free_gacha_campaign_id"].ToInt();
				}
				packConfig.ChildGachaInfoList.Add(packChildGachaInfo);
			}
			packConfig.IsPrerelease = jsonData2["is_pre_release"].ToBoolean();
			if (packConfig.IsPrerelease)
			{
				packConfig.PrereleasePurchaseInfo = new PrereleasePurchaseInfo(jsonData2["pre_release_gacha_info"], base.ResponseData["data_headers"]["servertime"].ToDouble());
			}
			packConfig.SetSpecialPackRewardsList(jsonData2);
			Data.PackInfo.dataList.Add(packConfig);
		}
		if (base.ResponseData["data"].Keys.Contains("pack_first_transition_data"))
		{
			JsonData jsonData6 = base.ResponseData["data"]["pack_first_transition_data"];
			int serialId = jsonData6["serial_id"].ToInt();
			int packId = jsonData6["pack_id"].ToInt();
			PackFirstTransitionData = new PackFirstTransition(serialId, packId);
		}
		NeedsFooterBadgeIcon = Data.PackInfo.dataList.Any((PackConfig config) => config.ChildGachaInfoList.Any((PackChildGachaInfo child) => child.AvailableCount > 0));
		return num;
	}
}
