using System.Collections;
using Cute;
using UnityEngine;

namespace Wizard;

public class CompetitionCardPanel : MyPageCardPanel
{
	[SerializeField]
	private UILabel _infoNextTimeLabel;

	[SerializeField]
	private UILabel _infoTitleLabel;

	[SerializeField]
	private UILabel _infoTimeLabel;

	[SerializeField]
	private CompetitionEntry _competitionEntry;

	[SerializeField]
	private GameObject _competitionEntryRoot;

	[SerializeField]
	private GameObject _competitionBadge;

	[SerializeField]
	private UISprite _panelLineSprite;

	[SerializeField]
	private string _competitionCardPanelPath;

	[SerializeField]
	private string _competition2PickCardPanelPath;

	private const int SECONDS_PER_MINUTE = 60;

	private static readonly Color PanelLineColor = new Color(64f / 85f, 2f / 15f, 2f / 15f);

	private bool _firstInitialize;

	private string _cardPanelPath;

	public bool IsEntry { get; set; }

	public bool IsEntryTimeEnd { get; set; }

	private void OnEnable()
	{
		PanelImageUpdate();
	}

	public override string GetResourcePath(bool isfetch)
	{
		_cardPanelPath = ((Data.ArenaData.CompetitionData.DeckFormat == Format.TwoPick) ? _competition2PickCardPanelPath : _competitionCardPanelPath);
		return Toolbox.ResourcesManager.GetAssetTypePath(_cardPanelPath, ResourcesManager.AssetLoadPathType.CardMenu, isfetch);
	}

	public void Update()
	{
		SetPanelLineColor();
		if (Data.ArenaData.CompetitionData != null)
		{
			if (_competitionEntryRoot.gameObject.activeSelf)
			{
				_competitionBadge.SetActive(value: false);
			}
			else
			{
				_competitionBadge.SetActive(Data.MyPageNotifications.data.IsCompetitionBadge);
			}
		}
	}

	public void PanelImageUpdate(bool callEntryMenuUpdate = true)
	{
		ArenaCompetition competitionData = Data.ArenaData.CompetitionData;
		SystemText systemText = Data.SystemText;
		if (!_firstInitialize)
		{
			_firstInitialize = true;
			IsEntryTimeEnd = competitionData.IsEntryTimeEnd;
		}
		base.TitleLabel.text = competitionData.CompetitionName;
		string text = null;
		double num = 0.0;
		double num2 = competitionData.RemainingServerUnixTime + (double)Time.realtimeSinceStartup - (double)competitionData.RemainingSinceTime;
		bool flag = Data.ArenaData.CompetitionData.RestEntryCount == 0 && Data.ArenaData.CompetitionData.IsRewardReceived;
		bool flag2 = !IsEntry && !IsEntryTimeEnd && !flag;
		if (flag2)
		{
			if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithFree)
			{
				_infoTitleLabel.text = systemText.Get("Competition_0064");
			}
			else
			{
				_infoTitleLabel.text = systemText.Get("Competition_0009");
			}
			num = competitionData.EntryRemainingUnixTime - num2;
			_infoNextTimeLabel.text = systemText.Get("Competition_0070");
		}
		else
		{
			if (IsEntry && Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithFree)
			{
				if (competitionData.FreebieStatus == ArenaCompetition.FreebieStatusType.InFreeBattle)
				{
					_infoTitleLabel.text = systemText.Get("Competition_0071");
					_infoNextTimeLabel.text = systemText.Get("Competition_0070");
				}
				else if (competitionData.FreebieStatus == ArenaCompetition.FreebieStatusType.CanPermanentEntry)
				{
					_infoTitleLabel.text = systemText.Get("Competition_0009");
					_infoNextTimeLabel.text = systemText.Get("Competition_0048");
				}
				else
				{
					_infoTitleLabel.text = systemText.Get("Competition_0010");
					_infoNextTimeLabel.text = systemText.Get("Competition_0049");
				}
			}
			else
			{
				_infoTitleLabel.text = systemText.Get("Competition_0010");
				_infoNextTimeLabel.text = systemText.Get("Competition_0049");
			}
			num = competitionData.RemainingUnixTime - num2;
		}
		if (!(num <= 0.0))
		{
			text = ((num < 3600.0) ? systemText.Get("Colosseum_0058", ((int)(num / 60.0) + 1).ToString()) : ((!(num < 86400.0)) ? systemText.Get("Colosseum_0044", ((int)(num / 86400.0)).ToString()) : systemText.Get("Colosseum_0057", ((int)(num / 3600.0)).ToString())));
		}
		else
		{
			text = systemText.Get("Colosseum_0058", "0");
			if (flag2)
			{
				IsEntryTimeEnd = true;
				PanelImageUpdate();
				return;
			}
		}
		float seconds = (float)(60.0 - num2 % 60.0);
		StartCoroutine(PanelImageUpdateTimer(seconds));
		_infoTimeLabel.text = text;
		if (callEntryMenuUpdate)
		{
			_competitionEntry.EntryMenuUpdate(IsEntryTimeEnd);
		}
	}

	private IEnumerator PanelImageUpdateTimer(float seconds)
	{
		yield return new WaitForSeconds(seconds);
		PanelImageUpdate();
	}

	private void OnApplicationPause(bool pause)
	{
		if (!pause)
		{
			PanelImageUpdate();
		}
	}

	private void SetPanelLineColor()
	{
		if (_panelLineSprite.color == Color.white)
		{
			_panelLineSprite.color = PanelLineColor;
		}
	}
}
