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

namespace Wizard;

public class ChatSendDeckUI : MonoBehaviour, IChatActionUI
{
	private const int DEPTH_DECK_SHARE_CONFIRM_DIALOG = 15;

	[SerializeField]
	private UIButton _buttonDeckShare;

	[SerializeField]
	private DeckDecisionUI _prefabDeckDecisionUI;

	private DeckSelectUIDialog _deckSelectUIDialog;

	private IChatApiSettings _chatApiSettings;

	private ChatConnectController _chatConnectController;

	private Action _eventOnSendDeckSuccess;

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatApiSettings = chatSettings.ApiSettings;
		_chatConnectController = chatConnectController;
		_eventOnSendDeckSuccess = actionAddNewChatLogAfterSendChat;
		_buttonDeckShare.onClick.Clear();
		_buttonDeckShare.onClick.Add(new EventDelegate(delegate
		{
			OnClickDeckShareButton();
		}));
	}

	private void OnClickDeckShareButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		DeckMyListTask task = new DeckMyListTask();
		task.SetParameter(Format.All);
		_chatConnectController.StartConnectCommon(task, delegate
		{
			List<DeckGroup> deckGroupList = task.DeckGroupListData.DeckGroupList.Where((DeckGroup deckGroup) => deckGroup.AttributeType == DeckAttributeType.CustomDeck).ToList();
			Format format = (Format)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.LAST_SELECT_DECK_FORMAT);
			if (format == Format.Avatar)
			{
				format = Format.Rotation;
			}
			DeckSelectUIDialog.eFormatChangeUIType formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.NormalFormatOnly;
			if (Prerelease.Status != Prerelease.eStatus.NONE)
			{
				formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithPrerotation;
			}
			else if (DeckListUtility.DeckGroupDataBaseClone().Any((DeckGroup deckgroup) => deckgroup.DeckFormat == Format.Crossover))
			{
				formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithCrossover;
			}
			else if (Data.MyRotationAllInfo.IsMyRotationEnable)
			{
				formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithMyRotation;
			}
			_deckSelectUIDialog = DeckSelectUIDialog.Create(Data.SystemText.Get("Guild_Chat_0011"), new DeckGroupListData(deckGroupList), format, formatChangeUIType, isVisibleCreateNew: false, delegate(DialogBase dialogDeckList, DeckData deckData)
			{
				CreateDeckSelectConfirmDialog(deckData);
			});
		});
	}

	private void CreateDeckSelectConfirmDialog(DeckData deckData)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetButtonText(systemText.Get("Common_0004"), systemText.Get("Card_0083"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_GrayBtn);
		dialogBase.SetPanelDepth(15);
		DeckDecisionUI deckDecisionUI = UnityEngine.Object.Instantiate(_prefabDeckDecisionUI);
		deckDecisionUI.SetText(systemText.Get("Guild_Chat_0012"), deckData.GetDeckName());
		deckDecisionUI.SetDeckData(deckData, null);
		dialogBase.SetObj(deckDecisionUI.gameObject);
		dialogBase.onPushButton2 = deckDecisionUI.OnClickCreateCardList;
		dialogBase.ClickSe_Btn2 = Se.TYPE.SYS_BTN_DECIDE;
		dialogBase.isNotCloseWindowButton2 = true;
		deckDecisionUI.DeckName = deckData.GetDeckName();
		dialogBase.onPushButton1 = delegate
		{
			OnDecideDeck(deckData);
			_deckSelectUIDialog.Close();
		};
	}

	private void OnDecideDeck(DeckData deckData)
	{
		ChatAddDeckTask chatAddDeckTask = new ChatAddDeckTask(_chatApiSettings.ApiChatAddDeck);
		int deckFormat = Data.FormatConvertApi(deckData.Format);
		int deckID = deckData.GetDeckID();
		chatAddDeckTask.SetParameter(deckFormat, deckID);
		_chatConnectController.StartConnectCommon(chatAddDeckTask, delegate
		{
			_eventOnSendDeckSuccess.Call();
		});
	}
}
