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

namespace Wizard;

public class ChatDeckView : MonoBehaviour
{
	private const int DEPTH_CONFIRM_DIALOG = 100;

	[SerializeField]
	private GameObject _deckViewPrefab;

	[SerializeField]
	private GameObject _cardDetailPrefab;

	private GameObject _deckViewObj;

	private UICardList _uiCardList;

	private GameObject _cardDetailObj;

	private CardDetailUI _cardDetail;

	private List<string> _loadCardAssetList;

	private IChatApiSettings _chatApiSettings;

	private ChatConnectController _chatConnectCtr;

	private Action<int> _actionCloseChatLogContent;

	private Action _onOpen;

	private Action _onClose;

	private Action<Format, Dictionary<Format, List<ChatMessageInfo.DeckLogData>>> _onDeleteDeckSuccess;

	public void Init(IChatApiSettings chatApiSettings, ChatConnectController chatConnectCtr, Action<int> actionCloseChatLogContent)
	{
		_chatApiSettings = chatApiSettings;
		_chatConnectCtr = chatConnectCtr;
		_actionCloseChatLogContent = actionCloseChatLogContent;
	}

	public bool IsShowDeckCardView()
	{
		if (_deckViewObj == null)
		{
			return false;
		}
		return _deckViewObj.activeSelf;
	}

	public void ShowDeckViewByDeckLog(ChatMessageInfo.DeckLogData deckLogData, Action onOpen = null, Action onClose = null, Action<Format, Dictionary<Format, List<ChatMessageInfo.DeckLogData>>> onDeleteDeckSuccess = null)
	{
		_onDeleteDeckSuccess = onDeleteDeckSuccess;
		ShowDeckViewByDeckData(deckLogData.DeckData, onOpen, onClose, deckLogData.IsAbleDeleteDeck);
		if (deckLogData.IsAbleDeleteDeck)
		{
			_uiCardList.SetRedButtonOnClickCallBack(Data.SystemText.Get("Guild_Chat_0034"), delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
				CreateConfirmDialogDeleteDeckLog(deckLogData);
			});
		}
	}

	public void ShowDeckViewByDeckData(DeckData deck, Action onOpen, Action onClose, bool isActiveRedButton, bool isActiveDeckIntroductionObj = true)
	{
		_onOpen = onOpen;
		_onClose = onClose;
		string text = "Detail";
		if (_cardDetailObj == null)
		{
			_cardDetailObj = NGUITools.AddChild(base.gameObject, _cardDetailPrefab);
			_cardDetail = _cardDetailObj.GetComponent<CardDetailUI>();
			_cardDetail.Initialize(LayerMask.NameToLayer(text), CardMaster.CardMasterId.Default);
			_cardDetailObj.SetActive(value: false);
		}
		if (_deckViewObj == null)
		{
			_deckViewObj = UnityEngine.Object.Instantiate(_deckViewPrefab);
			_uiCardList = _deckViewObj.GetComponent<UICardList>();
			_uiCardList.Init(base.gameObject, _cardDetail, null, CloseDeckView, text, in_DetailCameraUse: true, null, 40);
			_deckViewObj.SetActive(value: false);
		}
		_uiCardList.SetEnableRedButton(isActiveRedButton);
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(CardLoadCoroutine(deck, !isActiveDeckIntroductionObj, isActiveDeckIntroductionObj));
	}

	public void CloseDeckView()
	{
		if (IsShowDeckCardView())
		{
			_deckViewObj.SetActive(value: false);
			if (_loadCardAssetList != null)
			{
				Toolbox.ResourcesManager.RemoveAssetGroup(_loadCardAssetList);
				_loadCardAssetList.Clear();
			}
			_onClose.Call();
			_onOpen = null;
			_onClose = null;
		}
	}

	private IEnumerator CardLoadCoroutine(DeckData deck, bool showQRCode, bool isActiveDeckIntroductionObj)
	{
		IList<int> cardIdList = deck.GetCardIdList();
		_uiCardList.RemoveData();
		_loadCardAssetList = _uiCardList.GetLoadFileList(cardIdList as List<int>);
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadCardAssetList, null));
		_uiCardList.SetDeck(deck, null);
		_uiCardList.UpdateShortageRedEther();
		_uiCardList.SetShortageCardVisible(_uiCardList.IsEnableShortageCardVisible);
		if (showQRCode)
		{
			_uiCardList.SetQRSmallTexture();
		}
		_uiCardList.SetActiveDeckIntroductionObj(isActiveDeckIntroductionObj);
		yield return null;
		_deckViewObj.SetActive(value: true);
		UIManager.GetInstance().closeInSceneCenterLoading();
		_onOpen.Call();
	}

	private void CreateConfirmDialogDeleteDeckLog(ChatMessageInfo.DeckLogData deckLogData)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.RedBtn_CancelBtn);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Guild_Chat_0035"));
		dialogBase.SetText(Data.SystemText.Get("Guild_Chat_0036"));
		dialogBase.SetButtonText(Data.SystemText.Get("Guild_Chat_0037"));
		dialogBase.SetPanelDepth(100);
		dialogBase.onPushButton1 = delegate
		{
			DeleteDeckLog(deckLogData.DeckData.Format, deckLogData.MessageId);
		};
	}

	private void DeleteDeckLog(Format format, int messageId)
	{
		CloseDeckView();
		ChatDeleteDeckTask task = new ChatDeleteDeckTask(_chatApiSettings.ApiChatDeleteDeck);
		task.SetParameter(Data.FormatConvertApi(format), messageId);
		_chatConnectCtr.StartConnectCommon(task, delegate
		{
			CreateCompleteDialogDeleteDeckLog();
			_actionCloseChatLogContent.Call(messageId);
			_onDeleteDeckSuccess.Call(format, task.DictDeckLogList);
		});
	}

	private void CreateCompleteDialogDeleteDeckLog()
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetText(Data.SystemText.Get("Guild_Chat_0038"));
		dialogBase.SetPanelDepth(100);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
	}
}
