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

namespace Wizard;

public class SecretBossDeckConfirmDialog : MonoBehaviour
{
	private const int DIALOG_PANEL_DEPTH = 50;

	private const float GRID_CARD_SCALE = 0.97f;

	private const int GRID_CARD_DEPTH = 5;

	public const string LAYER_NAME = "MyPage";

	[SerializeField]
	private UIGrid _cardGrid;

	[SerializeField]
	private CardImageHelpder _cardLoader;

	[SerializeField]
	private DeckViewHelper _deckViewHelper;

	[SerializeField]
	private UILabel _deckNameLabel;

	[SerializeField]
	private UIButton _buttonOriginal;

	private List<string> _loadPathList = new List<string>();

	private DeckData _deck;

	private List<BossRushLobbyAbilityData> _abilityList;

	private bool _isBattleAgain;

	public static void Create(DeckData deck, List<BossRushLobbyAbilityData> abilityList, bool isBattleAgain)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Common_0021"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_GrayBtn);
		dialogBase.SetButtonText(Data.SystemText.Get("Common_0004"), Data.SystemText.Get("Card_0210"));
		dialogBase.ClickSe_Btn2 = Se.TYPE.SYS_BTN_DECIDE;
		dialogBase.isNotCloseWindowButton2 = true;
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/BossRush/SecretBossDeckConfirmDialog")) as GameObject;
		dialogBase.SetObj(gameObject);
		dialogBase.SetLayer("MyPage");
		dialogBase.SetPanelDepth(50);
		gameObject.GetComponent<SecretBossDeckConfirmDialog>().Initialize(dialogBase, deck, abilityList, isBattleAgain);
	}

	private void Initialize(DialogBase dialog, DeckData deck, List<BossRushLobbyAbilityData> abilityList, bool isBattleAgain)
	{
		_deck = deck;
		_abilityList = abilityList;
		_isBattleAgain = isBattleAgain;
		CardMaster instance = CardMaster.GetInstance(CardMaster.CardMasterId.Default);
		List<int> cardIdLIst = new List<int>();
		foreach (BossRushLobbyAbilityData ability in abilityList)
		{
			CardParameter cardParameterFromId = instance.GetCardParameterFromId(ability.DisplayCardId);
			if (ability.IsFoil)
			{
				cardIdLIst.Add(cardParameterFromId.FoilCardId);
			}
			else
			{
				cardIdLIst.Add(ability.DisplayCardId);
			}
		}
		_deckNameLabel.text = deck.GetDeckName();
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadResources(cardIdLIst, delegate
		{
			UIManager.GetInstance().closeInSceneCenterLoading();
			InitializeCardList(cardIdLIst);
			InitializeDeckView();
			dialog.ClickSe_Btn1 = Se.TYPE.SYS_BTN_DECIDE_TRANS;
			dialog.onPushButton1 = delegate
			{
				OnClickOkButton();
			};
			dialog.onPushButton2 = delegate
			{
				OnClickDeckViewButton();
			};
		}));
	}

	private void InitializeDeckView()
	{
		_deckViewHelper.Initialize();
		_deckViewHelper.UICardList.SetEnableBlueButton(isEnable: true, Data.SystemText.Get("BossRush_0038"), delegate
		{
			OnClickAbilityView();
		});
		_deckViewHelper.SetEnableDeckShareButton(enable: false);
	}

	private void OnClickAbilityView()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		BossRushLobbyAbilityDetailDialog.Create(_abilityList, _abilityList.Count);
	}

	private void OnClickDeckViewButton()
	{
		_deckViewHelper.ShowDeckView(_deck);
	}

	private IEnumerator LoadResources(List<int> cardNoList, Action onFinish)
	{
		if (_isBattleAgain)
		{
			_loadPathList.Add(UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.BossRush));
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadPathList, null));
			UIManager.GetInstance().AddResidentAtlas(UIAtlasManager.AssetBundleNames.BossRush);
		}
		float cardScale = 0.97f;
		int depth = 5;
		bool finish = false;
		_cardLoader.Load(cardNoList, cardScale, depth, CardTemplateCustomize, delegate
		{
			finish = true;
		});
		while (!finish)
		{
			yield return null;
		}
		onFinish.Call();
	}

	private void OnDestroy()
	{
		if (_isBattleAgain)
		{
			UIManager.GetInstance().RemoveResidentAtlas(UIAtlasManager.AssetBundleNames.BossRush);
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadPathList);
		_loadPathList.Clear();
	}

	private void CardTemplateCustomize(CardListTemplate cardTemplate)
	{
		cardTemplate.SetBossRushSkillFrame();
		cardTemplate.HideNum();
		cardTemplate.HideLabelsForBossRushSkill();
		cardTemplate.SetBossRushCardTexture();
	}

	private void InitializeCardList(List<int> cardNoList)
	{
		for (int i = 0; i < cardNoList.Count; i++)
		{
			BossRushLobbyAbilityData ability = _abilityList[i];
			UIButton component = NGUITools.AddChild(_cardGrid.gameObject, _buttonOriginal.gameObject).GetComponent<UIButton>();
			UIEventListener.Get(component.gameObject).onClick = delegate
			{
				OnClickCard(ability);
			};
			GameObject cardObj = _cardLoader.GetCardObjData(i).CardObj;
			Vector3 localScale = cardObj.transform.localScale;
			cardObj.transform.parent = component.transform;
			cardObj.transform.localPosition = Vector3.zero;
			cardObj.transform.localScale = localScale;
		}
		_cardGrid.Reposition();
	}

	private void OnClickCard(BossRushLobbyAbilityData selectAbility)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		BossRushLobbyAbilityDetailDialog.Create(_abilityList, _abilityList.Count, selectAbility);
	}

	private void OnClickOkButton()
	{
		BossRushHiddenBattleStartTask battleStartTask = new BossRushHiddenBattleStartTask(_deck, _abilityList);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(battleStartTask, delegate
		{
			MoveBattle(battleStartTask);
		}));
	}

	private void MoveBattle(BossRushHiddenBattleStartTask battleStartTask)
	{
		StoryAISettingData settingData = Data.Master.QuestAISettingList.GetSettingData(battleStartTask.QuestBossData.AI);
		GameMgr.GetIns().GetSoundMgr().StopAllBGM(0.5f);
		UIManager.GetInstance().CreatFadeClose(delegate
		{
			BattleCleanIfNeed(delegate
			{
				Data.Master.LoadAICsv(new AICsvLoadingInfo(settingData.DeckId, settingData.StyleId, settingData.EmoteId), delegate
				{
					DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
					dataMgr.SetCurrentEnemyDeckDataFromAIDeck(dataMgr.GetEnemyClassId(), -1, settingData.LogicLevel, battleStartTask.QuestBossData.Life, settingData.DeckId, settingData.StyleId, settingData.EmoteId, settingData.UseInnerEmote, settingData.EnemyAiId, battleStartTask.PlayerSkillList.Select((BossRushSpecialSkill s) => s.OriginalCardId).ToList());
					dataMgr.LoadEnemy();
					UIManager.ChangeViewSceneParam param = new UIManager.ChangeViewSceneParam
					{
						IsFadeout = false,
						IsShow_CardIntroduction = true
					};
					UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Battle, param);
				});
			});
		});
	}

	private void BattleCleanIfNeed(Action onFinish)
	{
		if (!_isBattleAgain)
		{
			onFinish.Call();
			return;
		}
		UIManager.GetInstance().StartCoroutine(BattleManagerBase.GetIns().GetBattleControl().BattleEnd(delegate
		{
			onFinish();
		}));
	}
}
