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

namespace Wizard;

public class SealedLobby : MonoBehaviour
{
	[SerializeField]
	private ArenaCommonLobby _commonLobbyPrefab;

	[SerializeField]
	private GameObject _buttonsRoot;

	[SerializeField]
	private UIButton _deckEditButton;

	[SerializeField]
	private UIButton _deckViewButton;

	[SerializeField]
	private UIButton _deckCodeGenerateButton;

	[SerializeField]
	private UIButton _stageSelectButton;

	[SerializeField]
	private UIButton _retireButton;

	[SerializeField]
	private UISprite _incompleteDeckIcon;

	[SerializeField]
	private CardSelectDialog _cardSelectDialogPrefab;

	private const int BATTLE_MAX_NUM = 5;

	private List<string> _unloadAssetList = new List<string>();

	private ArenaCommonLobby _commonLobby;

	private UICardList _deckViewerDialog;

	private SealedData SealedData => Data.ArenaData.SealedData;

	public bool IsReady => _commonLobby.IsReady;

	public void Init()
	{
		_commonLobby = NGUITools.AddChild(base.gameObject, _commonLobbyPrefab.gameObject).GetComponent<ArenaCommonLobby>();
		_commonLobby.Init(new ArenaCommonLobbyInitParam
		{
			ClassId = SealedData.SelectedClassId.Value,
			BattleMaxNum = 5,
			BattleWinNum = SealedData.BattleWinNum,
			BattleResultList = SealedData.BattleResultList,
			BattleButtonClickCallback = OnClickBattleButton,
			RewardReceiveButtonClickCallback = OnClickRewardReceiveButton,
			BattleMaintenanceType = NetworkDefine.MAINTENANCE_TYPE.ARENA_SEALED_BATTLE_MAINTENANCE
		});
		_buttonsRoot.transform.SetParent(_commonLobby.ButtonsRoot.transform);
		_buttonsRoot.transform.localPosition = Vector3.zero;
		UIEventListener.Get(_deckEditButton.gameObject).onClick = OnClickDeckEditButton;
		UIEventListener.Get(_deckViewButton.gameObject).onClick = OnClickDeckViewButton;
		UIEventListener.Get(_deckCodeGenerateButton.gameObject).onClick = OnClickDeckCodeGenerateButton;
		UIEventListener.Get(_stageSelectButton.gameObject).onClick = OnClickStageSelectButton;
		UIEventListener.Get(_retireButton.gameObject).onClick = OnClickRetireButton;
		bool flag = SealedData.BattleResultList.Length >= 5;
		UIManager.SetObjectToGrey(_deckEditButton.gameObject, flag);
		UIManager.SetObjectToGrey(_stageSelectButton.gameObject, flag);
		UIManager.SetObjectToGrey(_retireButton.gameObject, flag);
		bool flag2 = !SealedData.IsCompletedDeck.Value && !flag;
		_commonLobby.SetDecisionButtonToDark(flag2);
		UIManager.SetObjectToGrey(_deckCodeGenerateButton.gameObject, flag2);
		_incompleteDeckIcon.gameObject.SetActive(flag2 || SealedData.DeckData.GetCardIdList().Any((int id) => GameMgr.GetIns().GetDataMgr().IsMaintenanceCard(id)));
	}

	public void Final()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_unloadAssetList);
		_unloadAssetList.Clear();
		_deckViewerDialog?.RemoveData();
		_commonLobby.Final();
		UnityEngine.Object.Destroy(_commonLobby.gameObject);
	}

	private void OnClickDeckEditButton(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		SealedController.GoToSealedDeckEdit();
	}

	private void OnClickDeckViewButton(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		StartCoroutine(OpenDeckViewerDialogCoroutine());
	}

	private IEnumerator OpenDeckViewerDialogCoroutine()
	{
		if (_deckViewerDialog == null)
		{
			_deckViewerDialog = DialogCreator.CreateDeckViewerDialog(base.gameObject);
			DeckData deckData = SealedData.DeckData;
			List<string> loadAssetList = _deckViewerDialog.GetLoadFileList(deckData.GetCardIdList());
			UIManager uiMgr = UIManager.GetInstance();
			uiMgr.createInSceneCenterLoading();
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadAssetList, delegate
			{
				_unloadAssetList.AddRange(loadAssetList);
			}));
			uiMgr.closeInSceneCenterLoading();
			_deckViewerDialog.SetDeck(deckData, null);
			_deckViewerDialog.SetShareButtonUse(isUse: true);
			_deckViewerDialog.SubmitDeckType = GenerateDeckCodeTask.SubmitDeckType.SEALED;
			CardDetailUI cardDetail = _deckViewerDialog.CardDetail;
			cardDetail.IsShowFlavorTextButton = true;
			cardDetail.IsShowVoiceButton = true;
			cardDetail.IsShowEvolutionButton = true;
		}
		_deckViewerDialog.SetActive(in_Active: true);
		_deckViewerDialog.ResetScroll();
	}

	private void OnClickDeckCodeGenerateButton(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		ArenaCommonLobby.GenerateDeckCode(GenerateDeckCodeTask.SubmitDeckType.SEALED, SealedData.SelectedClassId.Value, SealedData.DeckOriginalExcludedPhantomCardList, SealedData.DeckOriginalPhantomCardList);
	}

	private void OnClickStageSelectButton(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DialogCreator.CreateStageSelectDialog();
	}

	private void OnClickRetireButton(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		ArenaCommonLobby.OpenRetireConfirmDialog().onPushButton1 = delegate
		{
			StartCoroutine(Toolbox.NetworkManager.Connect(new SealedRetireTask(), delegate
			{
				ReceiveReward();
			}));
		};
	}

	private void OnClickBattleButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		Data.CurrentFormat = Format.Sealed;
		dataMgr.m_BattleType = DataMgr.BattleType.Sealed;
		dataMgr.SetSelectDeckId(SealedData.EntryId.Value);
		dataMgr.LastSelectDeckAttributeType = DeckAttributeType.Invalid;
		dataMgr.SetPlayerCharaIdByClassId(SealedData.SelectedClassId.Value);
		dataMgr.SetCurrentDeckData(SealedData.SortedDeckOriginalCardList);
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.RankMatch);
	}

	private void OnClickRewardReceiveButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		StartCoroutine(Toolbox.NetworkManager.Connect(new SealedFinishTask(), delegate
		{
			ReceiveReward();
		}));
	}

	private void ReceiveReward()
	{
		_commonLobby.ClickProtectionCollider.gameObject.SetActive(value: true);
		GameMgr.GetIns().GetInputMgr().isBackKeyEnable = false;
		_commonLobby.TreasureBoxInfo.OpenBox(delegate
		{
			_commonLobby.ClickProtectionCollider.gameObject.SetActive(value: false);
			GameMgr.GetIns().GetInputMgr().isBackKeyEnable = true;
			if (SealedData.RewardCardCandidates.Count <= 0)
			{
				OpenRewardReceiveDialog();
			}
			else
			{
				OpenCardSelectDialog();
			}
		});
	}

	private void OpenRewardReceiveDialog()
	{
		DialogBase dialogBase = DialogCreator.CreateRewardReceiveDialog(SealedData.RewardList);
		dialogBase.SetLayer("Loading");
		dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_CANCEL_TRANS;
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.MyPage);
		});
	}

	private void OpenCardSelectDialog()
	{
		StartCoroutine(OpenCardSelectDialogCoroutine());
	}

	private IEnumerator OpenCardSelectDialogCoroutine()
	{
		UIManager uiMgr = UIManager.GetInstance();
		uiMgr.createInSceneCenterLoading();
		SystemText systemText = Data.SystemText;
		DialogBase dialog = uiMgr.CreateDialogClose();
		dialog.SetLayer("Loading");
		dialog.SetSize(DialogBase.Size.M);
		dialog.SetTitleLabel(systemText.Get("Sealed_RewardCardSelect_0001"));
		dialog.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialog.onPushButton1 = (dialog.onCloseWithoutSelect = _commonLobby.TreasureBoxInfo.Reset);
		CardSelectDialog dialogObject = NGUITools.AddChild(dialog.gameObject, _cardSelectDialogPrefab.gameObject).GetComponent<CardSelectDialog>();
		dialogObject.Init(SealedData.RewardCardCandidates, delegate(int cardId)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			OpenCardSelectConfirmDialog(dialog, cardId);
		}, systemText.Get("Sealed_RewardCardSelect_0002"));
		while (!dialogObject.IsReady)
		{
			yield return null;
		}
		uiMgr.closeInSceneCenterLoading();
	}

	private void OpenCardSelectConfirmDialog(DialogBase cardSelectDialog, int cardId)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.SetTitleLabel(systemText.Get("Sealed_RewardCardSelect_0003"));
		dialogBase.SetText(systemText.Get("Sealed_RewardCardSelect_0004", CardMaster.GetInstance(FormatBehaviorManager.GetDefaultBehaviour(Format.Sealed).CardMasterId).GetCardParameterFromId(cardId).CardName));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(systemText.Get("Common_0004"));
		dialogBase.onPushButton1 = delegate
		{
			cardSelectDialog.Close();
			StartCoroutine(Toolbox.NetworkManager.Connect(new SealedSelectPhantomCardTask(cardId), delegate
			{
				OpenRewardReceiveDialog();
			}));
		};
	}
}
