using System.Collections;
using Wizard.Battle.Phase;
using Wizard.DeckCardEdit;

namespace Wizard;

public static class FreeAndRankMatchDeckSelectConfirmDialog
{
	public static void Create(DialogBase dialogDeckList, DeckData deck, bool isBattleEnd)
	{
		if (!deck.IsUsable())
		{
			InCompleteDeckDecideDialog.Create(dialogDeckList, deck, null, canUseNonPossessionCard: false, ChangeViewSceneAndSetBattleRetry);
			return;
		}
		CompleteDeckDecideDialog.CreateForSingleDeck(dialogDeckList, deck, showSimpleStageOption: true, delegate
		{
			DecideDeck(deck, isBattleEnd);
		}).DecisionUI.CardListCustomize = delegate(UICardList uiCardList)
		{
			if (!deck.IsRentalDeck)
			{
				uiCardList.SetEnableBlueButton(isEnable: true, Data.SystemText.Get("Card_0007"), delegate
				{
					GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
					DeckCardEditUI.SetDeckEditParameter(deck, null);
					ChangeViewSceneAndSetBattleRetry(UIManager.ViewScene.DeckCardEdit);
				});
			}
		};
	}

	public static void DecideDeck(DeckData deck, bool isBattleEnd, bool notBlack = false, bool notCollider = false)
	{
		DeckListUtility.DataMgrSaveLastSelectDeckData(deck);
		ToolboxGame.UIManager.createInSceneLoadingMatching(notBlack, notCollider);
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
		UIManager.GetInstance().StartCoroutine(ChangeMatchingScene(isBattleEnd));
	}

	private static IEnumerator ChangeMatchingScene(bool isBattleEnd)
	{
		yield return UIManager.GetInstance().StartCoroutine(MasterResetMonthTask.MasterReset());
		if (isBattleEnd)
		{
			if (BattleManagerBase.GetIns() != null && BattleManagerBase.GetIns().GetCurrentPhase() is MainPhase)
			{
				LocalLog.AccumulateTraceLog("Move Matching From deckSelectEnd");
			}
			else
			{
				GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.RankMatch);
			}
		}
		else
		{
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.RankMatch);
		}
	}

	private static void ChangeViewSceneAndSetBattleRetry(UIManager.ViewScene viewScene)
	{
		if (UIManager.GetInstance().IsCurrentScene(UIManager.ViewScene.Battle))
		{
			GameMgr.GetIns().GetBattleCtrl().BattleEnd(viewScene, SetBattleRetryForDeckCardEdit);
			return;
		}
		UIManager.ChangeViewSceneParam changeViewSceneParam = new UIManager.ChangeViewSceneParam();
		changeViewSceneParam.OnChange = SetBattleRetryForDeckCardEdit;
		UIManager.GetInstance().ChangeViewScene(viewScene, changeViewSceneParam);
	}

	private static void SetBattleRetryForDeckCardEdit()
	{
		UIManager instance = UIManager.GetInstance();
		if (instance.IsCurrentScene(UIManager.ViewScene.DeckCardEdit))
		{
			(instance.GetUiBaseOfCurrentScene() as DeckCardEditUI).IsBattleRetry = true;
		}
	}
}
