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

namespace Wizard;

public class MultiDeckSelectDialog : MonoBehaviour
{
	private int _deckCount;

	private List<DeckData> _deckList;

	private DeckSelectUIDialog _deckSelectDialog;

	private int _deckIndex;

	private const int CARD_VIEW_DEPTH_OFFSET = 800;

	public Action OnClose;

	public Action<List<DeckData>> OnDecide { get; set; }

	public static MultiDeckSelectDialog Create(int deckCount, Format format, string deckListHeader)
	{
		MultiDeckSelectDialog multiDeckSelectDialog = new GameObject().AddComponent<MultiDeckSelectDialog>();
		multiDeckSelectDialog.Initialize(deckCount, format, deckListHeader);
		return multiDeckSelectDialog;
	}

	private void Initialize(int deckCount, Format format, string deckListHeader)
	{
		_deckCount = deckCount;
		_deckList = new List<DeckData>();
		for (int i = 0; i < deckCount; i++)
		{
			_deckList.Add(null);
		}
		DeckInfoTask task = new DeckInfoTask();
		task.SetParameter(format);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			_deckSelectDialog = DeckSelectUIDialog.Create(deckListHeader, task.DeckGroupListData, format, DeckSelectUIDialog.eFormatChangeUIType.SingleFormat, isVisibleCreateNew: false, OnSelectDeck, new DeckSelectUI.InitOptions
			{
				OnUpdateDeckUICustomize = OnUpDateDeckUICustomize
			});
		}));
	}

	private void OnSelectDeck(DialogBase dialogDeckList, DeckData deck)
	{
		int num = SearchDeckIndex(deck);
		if (num >= 0)
		{
			_deckList[num] = null;
			for (int i = num + 1; i < _deckList.Count; i++)
			{
				_deckList[i - 1] = _deckList[i];
				_deckList[i] = null;
			}
			_deckIndex--;
		}
		else
		{
			_deckList[_deckIndex] = deck;
			_deckIndex++;
			if (_deckIndex >= _deckCount)
			{
				string text = Data.SystemText.Get("RoomBattle_0091");
				text += "\n\n";
				CompleteDeckDecideDialog completeDeckDecideDialog = CompleteDeckDecideDialog.CreateForMultiDeck(dialogDeckList, text, showSimpleStageOption: false, OnDecideDeckList, OnCancelDeckList);
				DeckDecisionUI decisionUI = completeDeckDecideDialog.DecisionUI;
				decisionUI.OptionLabel.text = Data.SystemText.Get("Gathering_0027");
				if (_deckCount == 1)
				{
					DialogBase dialog = completeDeckDecideDialog.Dialog;
					dialog.SetButtonText(null, Data.SystemText.Get("Card_0083"));
					dialog.onPushButton2 = decisionUI.OnClickCreateCardList;
					dialog.ClickSe_Btn2 = Se.TYPE.SYS_BTN_DECIDE;
					dialog.isNotCloseWindowButton2 = true;
					decisionUI.SetDeckData(deck, null);
					decisionUI.DeckName = deck.GetDeckName();
					decisionUI.CardListCustomize = delegate(UICardList uiCardList)
					{
						uiCardList.SetPanelDepthOffset(800);
						UIPanel[] componentsInChildren = uiCardList.CardDetail.gameObject.GetComponentsInChildren<UIPanel>(includeInactive: true);
						for (int j = 0; j < componentsInChildren.Length; j++)
						{
							componentsInChildren[j].depth += 800;
						}
					};
				}
			}
		}
		_deckSelectDialog.UpdateAllDeckUICurrentPage();
	}

	private void OnDecideDeckList()
	{
		OnDecide.Call(_deckList);
		UnityEngine.Object.Destroy(base.gameObject);
		OnClose.Call();
	}

	private void OnCancelDeckList()
	{
		for (int i = 0; i < _deckList.Count; i++)
		{
			_deckList[i] = null;
		}
		_deckIndex = 0;
		_deckSelectDialog.UpdateAllDeckUICurrentPage();
	}

	private static bool IsSameDeck(DeckData deck1, DeckData deck2)
	{
		if (deck1.IsDefaultDeck() != deck2.IsDefaultDeck())
		{
			return false;
		}
		return deck1.GetDeckID() == deck2.GetDeckID();
	}

	private int SearchDeckIndex(DeckData deck)
	{
		return _deckList.FindIndex((DeckData tempDeck) => tempDeck != null && IsSameDeck(tempDeck, deck));
	}

	private void OnUpDateDeckUICustomize(DeckUI deckUI)
	{
		if (deckUI.ViewType == DeckUI.eViewType.CreateNew || deckUI.ViewType == DeckUI.eViewType.Empty)
		{
			return;
		}
		if (_deckIndex >= _deckCount)
		{
			deckUI.SetSelectable(isSelectable: false);
			return;
		}
		DeckData deck = deckUI.Deck;
		if (!deck.IsUsable())
		{
			deckUI.SetSelectable(isSelectable: false);
		}
		else if (_deckCount > 1)
		{
			int num = SearchDeckIndex(deck);
			if (num >= 0)
			{
				deckUI.SetTextCenterLabe(Data.SystemText.Get("RoomBattle_0084", (num + 1).ToString()));
				deckUI.SetSelectable(isSelectable: true);
				deckUI.SetColorToGrey(isGrey: true);
			}
			else if (IsSelectedClass(deck))
			{
				deckUI.SetTextCenterLabe(Data.SystemText.Get("RoomBattle_0085"));
				deckUI.SetSelectable(isSelectable: false);
			}
		}
	}

	private bool IsSelectedClass(DeckData deck)
	{
		List<int> list = new List<int>();
		bool useSubClass = FormatBehaviorManager.GetDefaultBehaviour(deck.Format).UseSubClass;
		foreach (DeckData deck2 in _deckList)
		{
			if (deck2 != null)
			{
				list.Add(deck2.GetDeckClassID());
				if (useSubClass)
				{
					list.Add(deck2.GetDeckSubClassID());
				}
			}
		}
		if (list.Contains(deck.GetDeckClassID()))
		{
			return true;
		}
		if (useSubClass && list.Contains(deck.GetDeckSubClassID()))
		{
			return true;
		}
		return false;
	}
}
