using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;

namespace Wizard;

public class CompetitionEntry : ArenaEntryBase
{
	private const int CARD_LIST_SORT_ORDER = 100;

	private const int CARD_DETAIL_SORT_ORDER = 110;

	[SerializeField]
	private UIButton _buttonFreeBattle;

	[SerializeField]
	private GameObject _detailPrefab;

	[SerializeField]
	private UIButton _detailButton;

	[SerializeField]
	private GameObject _costRoot;

	[SerializeField]
	private UILabel _periodTimeTitleLabel;

	[SerializeField]
	private UILabel _periodTimeLabel;

	[SerializeField]
	private UILabel _ownBeforeStatusLabel;

	[SerializeField]
	private UILabel _ownAfterStatusLabel;

	[SerializeField]
	private UILabel _formatLabel;

	[SerializeField]
	private UILabel _retryNumberLabel;

	[SerializeField]
	public GameObject CompetitionEntryCompleteDialog;

	[SerializeField]
	private GameObject _beforeEntryHeadLine;

	[SerializeField]
	private GameObject _afterEntryHeadLine;

	[SerializeField]
	private GameObject _competitionBadge;

	[SerializeField]
	private GameObject _freeEntryBadge;

	public bool _isEntryTimeEnd;

	public bool IsEnableUpdate = true;

	public Action EntryAction;

	private ArenaCompetition.EntryStatusType _entryStatus;

	private int _restChallengeCount;

	private int _restEntryCount;

	private const float GLAY_SCALE = 0.33f;

	private Color baseStatusTextColor;

	private Color grayStatusTextColor;

	private const float NO_FREE_BATTLE_STATUS_LABEL_POSITION_X = 3f;

	private const float NO_FREE_BATTLE_STATUS_LABEL_POSITION_Y = 47f;

	private const int NO_FREE_BATTLE_TEXT_FONT_SIZE = 22;

	private const int LONG_TEXT_FONT_SIZE = 21;

	private bool _isEntry => _entryStatus != ArenaCompetition.EntryStatusType.NotEntry;

	private bool IsCompetitionComplete
	{
		get
		{
			if (_restEntryCount == 0 && Data.ArenaData.CompetitionData.IsRewardReceived)
			{
				return _entryStatus == ArenaCompetition.EntryStatusType.NotEntry;
			}
			return false;
		}
	}

	private bool IsEnableEntry
	{
		get
		{
			if (!_isEntry && !_isEntryTimeEnd)
			{
				return !IsCompetitionComplete;
			}
			return false;
		}
	}

	private void Awake()
	{
		_isCompetition = true;
		IsEnableUpdate = true;
		baseStatusTextColor = _ownAfterStatusLabel.color;
		grayStatusTextColor = new Color(baseStatusTextColor.r * 0.33f, baseStatusTextColor.g * 0.33f, baseStatusTextColor.b * 0.33f, 255f);
		EntryBaseInit(_costRoot);
	}

	protected override void EntryBaseInit(GameObject costRootObject)
	{
		base.EntryBaseInit(costRootObject);
		_entryDialogTitleText = Data.SystemText.Get("Competition_0017");
		_resumeFunc = Resume;
		_isJoinFunc = IsJoin;
		_freeBattleFunc = FreeBattle;
		_isFreeBattleCompetition = IsFreeBattleCompetition;
		_detailButton.onClick.Clear();
		_detailButton.onClick.Add(new EventDelegate(OnClickDetailButton));
	}

	protected override void EntryDialogCreate(GameObject inDialogObject)
	{
		CompetitionEntryDialog component = inDialogObject.GetComponent<CompetitionEntryDialog>();
		component.SetRewardInfoEntryConfirm();
		component.CompleteEntryHandler = SetMyPageCompetitionReload;
		component.CompleteJoinHandler = AdmissionCompetitionNotFree;
	}

	private void EntryAndAdmissionCompetition()
	{
		CompetitionEntryTask competitionEntryTask = new CompetitionEntryTask();
		competitionEntryTask.SetParameter(ArenaData.eARENA_PAY.Crystal);
		StartCoroutine(Toolbox.NetworkManager.Connect(competitionEntryTask, delegate
		{
			CompetitionUpdateEntryResumeButton();
			AdmissionCompetition(isFreeBattleJoin: true);
		}));
	}

	private void Resume()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
		if (Data.ArenaData.CompetitionData.DeckFormat == Format.TwoPick)
		{
			if (_entryStatus == ArenaCompetition.EntryStatusType.NotChallenge)
			{
				AdmissionCompetition(isFreeBattleJoin: false);
			}
			else if (Data.ArenaData.CompetitionData.IsCompletedTwoPickDeck < 1)
			{
				UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Competition2Pick);
			}
			else
			{
				UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.CompetitionLobby);
			}
			return;
		}
		switch (_entryStatus)
		{
		case ArenaCompetition.EntryStatusType.NotChallenge:
			AdmissionCompetition(isFreeBattleJoin: false);
			break;
		case ArenaCompetition.EntryStatusType.NotRegistDeck:
			DeckListOpen();
			break;
		case ArenaCompetition.EntryStatusType.InBattle:
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.CompetitionLobby);
			break;
		}
	}

	private void FreeBattle()
	{
		EntryAndAdmissionCompetition();
	}

	private bool IsJoin()
	{
		return _entryStatus != ArenaCompetition.EntryStatusType.NotEntry;
	}

	private bool IsBeforePermanentEntry()
	{
		if (Data.ArenaData.CompetitionData.FreebieStatus < ArenaCompetition.FreebieStatusType.PermanentEntryDone)
		{
			return !IsCompetitionComplete;
		}
		return false;
	}

	protected override IEnumerator _InitCoroutine()
	{
		_buttonFreeBattle.onClick.Clear();
		_buttonFreeBattle.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
			_freeBattleFunc();
		}));
		return base._InitCoroutine();
	}

	protected override void UpdateEntryResumeButton()
	{
		base.UpdateEntryResumeButton();
		_buttonFreeBattle.gameObject.SetActive(value: false);
		if (_isCompetition && _isFreeBattleCompetition != null && _isFreeBattleCompetition())
		{
			ButtonEntry.gameObject.SetActive(value: false);
			ButtonResume.gameObject.SetActive(value: false);
			_buttonFreeBattle.gameObject.SetActive(value: true);
		}
	}

	private bool IsFreeBattleCompetition()
	{
		if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithCost)
		{
			return false;
		}
		double num = Data.ArenaData.CompetitionData.RemainingServerUnixTime + (double)Time.realtimeSinceStartup - (double)Data.ArenaData.CompetitionData.RemainingSinceTime;
		bool flag = Data.ArenaData.CompetitionData.EntryRemainingUnixTime - num < 0.0;
		if (Data.ArenaData.CompetitionData.FreebieStatus == ArenaCompetition.FreebieStatusType.InFreeBattle && _entryStatus == ArenaCompetition.EntryStatusType.NotEntry && _restEntryCount > 0 && !flag)
		{
			return !IsCompetitionComplete;
		}
		return false;
	}

	public void EntryMenuUpdate(bool isEntryTimeEnd)
	{
		_isEntryTimeEnd = isEntryTimeEnd;
		Initialize();
	}

	private void Initialize()
	{
		ArenaCompetition competitionData = Data.ArenaData.CompetitionData;
		SystemText systemText = Data.SystemText;
		_isFreeEntry = false;
		if (IsEnableUpdate)
		{
			IsEnableUpdate = false;
			_entryStatus = competitionData.EntryStatus;
			_restChallengeCount = competitionData.RestChallangeCount;
			_restEntryCount = competitionData.RestEntryCount;
		}
		bool flag = competitionData.CompetitionId <= PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.COMPETITION_JOIN_BUTTON_LATEST_ID);
		Data.MyPageNotifications.data.IsCompetitionBadge = !competitionData.IsRewardReceived && _entryStatus == ArenaCompetition.EntryStatusType.NotEntry && !_isEntryTimeEnd && !flag;
		UIManager.GetInstance()._Footer.UpdateArenaBadgeIcon();
		UpdateMenu();
		if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithCost)
		{
			_costRoot.SetActive(value: true);
			_ownBeforeStatusLabel.transform.localPosition = new Vector3(3f, 47f, 0f);
		}
		UpdateCompetitionEntryBadge();
		_freeEntryBadge.SetActive(Data.MyPageNotifications.data.IsCompetitionBadge);
		ButtonEntry.GetComponentInChildren<UILabel>().text = systemText.Get("Competition_0012");
		UILabel componentInChildren = ButtonResume.GetComponentInChildren<UILabel>();
		bool flag2 = _entryStatus == ArenaCompetition.EntryStatusType.NotRegistDeck || _entryStatus == ArenaCompetition.EntryStatusType.InBattle;
		componentInChildren.text = (flag2 ? systemText.Get("Arena_0024") : systemText.Get("Arena_0034"));
		_buttonFreeBattle.GetComponentInChildren<UILabel>().text = systemText.Get("Competition_0065");
		string text = "";
		text = competitionData.DeckFormat switch
		{
			Format.TwoPick => systemText.Get("Arena_0002"), 
			Format.Rotation => systemText.Get("Common_0154"), 
			_ => systemText.Get("Common_0155"), 
		};
		_formatLabel.text = systemText.Get("Colosseum_0054", text) + systemText.Get("Colosseum_0093");
		_beforeEntryHeadLine.SetActive(IsEnableEntry);
		_afterEntryHeadLine.SetActive(!IsEnableEntry);
		bool flag3 = true;
		bool flag4 = true;
		bool flag5 = false;
		double num = Data.ArenaData.CompetitionData.RemainingServerUnixTime + (double)Time.realtimeSinceStartup - (double)Data.ArenaData.CompetitionData.RemainingSinceTime;
		bool flag6 = Data.ArenaData.CompetitionData.EntryRemainingUnixTime - num < 0.0;
		bool flag7 = IsJoin() || (flag6 && competitionData.FreebieChallengeCount > 0 && !competitionData.IsEntry);
		_periodTimeTitleLabel.text = (flag7 ? systemText.Get("Competition_0029") : systemText.Get("Competition_0028"));
		_periodTimeLabel.text = (flag7 ? competitionData.NowRoundTimeText : competitionData.EntryTimeText);
		if (IsEnableEntry)
		{
			if (IsFreeBattleCompetition())
			{
				_ownBeforeStatusLabel.text = systemText.Get("Competition_0063", competitionData.MaxFreebieChallengeCount.ToString());
				_ownBeforeStatusLabel.fontSize = 21;
				_retryNumberLabel.text = systemText.Get("Competition_0024");
				flag3 = true;
			}
			else
			{
				_ownBeforeStatusLabel.text = systemText.Get("Competition_0073", Data.ArenaData.CompetitionData.RestEntryCount.ToString());
				_ownBeforeStatusLabel.fontSize = 22;
				flag3 = true;
			}
		}
		else
		{
			string freeEntryText = string.Empty;
			if (!_isEntry && _isEntryTimeEnd)
			{
				freeEntryText = systemText.Get("Competition_0025");
				flag3 = false;
				flag5 = true;
			}
			else if (competitionData.IsChampion)
			{
				freeEntryText = systemText.Get("Colosseum_0038", competitionData.CompetitionName);
				flag3 = _entryStatus != ArenaCompetition.EntryStatusType.NotEntry;
				flag5 = true;
			}
			else if (_restChallengeCount == 0)
			{
				freeEntryText = (IsCompetitionComplete ? systemText.Get("Competition_0026") : systemText.Get("Colosseum_0051"));
				flag3 = _entryStatus != ArenaCompetition.EntryStatusType.NotEntry;
				flag4 = IsCompetitionComplete;
				flag5 = true;
			}
			else if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithFree)
			{
				flag5 = TryGetFreeEntryStatusText(out freeEntryText);
				flag4 = !flag2;
			}
			if (!flag5)
			{
				freeEntryText = systemText.Get("Colosseum_0050", _restChallengeCount.ToString());
				flag4 = !flag2;
			}
			_ownAfterStatusLabel.text = freeEntryText;
		}
		_ownAfterStatusLabel.color = (flag4 ? baseStatusTextColor : grayStatusTextColor);
		UIManager.SetObjectToGrey(ButtonEntry.gameObject, !flag3);
		UIManager.SetObjectToGrey(ButtonResume.gameObject, !flag3);
	}

	protected override void UpdateCompetitionEntryBadge()
	{
		base.UpdateCompetitionEntryBadge();
		_competitionBadge.SetActive(Data.MyPageNotifications.data.IsCompetitionBadge);
	}

	public void AdmissionCompetition(bool isFreeBattleJoin)
	{
		CompetitionJoinTask task = new CompetitionJoinTask();
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			Data.MyPageNotifications.data.IsCompetitionBadge = false;
			PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.COMPETITION_JOIN_BUTTON_LATEST_ID, Data.ArenaData.CompetitionData.CompetitionId);
			_entryStatus = ArenaCompetition.EntryStatusType.NotRegistDeck;
			if (!isFreeBattleJoin)
			{
				_restChallengeCount--;
			}
			Initialize();
			if (Data.ArenaData.CompetitionData.DeckFormat == Format.TwoPick)
			{
				UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Competition2Pick);
			}
			else
			{
				DeckListOpen();
			}
		}));
	}

	public void DeckListOpen()
	{
		UpdateEntryResumeButton();
		GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.CompetitionNormal;
		DeckInfoTask task = new DeckInfoTask();
		task.SetParameter(Data.ArenaData.CompetitionData.DeckFormat);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			DeckSelectUIDialog.Create(Data.SystemText.Get("Competition_0006"), task.DeckGroupListData, Data.ArenaData.CompetitionData.DeckFormat, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: true, CreateDeckSelectConfirmDialog);
		}));
	}

	private void CreateDeckSelectConfirmDialog(DialogBase dialogDeckList, DeckData deck)
	{
		if (!deck.IsUsable())
		{
			InCompleteDeckDecideDialog.Create(dialogDeckList, deck);
			return;
		}
		CompleteDeckDecideDialog completeDeckDecideDialog = CompleteDeckDecideDialog.CreateForSingleDeck(dialogDeckList, deck, showSimpleStageOption: true, delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			DeckSetAndMoveCompetition(deck);
		});
		completeDeckDecideDialog.DecisionUI.CardDetailCustomize = delegate(CardDetailUI detailUI)
		{
			detailUI.IsShowFlavorTextButton = false;
			detailUI.IsShowVoiceButton = false;
			detailUI.IsShowEvolutionButton = false;
			detailUI.SetSortOrder(110);
		};
		completeDeckDecideDialog.DecisionUI.CardListCustomize = delegate(UICardList uiCardList)
		{
			uiCardList.SetPanelSortOrder(100);
		};
		completeDeckDecideDialog.DecisionUI.gameObject.GetComponent<UIWidget>().alpha = 0f;
		DeckDecisionCompetition deckDecisionCompetition = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject<DeckDecisionCompetition>("UI/DeckList/DeckDecisionCompetition", isServerResources: false));
		completeDeckDecideDialog.Dialog.SetObj(deckDecisionCompetition.gameObject);
		deckDecisionCompetition.Init(deck);
		completeDeckDecideDialog.DecisionUI.transform.SetParent(deckDecisionCompetition.transform.parent.transform);
	}

	private void DeckSetAndMoveCompetition(DeckData inDeckData)
	{
		Data.ArenaData.CompetitionData.DeckList.Clear();
		Data.ArenaData.CompetitionData.DeckList.Add(inDeckData);
		bool isPublished = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.COMPETITION_PUBLISHED_SETTING);
		CompetitionRegisterDeckTask competitionRegisterDeckTask = new CompetitionRegisterDeckTask();
		competitionRegisterDeckTask.SetParameter(new List<DeckData> { inDeckData }, isPublished);
		StartCoroutine(Toolbox.NetworkManager.Connect(competitionRegisterDeckTask, delegate
		{
			_entryStatus = ArenaCompetition.EntryStatusType.InBattle;
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.CompetitionLobby);
		}));
	}

	public void CompetitionUpdateEntryResumeButton()
	{
		_restEntryCount = Data.ArenaData.CompetitionData.RestEntryCount;
		_restChallengeCount = Data.ArenaData.CompetitionData.RestChallangeCount;
		_entryStatus = ArenaCompetition.EntryStatusType.NotChallenge;
		EntryAction();
	}

	public void OnClickDetailButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		GameObject gameObject = UnityEngine.Object.Instantiate(_detailPrefab);
		dialogBase.SetObj(gameObject);
		gameObject.GetComponent<CompetitionDetail>().Init(dialogBase, Data.ArenaData.CompetitionData, _isEntry);
	}

	private bool TryGetFreeEntryStatusText(out string freeEntryText)
	{
		ArenaCompetition competitionData = Data.ArenaData.CompetitionData;
		SystemText systemText = Data.SystemText;
		freeEntryText = string.Empty;
		bool result = true;
		if (competitionData.FreebieStatus == ArenaCompetition.FreebieStatusType.InFreeBattle)
		{
			freeEntryText = systemText.Get("Competition_0072", competitionData.FreebieChallengeCount.ToString());
		}
		else if (competitionData.FreebieStatus == ArenaCompetition.FreebieStatusType.CanPermanentEntry)
		{
			freeEntryText = systemText.Get("Competition_0073", _restChallengeCount.ToString());
		}
		else
		{
			result = false;
		}
		return result;
	}

	private void AdmissionCompetitionNotFree()
	{
		AdmissionCompetition(isFreeBattleJoin: false);
	}

	private void SetMyPageCompetitionReload()
	{
		CompetitionUpdateEntryResumeButton();
		Initialize();
		UpdateEntryResumeButton();
	}
}
