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

public class ColosseumEntry : ArenaEntryBase
{
	[SerializeField]
	private GameObject _detailPrefab;

	[SerializeField]
	private MyPageItemArena _myPageItemArena;

	[SerializeField]
	private GameObject _costRoot;

	[SerializeField]
	private GameObject _entryStatusRoot;

	[SerializeField]
	private UILabel _entryStatusLabel;

	[SerializeField]
	private UIButton _entryButton;

	[SerializeField]
	private GameObject _infoBase;

	[SerializeField]
	private UILabel _formatLabel;

	[SerializeField]
	private UILabel _periodLabel;

	[SerializeField]
	private UILabel _ownStatusLabel;

	[SerializeField]
	private GameObject _twoPickRound1InfoBase;

	[SerializeField]
	private UILabel _twoPickFormatLabel;

	[SerializeField]
	private UILabel _twoPickPoolLabel;

	[SerializeField]
	private UILabel _twoPickPeriodLabel;

	[SerializeField]
	private UILabel _twoPickStatusLabel;

	[SerializeField]
	private GameObject _twoPickRound2InfoBase;

	[SerializeField]
	private UILabel _twoPickRound2FormatLabel;

	[SerializeField]
	private UILabel _twoPickRound2PoolLabel;

	[SerializeField]
	private UILabel _twoPickRound2PeriodLabel;

	[SerializeField]
	private UILabel _twoPickRound2StatusLabel;

	[SerializeField]
	private UILabel _retryNumberLabel;

	[SerializeField]
	public GameObject _freeEntryIcon;

	private const string ORANGE_COLOR_CODE = "[fcd24a]";

	private const int FORMAT_LABEL_UP_Y = 49;

	private const int FORMAT_LABEL_CENTER_Y = 14;

	private const int FORMAT_LABEL_UP_Y_FIVE_LINES = 56;

	private const int FORMAT_LABEL_CENTER_FIVE_LINES = 30;

	private const string DECK_DECISION_COLOSSEUM_PATH = "UI/DeckList/DeckDecisionColosseum";

	private const string DECK_DECISION_COLOSSEUM_FINAL_PATH = "UI/DeckList/DeckDecisionColosseumFinal";

	private void Awake()
	{
		EntryBaseInit(_costRoot);
	}

	protected override void EntryBaseInit(GameObject costRootObject)
	{
		base.EntryBaseInit(costRootObject);
		SystemText systemText = Data.SystemText;
		_entryDialogTitleText = systemText.Get("Colosseum_0003");
		_resumeFunc = Resume;
		_isJoinFunc = IsJoin;
		_initFunc = Init;
	}

	private void Init()
	{
		if (Data.ArenaData.ColosseumData.IsFreeEntry)
		{
			_isFreeEntry = true;
			_freeEntryFunc = FreeEntry;
		}
		else
		{
			_isFreeEntry = false;
		}
		EntryStatusLabelInit();
		UIManager.SetObjectToGrey(_entryButton.gameObject, !isEntryPossible());
		UILabel componentInChildren = _entryButton.GetComponentInChildren<UILabel>();
		if (isEntryPossible() && _isFreeEntry)
		{
			componentInChildren.text = Data.SystemText.Get("Colosseum_0103");
		}
		else
		{
			componentInChildren.text = Data.SystemText.Get("Arena_0034");
		}
	}

	protected override void EntryDialogCreate(GameObject inDialogObject)
	{
		inDialogObject.AddComponent<ColosseumEntryDialog>().ColosseumEntryClass = this;
	}

	private void Resume()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
		if (Data.ArenaData.ColosseumData.IsDeckEntry)
		{
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Colosseum);
		}
		else
		{
			EntryTaskSuccess(NetworkTask.ResultCode.Success);
		}
	}

	private bool IsJoin()
	{
		return Data.ArenaData.ColosseumData.isJoin;
	}

	public void OnClickDetailButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		OpenDetail();
	}

	private void FreeEntry()
	{
		ColosseumEntryTask colosseumEntryTask = new ColosseumEntryTask();
		colosseumEntryTask.SetParameter(ArenaData.eARENA_PAY.Free);
		StartCoroutine(Toolbox.NetworkManager.Connect(colosseumEntryTask, FreeEntryTaskSuccess));
	}

	private void FreeEntryTaskSuccess(NetworkTask.ResultCode inResult)
	{
		EntryTaskSuccess(inResult);
		Data.ArenaData.ColosseumData.IsFreeEntry = false;
		_myPageItemArena._colosseumCardPanel.GetComponent<ColosseumCardPanel>().NowUpdate();
	}

	public void EntryTaskSuccess(NetworkTask.ResultCode inResult)
	{
		Data.ArenaData.ColosseumData.isJoin = true;
		if (Data.ArenaData.ColosseumData.IsTwoPickRule || Data.ArenaData.ColosseumData.DeckFormat == Format.Avatar)
		{
			GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.ColosseumTwoPick;
			Data.CurrentFormat = Format.Max;
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Colosseum);
		}
		else
		{
			UpdateEntryResumeButton();
			GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.ColosseumNormal;
			Data.CurrentFormat = Data.ArenaData.ColosseumData.DeckFormat;
			DeckInfoTask task = new DeckInfoTask();
			task.SetParameter(Data.ArenaData.ColosseumData.DeckFormat);
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
			{
				if (!RunSpecialDeckSelectEntry())
				{
					DeckSelectUI.InitOptions initOptions = new DeckSelectUI.InitOptions
					{
						CanUseNonPossessionCard = Data.ArenaData.ColosseumData.CanUseNonPossessionCard
					};
					DeckSelectUIDialog.Create(Data.SystemText.Get("Battle_0488"), task.DeckGroupListData, Data.ArenaData.ColosseumData.DeckFormat, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: true, CreateDeckSelectConfirmDialog, initOptions);
				}
			}));
		}
		HeadLineObject.GetComponent<ColosseumHeadLine>().UpdateHeadLine();
	}

	private bool RunSpecialDeckSelectEntry()
	{
		if (Data.ArenaData.ColosseumData.Rule == ArenaColosseum.eRule.HOF)
		{
			GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.ColosseumHof;
			ColosseumHOFDeckInfoTask task = new ColosseumHOFDeckInfoTask();
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
			{
				List<DeckGroup> deckGroupList = new List<DeckGroup>
				{
					new DeckGroup(task.DeckList, Format.Max, DeckAttributeType.CustomDeck)
				};
				DeckSelectUIDialog.Create(Data.SystemText.Get("Colosseum_0109"), new DeckGroupListData(deckGroupList), Format.Max, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: false, CreateDeckSelectConfirmDialog);
			}));
			return true;
		}
		if (Data.ArenaData.ColosseumData.Rule == ArenaColosseum.eRule.WindFall)
		{
			GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.ColosseumWindFall;
			ColosseumWindFallDeckInfoTask task2 = new ColosseumWindFallDeckInfoTask();
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task2, delegate
			{
				List<DeckGroup> deckGroupList = new List<DeckGroup>
				{
					new DeckGroup(task2.DeckList, Format.Max, DeckAttributeType.CustomDeck)
				};
				DeckSelectUIDialog.Create(Data.SystemText.Get("Colosseum_0117"), new DeckGroupListData(deckGroupList), Format.Max, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: false, CreateDeckSelectConfirmDialog);
			}));
			return true;
		}
		return false;
	}

	private void CreateDeckSelectConfirmDialog(DialogBase dialogDeckList, DeckData deck)
	{
		if (!deck.IsUsable(Data.ArenaData.ColosseumData.CanUseNonPossessionCard))
		{
			InCompleteDeckDecideDialog.Create(dialogDeckList, deck);
			return;
		}
		CompleteDeckDecideDialog completeDeckDecideDialog = CompleteDeckDecideDialog.CreateForSingleDeck(dialogDeckList, deck, showSimpleStageOption: true, delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			DeckSetAndMoveColosseum(deck);
		});
		completeDeckDecideDialog.DecisionUI.CardDetailCustomize = delegate(CardDetailUI detailUI)
		{
			detailUI.IsShowFlavorTextButton = false;
			detailUI.IsShowVoiceButton = false;
			detailUI.IsShowEvolutionButton = false;
		};
		completeDeckDecideDialog.DecisionUI.CardListCustomize = delegate(UICardList cardList)
		{
			if (Data.ArenaData.ColosseumData.IsSpecialDeckSelectRule)
			{
				cardList.SetShareButtonUse(isUse: false);
			}
			if (Data.ArenaData.ColosseumData.IsDeckMaxNumberChange)
			{
				cardList.SetMaxCardNum(Data.ArenaData.ColosseumData.DeckMaxNumber);
			}
		};
		if (Data.ArenaData.ColosseumData.Rule == ArenaColosseum.eRule.HOF)
		{
			completeDeckDecideDialog.DecisionUI.IsCanShowQRCode = false;
		}
		if (GameMgr.GetIns().GetDataMgr().m_BattleType == DataMgr.BattleType.ColosseumNormal)
		{
			completeDeckDecideDialog.DecisionUI.gameObject.GetComponent<UIWidget>().alpha = 0f;
			DeckDecisionColosseum deckDecisionColosseum = Object.Instantiate(Toolbox.ResourcesManager.LoadObject<DeckDecisionColosseum>("UI/DeckList/DeckDecisionColosseum", isServerResources: false));
			completeDeckDecideDialog.Dialog.SetObj(deckDecisionColosseum.gameObject);
			deckDecisionColosseum.Init(deck);
			completeDeckDecideDialog.DecisionUI.transform.SetParent(deckDecisionColosseum.transform.parent.transform);
		}
	}

	private void DeckSetAndMoveColosseum(DeckData inDeckData)
	{
		Data.ArenaData.ColosseumData.DeckList.Clear();
		Data.ArenaData.ColosseumData.DeckList.Add(inDeckData);
		if (!Data.ArenaData.ColosseumData.IsSpecialDeckSelectRule)
		{
			DeckListUtility.SaveLastSelectDeck(inDeckData.GetDeckID(), isDefaultDeck: false, isTrialDeck: false, Data.ArenaData.ColosseumData.DeckFormat);
		}
		BaseTask baseTask = null;
		if (Data.ArenaData.ColosseumData.Rule == ArenaColosseum.eRule.HOF)
		{
			((ColosseumDeckEntryHOFTask)(baseTask = new ColosseumDeckEntryHOFTask())).SetParameter(Data.ArenaData.ColosseumData.DeckList);
		}
		else if (Data.ArenaData.ColosseumData.Rule == ArenaColosseum.eRule.WindFall)
		{
			((ColosseumWindFallDeckEntry)(baseTask = new ColosseumWindFallDeckEntry())).SetParameter(Data.ArenaData.ColosseumData.DeckList);
		}
		else if (Data.ArenaData.ColosseumData.DeckFormat == Format.Avatar)
		{
			((ColosseumDeckEntryAvatarTask)(baseTask = new ColosseumDeckEntryAvatarTask())).SetParameter(Data.ArenaData.ColosseumData.DeckList);
		}
		else
		{
			((ColosseumDeckEntryTask)(baseTask = new ColosseumDeckEntryTask())).SetParameter(Data.ArenaData.ColosseumData.DeckList, isPublished: false);
		}
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(baseTask, delegate
		{
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Colosseum);
		}));
	}

	public void OpenDetail()
	{
		ColosseumDetailTask task = new ColosseumDetailTask();
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, DetailTaskSuccess));
	}

	private void DetailTaskSuccess(NetworkTask.ResultCode inResultCode)
	{
		Data.ArenaData.ColosseumData.CreateDetailDialog(_detailPrefab);
	}

	private bool isEntryPossible()
	{
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		if ((!colosseumData.IsRetry || colosseumData.IsClear || colosseumData.IsFinalRoundTry) && !colosseumData.isJoin)
		{
			return false;
		}
		return true;
	}

	protected void EntryStatusLabelInit()
	{
		InfoTextUpdate();
		EntryTextUpdate();
	}

	private void ColosseumDeckDeletedDialogMessageReceiver()
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.S);
		if (Data.ArenaData.ColosseumData.IsTwoPickRule)
		{
			dialogBase.SetText(Data.SystemText.Get("Colosseum_0102"));
		}
		else
		{
			dialogBase.SetText(Data.SystemText.Get("Error_4403"));
		}
		dialogBase.SetTitleLabel(Data.SystemText.Get("Common_0021"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		Data.ArenaData.ColosseumData.IsDeckDeleted = false;
	}

	private void InfoTextUpdate()
	{
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		SystemText systemText = Data.SystemText;
		UILabel uILabel = null;
		string text = colosseumData.Rule switch
		{
			ArenaColosseum.eRule.TwoPick => (colosseumData.IsNormalTwoPick ? systemText.Get("Arena_0002") : systemText.Get("Colosseum_0105")) + " " + systemText.Get("Colosseum_0093"), 
			ArenaColosseum.eRule.TwoPickChaos => systemText.Get("Chaos_FormatName") + " " + systemText.Get("Colosseum_0093"), 
			ArenaColosseum.eRule.HOF => systemText.Get("Colosseum_0108"), 
			ArenaColosseum.eRule.WindFall => systemText.Get("Colosseum_0116"), 
			_ => UIUtil.GetFormatName(colosseumData.DeckFormat) + systemText.Get("Colosseum_0093"), 
		};
		UILabel uILabel2;
		UILabel uILabel3;
		UILabel uILabel4;
		if (colosseumData.Rule == ArenaColosseum.eRule.TwoPick || colosseumData.Rule == ArenaColosseum.eRule.TwoPickChaos)
		{
			if (colosseumData.StageNo == ArenaColosseum.eStageNo.Stage2)
			{
				_infoBase.SetActive(value: false);
				_twoPickRound1InfoBase.SetActive(value: false);
				_twoPickRound2InfoBase.SetActive(value: true);
				uILabel2 = _twoPickRound2FormatLabel;
				uILabel3 = _twoPickRound2PoolLabel;
				uILabel4 = _twoPickRound2PeriodLabel;
				uILabel = _twoPickRound2StatusLabel;
			}
			else
			{
				_infoBase.SetActive(value: false);
				_twoPickRound1InfoBase.SetActive(value: true);
				_twoPickRound2InfoBase.SetActive(value: false);
				uILabel2 = _twoPickFormatLabel;
				uILabel3 = _twoPickPoolLabel;
				uILabel4 = _twoPickPeriodLabel;
				uILabel = _twoPickStatusLabel;
			}
		}
		else
		{
			_infoBase.SetActive(value: true);
			_twoPickRound1InfoBase.SetActive(value: false);
			_twoPickRound2InfoBase.SetActive(value: false);
			uILabel2 = _formatLabel;
			uILabel3 = null;
			uILabel4 = _periodLabel;
			uILabel = _ownStatusLabel;
		}
		uILabel2.text = systemText.Get("Colosseum_0054", text);
		if (colosseumData.IsRoundPeriod)
		{
			uILabel4.text = colosseumData.NowRoundTimeText;
		}
		else
		{
			string text2 = ((colosseumData.StageNo != ArenaColosseum.eStageNo.FinalStage) ? systemText.Get("Colosseum_0007", ((int)colosseumData.StageNo).ToString()) : systemText.Get("Colosseum_0008"));
			uILabel4.text = text2 + systemText.Get("Colosseum_0101") + colosseumData.NowRoundTimeText;
		}
		if (colosseumData.Rule == ArenaColosseum.eRule.TwoPick || colosseumData.Rule == ArenaColosseum.eRule.TwoPickChaos)
		{
			uILabel3.text = systemText.Get("Arena_0142", colosseumData.CardPool);
		}
		_ownStatusLabel.text = string.Empty;
		_twoPickStatusLabel.text = string.Empty;
		_twoPickRound2StatusLabel.text = string.Empty;
		if (colosseumData.StageNo == ArenaColosseum.eStageNo.Stage1)
		{
			if (colosseumData.NextRound == ArenaColosseum.eRound.Round2A || colosseumData.NextRound == ArenaColosseum.eRound.Round2B)
			{
				uILabel.text = systemText.Get("Colosseum_0048", colosseumData.GetGroupText(colosseumData.NextRound));
			}
		}
		else if (colosseumData.StageNo == ArenaColosseum.eStageNo.Stage2)
		{
			string text3 = string.Empty;
			string text4 = string.Empty;
			if (colosseumData.IsRoundPeriod)
			{
				if (colosseumData.Round == ArenaColosseum.eRound.Round2A || colosseumData.Round == ArenaColosseum.eRound.Round2B)
				{
					text3 = systemText.Get("Colosseum_0100", colosseumData.GetGroupText(colosseumData.Round));
				}
				if (colosseumData.NextRound == ArenaColosseum.eRound.Undecided)
				{
					text4 = systemText.Get("Colosseum_0052");
				}
				else if (colosseumData.NextRound == ArenaColosseum.eRound.FinalA || colosseumData.NextRound == ArenaColosseum.eRound.FinalB)
				{
					text4 = systemText.Get("Colosseum_0037_Group", colosseumData.GetGroupText(colosseumData.NextRound));
				}
				uILabel.text = text3 + "\n" + text4;
			}
			else if (colosseumData.NextRound == ArenaColosseum.eRound.Round2A || colosseumData.NextRound == ArenaColosseum.eRound.Round2B)
			{
				uILabel.text = systemText.Get("Colosseum_0048", colosseumData.GetGroupText(colosseumData.NextRound));
			}
		}
		else if (colosseumData.StageNo == ArenaColosseum.eStageNo.FinalStage && colosseumData.Round != ArenaColosseum.eRound.Lose)
		{
			if (colosseumData.IsClear)
			{
				uILabel.text = systemText.Get("Colosseum_0038", colosseumData.Name);
			}
			else if (!colosseumData.IsFinalRoundTry)
			{
				string text5 = string.Empty;
				if (colosseumData.IsRoundPeriod)
				{
					if (colosseumData.Round == ArenaColosseum.eRound.FinalA || colosseumData.Round == ArenaColosseum.eRound.FinalB)
					{
						text5 = systemText.Get("Colosseum_0100", colosseumData.GetGroupText(colosseumData.Round));
					}
					uILabel.text = text5;
				}
				else if (colosseumData.NextRound == ArenaColosseum.eRound.Lose)
				{
					uILabel.text = string.Empty;
				}
				else
				{
					if (colosseumData.NextRound == ArenaColosseum.eRound.FinalA || colosseumData.NextRound == ArenaColosseum.eRound.FinalB)
					{
						text5 = systemText.Get("Colosseum_0100", colosseumData.GetGroupText(colosseumData.NextRound));
					}
					uILabel.text = text5 + "\n" + systemText.Get("Colosseum_0037");
				}
			}
		}
		if (uILabel.text == string.Empty)
		{
			if ((colosseumData.Rule == ArenaColosseum.eRule.TwoPick || colosseumData.Rule == ArenaColosseum.eRule.TwoPickChaos) && colosseumData.StageNo == ArenaColosseum.eStageNo.FinalStage)
			{
				uILabel2.transform.localPosition = new Vector3(uILabel2.transform.localPosition.x, 30f);
			}
			else
			{
				uILabel2.transform.localPosition = new Vector3(uILabel2.transform.localPosition.x, 14f);
			}
			return;
		}
		if ((colosseumData.Rule == ArenaColosseum.eRule.TwoPick || colosseumData.Rule == ArenaColosseum.eRule.TwoPickChaos) && colosseumData.StageNo == ArenaColosseum.eStageNo.Stage2)
		{
			uILabel2.transform.localPosition = new Vector3(uILabel2.transform.localPosition.x, 56f);
		}
		else
		{
			uILabel2.transform.localPosition = new Vector3(uILabel2.transform.localPosition.x, 49f);
		}
		uILabel.text = "[fcd24a]" + uILabel.text + "[-]";
	}

	private void EntryTextUpdate()
	{
		ArenaColosseum colosseumData = Data.ArenaData.ColosseumData;
		SystemText systemText = Data.SystemText;
		if (!isEntryPossible())
		{
			_costRoot.SetActive(value: false);
			_entryStatusRoot.SetActive(value: true);
			_entryStatusLabel.text = string.Empty;
			if (colosseumData.IsClear || colosseumData.IsFinalRoundTry)
			{
				_entryStatusLabel.text = systemText.Get("Colosseum_0086");
			}
			else if (colosseumData.Round == ArenaColosseum.eRound.Lose)
			{
				_entryStatusLabel.text = systemText.Get("Colosseum_0022");
			}
			else if (!colosseumData.IsRoundPeriod)
			{
				if (colosseumData.StageNo == ArenaColosseum.eStageNo.Stage1)
				{
					_entryStatusLabel.text = systemText.Get("Colosseum_0064");
				}
				else if (colosseumData.NextRound == ArenaColosseum.eRound.Lose)
				{
					_entryStatusLabel.text = systemText.Get("Colosseum_0022");
				}
				else
				{
					_entryStatusLabel.text = systemText.Get("Colosseum_0056");
				}
			}
			else if (!colosseumData.IsRetry)
			{
				if (colosseumData.IsLastDay)
				{
					_entryStatusLabel.text = systemText.Get("Colosseum_0068");
				}
				else
				{
					_entryStatusLabel.text = systemText.Get("Colosseum_0067");
				}
			}
		}
		else if (colosseumData.IsFreeEntry || colosseumData.IsFinalRound())
		{
			_costRoot.SetActive(value: false);
			_entryStatusRoot.SetActive(value: true);
			if (!colosseumData.IsFinalRound())
			{
				string id = "Colosseum_0047";
				if (colosseumData.IsLastDay)
				{
					id = "Colosseum_0050";
				}
				_entryStatusLabel.text = "[fcd24a]" + systemText.Get(id, colosseumData.RetryRemainingNum.ToString()) + "[-]\n\n" + systemText.Get("Colosseum_0026");
			}
			else
			{
				_entryStatusLabel.text = systemText.Get("Colosseum_0092");
			}
		}
		else
		{
			_costRoot.SetActive(value: true);
			_entryStatusRoot.SetActive(value: false);
		}
		if (colosseumData.IsLastDay)
		{
			_retryNumberLabel.text = systemText.Get("Colosseum_0050", colosseumData.RetryRemainingNum.ToString());
		}
		else
		{
			_retryNumberLabel.text = systemText.Get("Colosseum_0047", colosseumData.RetryRemainingNum.ToString());
		}
	}
}
