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

namespace Wizard;

public class AvatarAbilityDetailDialog : MonoBehaviour
{
	private const int KEYWORD_DIALOG_DEPTH = 20;

	private const int KEYWORD_PANEL_DEPTH = 25;

	private const int TEXT_COLLIDER_HEIGHT_OFFSET = 22;

	private const float PASSIVE_TEXT_OFFSET = 140f;

	private const int DEFAULT_LABEL_HEIGHT = 60;

	[SerializeField]
	private UITexture _characterTexture;

	[SerializeField]
	private UILabel _characterName;

	[SerializeField]
	private UILabel _characterHBP;

	[SerializeField]
	private UISprite _classIcon;

	[SerializeField]
	private GameObject _itemRoot;

	[SerializeField]
	private UILabel _passiveAbilityDescLabel;

	[SerializeField]
	private List<UILabel> _abilityDescLabel = new List<UILabel>();

	[SerializeField]
	private GameObject _passiveKeyWordCollider;

	[SerializeField]
	private List<GameObject> _keyWordCollider = new List<GameObject>();

	[SerializeField]
	private List<UIWidget> _abilityItemRoot = new List<UIWidget>();

	[SerializeField]
	private UIWidget _passiveAbilityRoot;

	[SerializeField]
	private GameObject _thirdUnderLine;

	[SerializeField]
	private List<UILabel> _costLabel = new List<UILabel>();

	[SerializeField]
	private List<UILabel> _signLabel = new List<UILabel>();

	[SerializeField]
	private UIScrollView _scrollView;

	private DeckData _deckData;

	private AvatarBattleInfo _avatarBattleInfo;

	private static DialogBase _avatarAbilityDialog;

	private static DialogBase _keywordDialog;

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

	public static void Create(AvatarBattleInfo avatarBattleInfo, DeckData deck)
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/AvatarAbilityDetailDialog")) as GameObject;
		string titleLabel = Data.SystemText.Get("HeroesBattle_0006");
		_avatarAbilityDialog = UIManager.GetInstance().CreateDialogClose();
		_avatarAbilityDialog.SetTitleLabel(titleLabel);
		_avatarAbilityDialog.SetSize(DialogBase.Size.L);
		_avatarAbilityDialog.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		_avatarAbilityDialog.SetObj(gameObject);
		gameObject.GetComponent<AvatarAbilityDetailDialog>().Initialize(avatarBattleInfo, deck);
	}

	private void Initialize(AvatarBattleInfo avatarBattleInfo, DeckData deck)
	{
		_deckData = deck;
		_avatarBattleInfo = avatarBattleInfo;
		_itemRoot.SetActive(value: false);
		RoomBase.StartDialogLoading();
		UIManager.GetInstance().createInSceneCenterLoading();
		StartCoroutine(LoadResources(deck.GetSkinId(), delegate
		{
			StartCoroutine(SetResources());
		}));
	}

	private IEnumerator SetResources()
	{
		ClassCharacterMasterData charaPrmByCharaId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(_deckData.GetSkinId());
		InitializeClassCharacter(_deckData.GetSkinId());
		InitializeDescLabel(_avatarBattleInfo);
		_characterName.text = charaPrmByCharaId.chara_name;
		_characterHBP.text = _avatarBattleInfo.Bonus.BattleStartFirstPlayerTurnBp.ToString();
		ClassCharaPrm.SetClassLabelSetting(_characterName, charaPrmByCharaId.clan);
		_classIcon.spriteName = ClassCharaPrm.GetIconSpriteName(charaPrmByCharaId.clan);
		_itemRoot.SetActive(value: true);
		yield return null;
		_scrollView.GetComponent<FlexibleGrid>().Reposition();
		_scrollView.ResetPosition();
		UIManager.GetInstance().closeInSceneCenterLoading();
		RoomBase.FinishDiloagLoading();
	}

	private void InitializeDescLabel(AvatarBattleInfo avatarBattleInfo)
	{
		string allAbilityText = avatarBattleInfo.Bonus.PassiveAbilityDesc;
		string[] abilityDesc = avatarBattleInfo.Bonus.AbilityDesc;
		foreach (string text in abilityDesc)
		{
			allAbilityText += text;
		}
		if (avatarBattleInfo.Bonus.PassiveAbilityDesc == string.Empty)
		{
			_passiveAbilityRoot.gameObject.SetActive(value: false);
			_passiveAbilityRoot.transform.localPosition = new Vector3(0f, 140f, 0f);
		}
		else
		{
			_passiveAbilityDescLabel.SetWrapText(BattleCardBase.ConvertSkillDescriptionText(avatarBattleInfo.Bonus.PassiveAbilityDesc));
			UIEventListener.Get(_passiveAbilityDescLabel.gameObject).onClick = delegate
			{
				OnClickDescLabel(allAbilityText, _passiveAbilityDescLabel);
			};
			UIEventListener.Get(_passiveKeyWordCollider).onPress = delegate(GameObject g, bool b)
			{
				BattlePlayerView.PressKeyWordColorChange(_passiveAbilityDescLabel, b);
			};
			UIEventListener.Get(_passiveKeyWordCollider).onDragStart = delegate
			{
				BattlePlayerView.SetKeyWordLabelColor(_passiveAbilityDescLabel);
			};
			_passiveAbilityRoot.height = _passiveAbilityDescLabel.height;
			_passiveAbilityDescLabel.GetComponent<BoxCollider>().size = new Vector3(_passiveAbilityRoot.localSize.x, _passiveAbilityRoot.localSize.y + 22f, 0f);
		}
		if (avatarBattleInfo.Bonus.AbilityCosts.Length < 3 || avatarBattleInfo.Bonus.AbilityDesc.Length < 3)
		{
			_abilityItemRoot[2].gameObject.SetActive(value: false);
			_thirdUnderLine.SetActive(value: false);
		}
		for (int num = 0; num < avatarBattleInfo.Bonus.AbilityDesc.Length; num++)
		{
			int textNum = num;
			_abilityDescLabel[textNum].SetWrapText(BattleCardBase.ConvertSkillDescriptionText(avatarBattleInfo.Bonus.AbilityDesc[textNum]));
			UIEventListener.Get(_abilityDescLabel[textNum].gameObject).onClick = delegate
			{
				OnClickDescLabel(allAbilityText, _abilityDescLabel[textNum]);
			};
			UIEventListener.Get(_keyWordCollider[textNum]).onPress = delegate(GameObject g, bool b)
			{
				BattlePlayerView.PressKeyWordColorChange(_abilityDescLabel[textNum], b);
			};
			UIEventListener.Get(_keyWordCollider[textNum]).onDragStart = delegate
			{
				BattlePlayerView.SetKeyWordLabelColor(_abilityDescLabel[textNum]);
			};
			_abilityItemRoot[textNum].height = 60;
			if (_abilityItemRoot[textNum].height < _abilityDescLabel[textNum].height)
			{
				_abilityItemRoot[textNum].height = _abilityDescLabel[textNum].height;
			}
			_abilityDescLabel[textNum].GetComponent<BoxCollider>().size = new Vector3(_abilityItemRoot[textNum].localSize.x, _abilityItemRoot[textNum].localSize.y + 22f, 0f);
			_abilityDescLabel[textNum].GetComponent<BoxCollider>().center = new Vector3(-22f, 0f, 0f);
			if (!int.TryParse(avatarBattleInfo.Bonus.AbilityCosts[textNum], out var result))
			{
				_signLabel[textNum].text = "-";
				_costLabel[textNum].text = "X";
			}
			else if (result != 0)
			{
				_signLabel[textNum].text = ((result < 0) ? "-" : "+");
				_costLabel[textNum].text = Mathf.Abs(result).ToString();
			}
			else
			{
				_signLabel[textNum].text = string.Empty;
				_costLabel[textNum].text = "0";
				_costLabel[textNum].transform.localPosition = new Vector3(0f, 2f, 0f);
			}
		}
	}

	private void OnClickDescLabel(string abilityText, UILabel textLabel)
	{
		if (HasKeyword(abilityText))
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			IList<string> keywords = BattleKeywordInfoListMgr.GetKeywords(abilityText);
			_keywordDialog = BattlePlayerView.CreateKeyPanel(textLabel, keywords, CardMaster.CardMasterId.Default);
			_keywordDialog.SetPanelDepth(20, isSetBackViewDepthImmediately: true);
			_keywordDialog.GetComponentInChildren<BattleKeywordInfoListMgr>().SetPanelDepth(25);
		}
	}

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

	private void OnDestroy()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadList);
		_loadList.Clear();
	}

	public static bool LiveDialog()
	{
		return _avatarAbilityDialog != null;
	}

	public static void CloseDialog()
	{
		_avatarAbilityDialog.Close();
		if (_keywordDialog != null)
		{
			_keywordDialog.Close();
		}
	}

	private IEnumerator LoadResources(int skinId, Action callBack)
	{
		_loadList.Add(GetCharacterTexturePath(skinId, isFetch: false));
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadList, null));
		callBack.Call();
	}

	private string GetCharacterTexturePath(int skinId, bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath(skinId.ToString("00"), ResourcesManager.AssetLoadPathType.ClassCharaSkinThumbnail, isFetch);
	}

	private void InitializeClassCharacter(int skinId)
	{
		_characterTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(GetCharacterTexturePath(skinId, isFetch: true)) as Texture;
	}
}
