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

namespace Wizard.Bingo;

public class BingoInfoTask : BaseTask
{
	public struct SquareData
	{
		public int SquareId;

		public int DisplayId;

		public bool IsOpen;

		public int TreasureBoxGradeId;

		public SquareData(JsonData squareJsonData)
		{
			SquareId = squareJsonData["square_id"].ToInt();
			DisplayId = squareJsonData["display_id"].ToInt();
			IsOpen = squareJsonData["is_open"].ToInt() == 1;
			TreasureBoxGradeId = squareJsonData["treasure_box_grade_id"].ToInt();
		}
	}

	public struct BingoMissionData
	{
		public string MissionTitle { get; private set; }

		public int MissionCurrent { get; private set; }

		public int MissionMax { get; private set; }

		public bool IsCleared { get; private set; }

		public ReceivedReward Reward { get; private set; }

		public float MissionRatio
		{
			get
			{
				if (MissionMax == 0)
				{
					return 0f;
				}
				return (float)MissionCurrent / (float)MissionMax;
			}
		}

		public BingoMissionData(JsonData json)
		{
			MissionTitle = json["mission_name"].ToString();
			MissionCurrent = json["total_count"].ToInt();
			MissionMax = json["require_number"].ToInt();
			IsCleared = json["is_achieved"].ToInt() == 1;
			ReceivedReward reward = ReceivedReward.CreateFromBingoMissionReward(json["reward_list"][0]);
			Reward = reward;
		}
	}

	public struct CurrentLineRewardInfo
	{
		public string LineNum { get; private set; }

		public bool IsCleared { get; private set; }

		public ReceivedReward Reward { get; private set; }

		public CurrentLineRewardInfo(JsonData json)
		{
			LineNum = json["line_num"].ToString();
			IsCleared = json["is_achieved"].ToInt() == 1;
			ReceivedReward reward = ReceivedReward.CreateFromBingoMissionReward(json);
			Reward = reward;
		}
	}

	public class BingoInfoData
	{
		public int CampaignId;

		public int SheetNum { get; private set; }

		public int BingoTicketNum { get; private set; }

		public int MaxSquareNum { get; private set; }

		public int MaxSheetNum { get; private set; }

		public int BingoDrawCost { get; private set; }

		public List<SquareData> SquareDataList { get; private set; }

		public Dictionary<int, List<ReceivedReward>> AllLineRewardsListDic { get; private set; }

		public DateTime StartTime { get; private set; }

		public DateTime EndTime { get; private set; }

		public string NotificationId { get; private set; }

		public List<BingoMissionData> MissionList { get; private set; }

		public List<CurrentLineRewardInfo> CurrentLineRewardList { get; private set; }

		public DateTime MissionEndTime { get; private set; }

		public bool IsMissionActivePeriod { get; private set; }

		public BingoInfoData(int campaignId, int sheetNum, int bingoTicketNum, int maxSquareNum, int maxSheetNum, int bingoDrawCost, DateTime startTime, DateTime endTime, List<SquareData> squareDataList, Dictionary<int, List<ReceivedReward>> allLineRewardsListDic, string notificationId, List<BingoMissionData> missionList, List<CurrentLineRewardInfo> currentLineRewardList, DateTime missionEndTime, bool isMissionActivePeriod)
		{
			CampaignId = campaignId;
			SheetNum = sheetNum;
			BingoTicketNum = bingoTicketNum;
			MaxSquareNum = maxSquareNum;
			MaxSheetNum = maxSheetNum;
			BingoDrawCost = bingoDrawCost;
			StartTime = startTime;
			EndTime = endTime;
			SquareDataList = squareDataList;
			AllLineRewardsListDic = allLineRewardsListDic;
			NotificationId = notificationId;
			MissionList = missionList;
			CurrentLineRewardList = currentLineRewardList;
			MissionEndTime = missionEndTime;
			IsMissionActivePeriod = isMissionActivePeriod;
		}
	}

	public class BingoInfoTaskParam : BaseParam
	{
		public bool is_display_update;
	}

	public BingoInfoData Result { get; private set; }

	public bool CanGiveDailyLoginBonus { get; private set; }

	public BingoDrawTask.BingoDrawTaskData BingoLoginDrawResult { get; private set; }

	public BingoInfoTask()
	{
		base.type = ApiType.Type.BingoInfo;
	}

	public void SetParameter(bool isDisplayUpdate)
	{
		BingoInfoTaskParam bingoInfoTaskParam = new BingoInfoTaskParam();
		bingoInfoTaskParam.is_display_update = isDisplayUpdate;
		base.Params = bingoInfoTaskParam;
	}

	protected override int Parse()
	{
		int num = base.Parse();
		if (num != 1)
		{
			return num;
		}
		JsonData jsonData = base.ResponseData["data"];
		CanGiveDailyLoginBonus = jsonData.GetValueOrDefault("can_give_login_bonus", defaultValue: false);
		if (CanGiveDailyLoginBonus)
		{
			return num;
		}
		int campaignId = jsonData["campaign_id"].ToInt();
		int sheetNum = jsonData["sheet_num"].ToInt();
		int bingoTicketNum = jsonData["bingo_ticket_num"].ToInt();
		int maxSquareNum = jsonData["max_square_num"].ToInt();
		int maxSheetNum = jsonData["max_sheet_num"].ToInt();
		int bingoDrawCost = jsonData["bingo_draw_cost"].ToInt();
		string notificationId = jsonData["notice_text_id"].ToString();
		DateTime startTime = DateTime.Parse(jsonData["start_time"].ToString());
		DateTime endTime = DateTime.Parse(jsonData["end_time"].ToString());
		DateTime missionEndTime = DateTime.Parse(jsonData["mission_end_time"].ToString());
		bool isMissionActivePeriod = jsonData["is_mission_active_period"].ToBoolean();
		JsonData jsonData2 = jsonData["square_list"];
		List<SquareData> list = new List<SquareData>();
		for (int i = 1; i < jsonData2.Count; i++)
		{
			SquareData item = new SquareData(jsonData2[i]);
			list.Add(item);
		}
		JsonData jsonData3 = jsonData["all_line_reward_list"];
		Dictionary<int, List<ReceivedReward>> dictionary = new Dictionary<int, List<ReceivedReward>>();
		for (int j = 0; j < jsonData3.Count; j++)
		{
			for (int k = 0; k < jsonData3[j].Count; k++)
			{
				ReceivedReward item2 = ReceivedReward.CreateFromBingoLineReward(jsonData3[j][k]);
				if (dictionary.ContainsKey(j))
				{
					dictionary[j].Add(item2);
					continue;
				}
				dictionary[j] = new List<ReceivedReward>();
				dictionary[j].Add(item2);
			}
		}
		JsonData jsonData4 = jsonData["mission_list"];
		List<BingoMissionData> list2 = new List<BingoMissionData>();
		for (int l = 0; l < jsonData4.Count; l++)
		{
			BingoMissionData item3 = new BingoMissionData(jsonData4[l]);
			list2.Add(item3);
		}
		JsonData jsonData5 = jsonData["current_line_reward_list"];
		List<CurrentLineRewardInfo> list3 = new List<CurrentLineRewardInfo>();
		for (int m = 0; m < jsonData5.Count; m++)
		{
			CurrentLineRewardInfo item4 = new CurrentLineRewardInfo(jsonData5[m]);
			list3.Add(item4);
		}
		if (jsonData.TryGetValue("login_bonus", out var value))
		{
			BingoLoginDrawResult = BingoDrawTask.ParseBingoDrawResult(value, isBingoLoginBonus: true);
			PlayerStaticData.UpdateHaveUserGoodsNumByJsonData(value["reward_list"]);
		}
		Result = new BingoInfoData(campaignId, sheetNum, bingoTicketNum, maxSquareNum, maxSheetNum, bingoDrawCost, startTime, endTime, list, dictionary, notificationId, list2, list3, missionEndTime, isMissionActivePeriod);
		return num;
	}
}
