using System;
using LitJson;
using UnityEngine;

namespace Wizard.Lottery;

public class LotteryLoadTaskData
{
	private bool IsEnable { get; set; }

	private DateTime StartTime { get; set; }

	private DateTime EndTime { get; set; }

	private DateTime CloseTime { get; set; }

	private int ReceiveServerTime { get; set; }

	private double ReceiveSinceStartUpTime { get; set; }

	public bool IsExistReward { get; private set; }

	public static LotteryLoadTaskData Parse(JsonData json)
	{
		LotteryLoadTaskData lotteryLoadTaskData = new LotteryLoadTaskData();
		if (json["data"].Keys.Contains("lottery_period_info"))
		{
			JsonData jsonData = json["data"]["lottery_period_info"];
			if (jsonData != null)
			{
				DateTime startTime = DateTime.Parse(jsonData["start_time"].ToString());
				DateTime endTime = DateTime.Parse(jsonData["end_time"].ToString());
				lotteryLoadTaskData.IsEnable = true;
				lotteryLoadTaskData.StartTime = startTime;
				lotteryLoadTaskData.EndTime = endTime;
				lotteryLoadTaskData.CloseTime = DateTime.Parse(jsonData["close_time"].ToString());
				lotteryLoadTaskData.ReceiveServerTime = json["data_headers"]["servertime"].ToInt();
				lotteryLoadTaskData.ReceiveSinceStartUpTime = Time.realtimeSinceStartup;
				lotteryLoadTaskData.IsExistReward = jsonData["has_reward"].ToBoolean();
			}
			else
			{
				lotteryLoadTaskData.IsEnable = false;
			}
		}
		else
		{
			lotteryLoadTaskData.IsEnable = false;
		}
		return lotteryLoadTaskData;
	}

	public bool IsCampaignTimeNow()
	{
		return IsTimeEnablePeriod(StartTime, EndTime);
	}

	public bool IsReceiveTimeNow()
	{
		return IsTimeEnablePeriod(StartTime, CloseTime);
	}

	private bool IsTimeEnablePeriod(DateTime start, DateTime end)
	{
		if (!IsEnable)
		{
			return false;
		}
		double num = ConvertTime.DateTimeToUnixTime(start);
		double num2 = ConvertTime.DateTimeToUnixTime(end);
		double num3 = (double)((float)ReceiveServerTime + Time.realtimeSinceStartup) - ReceiveSinceStartUpTime;
		if (num3 < num || num3 >= num2)
		{
			return false;
		}
		return true;
	}
}
