using System;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.Arena;

namespace Wizard;

public class CompetitionEntryDialog : ArenaEntryDialogBase
{
	public delegate void OnCompleteEntry();

	public delegate void OnCompleteJoin();

	[SerializeField]
	private GameObject _prefabRewardDialog;

	[SerializeField]
	private CardDetailUI _cardDetailDialog;

	[SerializeField]
	private UILabel _mainText;

	[SerializeField]
	private GameObject _competitionEntryCompleteDialog;

	[SerializeField]
	private GameObject _rewardRoot;

	[SerializeField]
	private GameObject _freeEntryDialogLine;

	[SerializeField]
	private UIButton _entryRupyButton;

	[SerializeField]
	private UIButton _entryCrystalButton;

	[SerializeField]
	private UIGrid _entryButtonGrid;

	[SerializeField]
	private GameObject _rewardLabel;

	[SerializeField]
	private GameObject _coutionLabel;

	public Action CompleteEntryHandler;

	public Action CompleteJoinHandler;

	private const int CELL_WIDTH_ADJUST = -12;

	private const int CELL_WIDTH_ADJUST_COUNT = 4;

	private readonly Vector3 FREE_BATTLE_LABEL_POS = new Vector3(0f, 198f, 0f);

	private readonly Vector3 FREE_BATTLE_COUTION_LABEL_POS = new Vector3(0f, 108f, 0f);

	private readonly Vector3 FREE_BATTLE_REWARD_LABEL_POS = new Vector3(0f, 29f, 0f);

	private readonly Vector3 FREE_BATTLE_REWARD_POS = new Vector3(0f, -96f, 0f);

	protected override void Init()
	{
		IsCompetition = true;
		switch (Data.ArenaData.CompetitionData.CostType)
		{
		case ArenaCompetition.EntryCostType.EntryWithFree:
		{
			bool flag = Data.ArenaData.CompetitionData.CurrentWinCount > 0;
			_mainText.text = (flag ? Data.SystemText.Get("Competition_0062", Data.ArenaData.CompetitionData.MaxChallengeCount.ToString()) : Data.SystemText.Get("Competition_0069", Data.ArenaData.CompetitionData.MaxChallengeCount.ToString()));
			_freeEntryDialogLine.SetActive(value: true);
			_mainText.gameObject.transform.localPosition = FREE_BATTLE_LABEL_POS;
			_rewardRoot.transform.localPosition = FREE_BATTLE_REWARD_POS;
			_rewardLabel.transform.localPosition = FREE_BATTLE_REWARD_LABEL_POS;
			_coutionLabel.transform.localPosition = FREE_BATTLE_COUTION_LABEL_POS;
			break;
		}
		case ArenaCompetition.EntryCostType.EntryWithCost:
			_mainText.text = Data.SystemText.Get("Competition_0077", Data.ArenaData.CompetitionData.MaxChallengeCount.ToString());
			_freeEntryDialogLine.SetActive(value: false);
			break;
		}
		_entryCrystalButton.gameObject.SetActive(Data.ArenaData.CompetitionData.crystalCost != 0);
		_entryRupyButton.gameObject.SetActive(Data.ArenaData.CompetitionData.rupyCost != 0);
		_entryButtonGrid.Reposition();
		_arenaNameTextId = "Competition_0035";
		_entryButtonSe = Se.TYPE.SYS_BTN_DECIDE;
	}

	protected override int GetTicketNum()
	{
		return PlayerStaticData.UserColosseumTicketNum;
	}

	protected override ArenaEntryDataBase GetEntryData()
	{
		return Data.ArenaData.CompetitionData;
	}

	protected override void Entry()
	{
		base.Entry();
		if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithCost)
		{
			CompetitionEntryTask competitionEntryTask = new CompetitionEntryTask();
			competitionEntryTask.SetParameter(_payType);
			StartCoroutine(Toolbox.NetworkManager.Connect(competitionEntryTask, EntryTaskSuccess));
		}
		else
		{
			CompetitionPermanentEntryTask competitionPermanentEntryTask = new CompetitionPermanentEntryTask();
			competitionPermanentEntryTask.SetParameter(_payType);
			StartCoroutine(Toolbox.NetworkManager.Connect(competitionPermanentEntryTask, EntryTaskSuccess));
		}
	}

	private void EntryTaskSuccess(NetworkTask.ResultCode inResult)
	{
		base.ParentDialog.CloseWithoutSelect();
		CompleteEntryHandler.Call();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Competition_0018"));
		GameObject gameObject = UnityEngine.Object.Instantiate(_competitionEntryCompleteDialog);
		dialogBase.SetObj(gameObject);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		SetRewardInfo(gameObject, isOnlyIcon: false, isSetLabel: true);
		gameObject.transform.localPosition = Vector3.forward * 10f;
		dialogBase.OnClose = delegate
		{
			if (Data.ArenaData.CompetitionData.CostType == ArenaCompetition.EntryCostType.EntryWithFree)
			{
				UIManager.ViewScene nextScene = ((Data.ArenaData.CompetitionData.Rule == ArenaColosseum.eRule.TwoPick) ? UIManager.ViewScene.Competition2Pick : UIManager.ViewScene.CompetitionLobby);
				UIManager.GetInstance().ChangeViewScene(nextScene);
			}
			else
			{
				CreateJoinCompetitionDialog();
			}
		};
	}

	public void SetRewardInfoEntryConfirm()
	{
		SetRewardInfo(_rewardRoot, isOnlyIcon: true);
	}

	public void SetRewardInfo(GameObject root, bool isOnlyIcon = false, bool isSetLabel = false)
	{
		RewardBase component = NGUITools.AddChild(root, _prefabRewardDialog).GetComponent<RewardBase>();
		component.SetCardDetailUI(_cardDetailDialog);
		foreach (Wizard.Scripts.Network.Data.TaskData.Arena.Reward entryReward in Data.ArenaData.CompetitionData.EntryRewardList)
		{
			component.AddReward(entryReward);
		}
		component.GetComponent<UIPanel>().depth = 8;
		if (component._rewardObjectInfoList.Count >= 4)
		{
			component.AllInEndCreate(0.8f, -12);
		}
		else
		{
			component.AllInEndCreate(1f);
		}
		if (isOnlyIcon)
		{
			component.SetOnlyIconEndCreate(isSleeveRotate: false);
		}
		component.SetActiveRewardLabel(isSetLabel);
	}

	public void CreateJoinCompetitionDialog()
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(systemText.Get("Competition_0039"));
		dialogBase.SetText(systemText.Get("Competition_0078"));
		dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_DECIDE_TRANS;
		dialogBase.SetButtonDelegate(delegate
		{
			CompleteJoinHandler.Call();
		});
	}
}
