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

namespace Wizard;

public class GatheringChatDeckList : MonoBehaviour, IChatActionUI
{
	private const int DECK_DETAIL_DIALOG_DEPTH = 100;

	[SerializeField]
	private UIButton _buttonGatheringDeckList;

	[SerializeField]
	private UILabel _labelGatheringDeckListButton;

	[SerializeField]
	private DeckDetailDialog _deckDetailDialogPrefab;

	private DeckDetailDialog _deckDetailDialog;

	private GatheringInfo _gatheringInfo;

	private DialogBase _dialogDeckList;

	private ChatConnectController _chatConnectController;

	private ChatLogUI _chatLogUI;

	private readonly List<string> _loadedVoiceList = new List<string>();

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatConnectController = chatConnectController;
		_chatLogUI = chatLogUI;
		GatheringChatSettings gatheringChatSettings = chatSettings as GatheringChatSettings;
		_gatheringInfo = gatheringChatSettings.GatheringInfo;
		_labelGatheringDeckListButton.text = GetTextDeckListTitle(_gatheringInfo);
		_buttonGatheringDeckList.onClick.Clear();
		_buttonGatheringDeckList.onClick.Add(new EventDelegate(delegate
		{
			OnClickGatheringDeckList();
		}));
	}

	private string GetTextDeckListTitle(GatheringInfo gatheringInfo)
	{
		if (gatheringInfo.Rule.IsEntryDeckOnly)
		{
			return Data.SystemText.Get("Gathering_Chat_0001");
		}
		return Data.SystemText.Get("Gathering_Chat_0021");
	}

	private void OnClickGatheringDeckList()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		GatheringConfirmDeckListTask task = new GatheringConfirmDeckListTask();
		_chatConnectController.StartConnectCommon(task, delegate
		{
			CreateDeckList(task.DeckGroupListData, task.DeckFormat);
		});
	}

	private void CreateDeckList(DeckGroupListData deckGroupListData, Format deckFormat, bool isFadeBackView = true, bool isOpenSE = true)
	{
		DeckSelectUIDialog deckSelectUIDialog = DeckSelectUIDialog.Create(GetTextDeckListTitle(_gatheringInfo), deckGroupListData, deckFormat, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: false, delegate(DialogBase dialog, DeckData deck)
		{
			CreateDeckDetailDialog(deck);
		});
		_dialogDeckList = deckSelectUIDialog.Dialog;
		_dialogDeckList.IsEnableOpenSe = isOpenSE;
		if (!isFadeBackView)
		{
			_dialogDeckList.ResetBackViewAlpha();
		}
		_dialogDeckList.SetLayer("Loading");
	}

	private void CreateDeckDetailDialog(DeckData deck)
	{
		_deckDetailDialog = UnityEngine.Object.Instantiate(_deckDetailDialogPrefab);
		_deckDetailDialog.gameObject.SetActive(value: true);
		_deckDetailDialog.InitializeForGathering(deck, _gatheringInfo.Rule.IsEntryDeckOnly, delegate
		{
			GatheringConfirmDeckListTask task = new GatheringConfirmDeckListTask();
			_chatConnectController.StartConnectCommon(task, delegate
			{
				_dialogDeckList.Close();
				CreateDeckList(task.DeckGroupListData, task.DeckFormat, isFadeBackView: false, isOpenSE: false);
			});
		}, _loadedVoiceList);
		DialogBase dialog = UIManager.GetInstance().CreateDialogClose();
		dialog.SetSize(DialogBase.Size.M);
		dialog.SetPanelDepth(100);
		dialog.SetTitleLabel("");
		dialog.SetObj(_deckDetailDialog.gameObject);
		DialogBase dialogBase = dialog;
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			_deckDetailDialog.Final();
		});
		Action OnOpenDeckView = delegate
		{
			if (_dialogDeckList != null)
			{
				_dialogDeckList.SetActive(inActive: false);
			}
			dialog.Close();
		};
		Action OnCloseDeckView = delegate
		{
			if (_dialogDeckList != null)
			{
				_dialogDeckList.SetActive(inActive: true);
			}
		};
		Action onPushButton = delegate
		{
			_chatLogUI.ShowDeckViewByDeckData(deck, OnOpenDeckView, OnCloseDeckView, isActiveRedButton: false, isActiveDeckIntroductionObj: false);
		};
		dialog.SetButtonLayout(DialogBase.ButtonLayout.GrayBtn);
		dialog.SetButtonText(Data.SystemText.Get("Card_0083"));
		dialog.ClickSe_Btn1 = Se.TYPE.SYS_BTN_DECIDE;
		dialog.onPushButton1 = onPushButton;
		dialog.isNotCloseWindowButton1 = true;
	}

	private void OnDestroy()
	{
		if (_loadedVoiceList.Count > 0)
		{
			GameMgr.GetIns().GetSoundMgr().StopVoiceAll(0f);
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadedVoiceList);
			_loadedVoiceList.Clear();
		}
	}
}
