using System.Collections.Generic;
using LitJson;

namespace Wizard.Lottery;

public class LotteryRewardData
{
	public class NormalGotRewardData
	{
		private string _textureName = string.Empty;

		public UserGoods.Type RewardType { get; private set; }

		public long RewardId { get; private set; }

		public int RewardGotNum { get; private set; }

		public string TextureName
		{
			get
			{
				if (_textureName == string.Empty)
				{
					_textureName = UserGoods.GetUserGoodsImageName(RewardType, RewardId);
				}
				return _textureName;
			}
		}

		public NormalGotRewardData(UserGoods.Type reward_type, long reward_id, int reward_got_num)
		{
			RewardType = reward_type;
			RewardId = reward_id;
			RewardGotNum = reward_got_num;
		}
	}

	public enum eLotteryRewardState
	{
		None,
		Unapplied,
		Applied,
		WaitResult,
		NotReceived,
		Received,
		NotAchieved
	}

	public eLotteryRewardState state;

	private JsonData NormalRewardList;

	public int MissionId { get; private set; }

	public int GradeId { get; private set; }

	public bool IsCurrent { get; private set; }

	public int LotteryId { get; private set; }

	public string LotteryUrl { get; private set; }

	public string DialogMessage { get; private set; }

	public string Description { get; private set; }

	public string PrizeMessage { get; private set; }

	public List<NormalGotRewardData> NormalGotRewardList { get; private set; }

	public LotteryMissionData MissionData { get; set; }

	public bool IsSpecial
	{
		get
		{
			if (LotteryId <= 0)
			{
				return false;
			}
			return true;
		}
	}

	public LotteryRewardData(JsonData lotteryRewardData)
	{
		MissionId = lotteryRewardData["mission_id"].ToInt();
		state = (eLotteryRewardState)lotteryRewardData["status"].ToInt();
		GradeId = lotteryRewardData["grade_id"].ToInt();
		IsCurrent = lotteryRewardData["is_current"].ToInt() == 1;
		LotteryId = lotteryRewardData["lottery_id"].ToInt();
		LotteryUrl = lotteryRewardData["lottery_url"].ToString();
		DialogMessage = lotteryRewardData["dialog_message"].ToString();
		NormalGotRewardList = ParseGotRewardList(lotteryRewardData["got_reward_list"]);
		PrizeMessage = lotteryRewardData.GetValueOrDefault("prize_message", string.Empty);
		if (lotteryRewardData.TryGetValue("reward_list", out var _))
		{
			NormalRewardList = lotteryRewardData["reward_list"];
		}
		Description = lotteryRewardData["description_message"].ToString().Replace("\\n", "\n");
	}

	public void UpdateHaveUserGoodsByNormalRwardList()
	{
		if (NormalRewardList != null)
		{
			PlayerStaticData.UpdateHaveUserGoodsNumByJsonData(NormalRewardList);
			NormalRewardList = null;
		}
	}

	private List<NormalGotRewardData> ParseGotRewardList(JsonData data)
	{
		List<NormalGotRewardData> list = new List<NormalGotRewardData>();
		for (int i = 0; i < data.Count; i++)
		{
			JsonData jsonData = data[i];
			UserGoods.Type reward_type = (UserGoods.Type)jsonData["reward_type"].ToInt();
			long reward_id = jsonData["reward_id"].ToLong();
			int reward_got_num = jsonData["reward_got_num"].ToInt();
			list.Add(new NormalGotRewardData(reward_type, reward_id, reward_got_num));
		}
		return list;
	}
}
