using System;
using LitJson;
using UnityEngine;

namespace Wizard;

public class TreasureBoxCp
{
	public const string TREASURE_BOX_OPEN_EFFECT = "cmn_arena_treasure_{0}";

	public const string GRADE_UP_EFFECT = "cmn_treasure_gradeup_gp_{0}";

	private const string TREASURE_GET_SPRITE_NAME = "treasure_get";

	private const string TREASURE_RANK_UP_SPRITE_NAME = "treasure_rank_up";

	private const string TREASURE_RANK_MAX_SPRITE_NAME = "treasure_rank_max";

	private bool _treasureBoxCpNotExist = true;

	public static readonly float[] OPEN_TREASURE_BOX_TIME = new float[4] { 1f, 1.5f, 1.5f, 4f };

	public static readonly int[] GRADE_TO_TREASURE_BOX_ID = new int[5] { -1, 0, 2, 4, 5 };

	private double _receiveServerUnixTime;

	private double _receiveSinceTime;

	public bool IsTreasureBoxOpened => CurrentGrade == MaxGrade;

	public bool IsReceivable { get; private set; }

	public double StartUnixTime { get; private set; }

	public double EndUnixTime { get; private set; }

	public int CurrentGrade { get; private set; }

	public int NextGrade { get; private set; }

	public int MaxGrade { get; private set; }

	public int CurrentMemory { get; private set; }

	public int MaxMemory { get; private set; }

	public void Parse(JsonData data, JsonData headerData)
	{
		_treasureBoxCpNotExist = false;
		IsReceivable = false;
		if (data.Keys.Contains("is_receivable"))
		{
			IsReceivable = data["is_receivable"].ToInt() == 1;
		}
		StartUnixTime = ConvertTime.DateTimeToUnixTime(DateTime.Parse(data["start_time"].ToString()));
		EndUnixTime = ConvertTime.DateTimeToUnixTime(DateTime.Parse(data["end_time"].ToString()));
		CurrentGrade = data["current_grade"].ToInt();
		NextGrade = data["next_grade"].ToInt();
		CurrentMemory = data["current_memory"].ToInt();
		MaxGrade = data["max_grade"].ToInt();
		MaxMemory = data["max_memory"].ToInt();
		_receiveServerUnixTime = headerData["servertime"].ToDouble();
		_receiveSinceTime = Time.realtimeSinceStartup;
	}

	public bool IsWithinPeriod()
	{
		if (_treasureBoxCpNotExist)
		{
			return false;
		}
		double num = _receiveServerUnixTime + (double)Time.realtimeSinceStartup - _receiveSinceTime;
		if (num >= EndUnixTime)
		{
			return false;
		}
		return num > StartUnixTime;
	}

	public static Se.TYPE GetGradeSE(int grade)
	{
		return grade switch
		{
			1 => Se.TYPE.SE_SYS_UPGRADE_TREASURE_BOX_01, 
			2 => Se.TYPE.SE_SYS_UPGRADE_TREASURE_BOX_02, 
			3 => Se.TYPE.SE_SYS_UPGRADE_TREASURE_BOX_03, 
			4 => Se.TYPE.SE_SYS_UPGRADE_TREASURE_BOX_04, 
			_ => Se.TYPE.NONE, 
		};
	}

	public static string GetTreasureSpriteName(int grade)
	{
		switch (grade)
		{
		case 1:
			return "treasure_get";
		case 2:
		case 3:
			return "treasure_rank_up";
		case 4:
			return "treasure_rank_max";
		default:
			return "";
		}
	}
}
