using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard.Battle.View;

namespace Wizard;

public class BossRushLobbyAbilityDetailDialog : MonoBehaviour
{
	private const float SELECT_CARD_SCALE = 1.35f;

	private const float GRID_CARD_SCALE = 0.64f;

	private const int GRID_CARD_DEPTH = 5;

	private const int KEYWORD_DIALOG_DEPTH = 20;

	private const int KEYWORD_PANEL_DEPTH = 25;

	private static readonly Vector3 DIALOG_POSITION = new Vector3(0f, 0f, 2f);

	[SerializeField]
	private CardImageHelpder _cardLoader;

	[SerializeField]
	private UIButton _buttonOriginal;

	[SerializeField]
	private UIGrid _cardGrid;

	[SerializeField]
	private UILabel _nameLabel;

	[SerializeField]
	private UIScrollView _descScrollView;

	[SerializeField]
	private UILabel _descLabel;

	[SerializeField]
	private GameObject _selectCardRoot;

	[SerializeField]
	private GameObject _selectCursor;

	[SerializeField]
	private TextLineCreater _descLineCreator;

	[SerializeField]
	private GameObject _emptyOriginal;

	[SerializeField]
	private GameObject _keywordCollider;

	private GameObject _selectCard;

	public static void Create(List<BossRushLobbyAbilityData> abilityList, int maxBossCount, BossRushLobbyAbilityData defaultSelectAbility = null)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.L);
		dialogBase.SetTitleLabel(Data.SystemText.Get("BossRush_0022"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialogBase.transform.localPosition = DIALOG_POSITION;
		if (abilityList.Count == 0)
		{
			dialogBase.SetText(Data.SystemText.Get("BossRush_0024"));
			return;
		}
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/BossRush/BossRushLobbyAbilityDetailDialog")) as GameObject;
		dialogBase.SetObj(gameObject);
		gameObject.GetComponent<BossRushLobbyAbilityDetailDialog>().Initialize(abilityList, maxBossCount, defaultSelectAbility);
	}

	private void Initialize(List<BossRushLobbyAbilityData> abilityList, int maxBossCount, BossRushLobbyAbilityData defaultSelectAbility)
	{
		_emptyOriginal.SetActive(value: false);
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadResources(abilityList, delegate
		{
			InitializeCardList(abilityList, maxBossCount, defaultSelectAbility);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private void InitializeCardList(List<BossRushLobbyAbilityData> abilityList, int maxBossCount, BossRushLobbyAbilityData defaultSelectAbility)
	{
		_buttonOriginal.gameObject.SetActive(value: false);
		int num = 0;
		if (defaultSelectAbility != null)
		{
			for (int i = 0; i < abilityList.Count; i++)
			{
				if (abilityList[i] == defaultSelectAbility)
				{
					num = i;
					break;
				}
			}
		}
		for (int j = 0; j < maxBossCount; j++)
		{
			if (j >= abilityList.Count)
			{
				AddEmptyAbilityFrame();
			}
			else
			{
				AddAbilityCardObj(abilityList, j, num);
			}
		}
		_cardGrid.Reposition();
		UIBase_CardManager.CardObjData cardObjData = _cardLoader.GetCardObjData(num);
		_selectCursor.transform.position = cardObjData.CardObj.transform.position;
	}

	private void AddEmptyAbilityFrame()
	{
		NGUITools.AddChild(_cardGrid.gameObject, _emptyOriginal).SetActive(value: true);
	}

	private void AddAbilityCardObj(List<BossRushLobbyAbilityData> abilityList, int index, int defaultSelectIndex)
	{
		GameObject gameObject = NGUITools.AddChild(_cardGrid.gameObject, _buttonOriginal.gameObject);
		UIButton component = gameObject.GetComponent<UIButton>();
		gameObject.SetActive(value: true);
		BossRushLobbyAbilityData abilityData = abilityList[index];
		UIBase_CardManager.CardObjData objData = _cardLoader.GetCardObjData(index);
		GameObject cardObj = objData.CardObj;
		Vector3 localScale = cardObj.transform.localScale;
		component.onClick.Add(new EventDelegate(delegate
		{
			OnClickAbility(abilityData, objData);
		}));
		cardObj.transform.parent = gameObject.transform;
		cardObj.transform.localPosition = Vector3.zero;
		cardObj.transform.localScale = localScale;
		if (index == defaultSelectIndex)
		{
			SelectAbility(abilityData, objData);
		}
	}

	private void OnClickAbility(BossRushLobbyAbilityData abilityData, UIBase_CardManager.CardObjData objData)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		SelectAbility(abilityData, objData);
	}

	private void SelectAbility(BossRushLobbyAbilityData abilityData, UIBase_CardManager.CardObjData objData)
	{
		SetNameLabel(abilityData);
		_descLabel.SetWrapText(BattleCardBase.ConvertSkillDescriptionText(abilityData.SkillDescText));
		if (_selectCard != null)
		{
			UnityEngine.Object.Destroy(_selectCard);
		}
		_selectCursor.transform.position = objData.CardObj.transform.position;
		_selectCard = NGUITools.AddChild(_selectCardRoot, objData.CardObj);
		_selectCard.transform.localScale = new Vector3(1.35f, 1.35f, 1f);
		CardListTemplate component = _selectCard.GetComponent<CardListTemplate>();
		component.SetId(abilityData.DisplayCardId);
		CardTemplateCustomize(component);
		int textLineCount = Global.GetTextLineCount(_descLabel.text);
		_descLineCreator.ShowLines(textLineCount);
		_descScrollView.ResetPosition();
		UIEventListener.Get(_descLabel.gameObject).onClick = delegate
		{
			OnClickDescLabel(abilityData);
		};
		UIEventListener.Get(_keywordCollider).onPress = delegate(GameObject g, bool b)
		{
			BattlePlayerView.PressKeyWordColorChange(_descLabel, b);
		};
		UIEventListener.Get(_keywordCollider).onDragStart = delegate
		{
			BattlePlayerView.SetKeyWordLabelColor(_descLabel);
		};
	}

	private void OnClickDescLabel(BossRushLobbyAbilityData abilityData)
	{
		if (abilityData.SkillDescText.Length > 0 && HasKeyword(abilityData.SkillDescText))
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			IList<string> keywords = BattleKeywordInfoListMgr.GetKeywords(abilityData.SkillDescText);
			DialogBase dialogBase = BattlePlayerView.CreateKeyPanel(_descLabel, keywords, CardMaster.CardMasterId.Default);
			dialogBase.SetPanelDepth(20, isSetBackViewDepthImmediately: true);
			dialogBase.GetComponentInChildren<BattleKeywordInfoListMgr>().SetPanelDepth(25);
		}
	}

	public static bool HasKeyword(string skillDescText)
	{
		IList<string> keywords = BattleKeywordInfoListMgr.GetKeywords(skillDescText);
		bool result = false;
		foreach (string item in keywords)
		{
			if (Data.Master.BattleKeyWordDic.ContainsKey(item))
			{
				result = true;
				break;
			}
		}
		return result;
	}

	private void SetNameLabel(BossRushLobbyAbilityData abilityData)
	{
		_nameLabel.text = abilityData.Name;
		UIManager.GetInstance().getUIBase_CardManager().SetNameLabelStyle(_nameLabel, abilityData.IsFoil);
	}

	private IEnumerator LoadResources(List<BossRushLobbyAbilityData> abilityList, Action callBack)
	{
		List<int> list = new List<int>();
		CardMaster instance = CardMaster.GetInstance(CardMaster.CardMasterId.Default);
		foreach (BossRushLobbyAbilityData ability in abilityList)
		{
			CardParameter cardParameterFromId = instance.GetCardParameterFromId(ability.DisplayCardId);
			if (ability.IsFoil)
			{
				list.Add(cardParameterFromId.FoilCardId);
			}
			else
			{
				list.Add(ability.DisplayCardId);
			}
		}
		float cardScale = 0.64f;
		int depth = 5;
		bool finish = false;
		_cardLoader.Load(list, cardScale, depth, CardTemplateCustomize, delegate
		{
			finish = true;
		});
		while (!finish)
		{
			yield return null;
		}
		callBack.Call();
	}

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