using System;
using System.Collections;
using Cute;
using UnityEngine;
using Wizard;

public class DeckIntroductionItem : MonoBehaviour
{
	private const int WIDTH_DECK_NAME_LABEL_NORMAL = 284;

	private const int WIDTH_DECK_NAME_LABEL_USE_SUBCLASS = 242;

	[SerializeField]
	private CostCurveUI _costCurve;

	[SerializeField]
	private UILabel _deckName;

	[SerializeField]
	private UISprite _formatIcon;

	[SerializeField]
	private ClassInfoParts _classInfoParts;

	[SerializeField]
	private UILabel _playerName;

	[SerializeField]
	private UILabel _deckDetail;

	[SerializeField]
	private UILabel _followerCount;

	[SerializeField]
	private UILabel _amuletCount;

	[SerializeField]
	private UILabel _spellCount;

	[SerializeField]
	private UITexture _cardTexture;

	[SerializeField]
	private UIButton _button;

	[SerializeField]
	private TweenScale _buttonAnimationTween;

	private DeckIntroductionTask.IntroductionData _data;

	public Action<DeckIntroductionTask.IntroductionData> OnClick { get; set; }

	public void Initialize(DeckIntroductionTask.IntroductionData data)
	{
		IFormatBehavior defaultBehaviour = FormatBehaviorManager.GetDefaultBehaviour(data.Deck.Format);
		_data = data;
		_playerName.text = data.PlayerName;
		_deckDetail.text = data.Detail;
		_formatIcon.spriteName = defaultBehaviour.SmallIconSpriteName;
		if (data.Deck.Format == Format.Rotation)
		{
			_formatIcon.spriteName = "icon_rotation_s";
		}
		_deckName.text = data.Deck.GetDeckName();
		_deckName.width = (defaultBehaviour.UseSubClass ? 242 : 284);
		_classInfoParts.gameObject.SetActive(defaultBehaviour.UseSubClass);
		if (defaultBehaviour.UseSubClass)
		{
			_classInfoParts.InitByCharaPrm(GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(data.Deck.GetDeckClassID()));
			_classInfoParts.SetSubClass((CardBasePrm.ClanType)data.Deck.GetDeckSubClassID());
		}
		int followerCount = 0;
		int spellCount = 0;
		int amuletCount = 0;
		GetCardCountEachType(data.Deck, out followerCount, out spellCount, out amuletCount);
		_followerCount.text = followerCount.ToString();
		_spellCount.text = spellCount.ToString();
		_amuletCount.text = amuletCount.ToString();
		_costCurve.Initialize(defaultBehaviour.CardMasterId);
		_costCurve.Refresh(data.Deck.GetCardIdList().ToArray());
		_button.onClick.Clear();
		_button.onClick.Add(new EventDelegate(delegate
		{
			OnClickButton();
		}));
		string battleLogTexturePath = GetBattleLogTexturePath(data.TopCardId);
		_cardTexture.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(battleLogTexturePath);
	}

	private void Start()
	{
		UIEventListener uIEventListener = UIEventListener.Get(_button.gameObject);
		uIEventListener.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uIEventListener.onPress, (UIEventListener.BoolDelegate)delegate
		{
			StartCoroutine(ButtonAnimation());
		});
	}

	private IEnumerator ButtonAnimation()
	{
		BoxCollider btn = _button.GetComponent<BoxCollider>();
		Vector3 to = _buttonAnimationTween.to;
		Vector3 defaultSize = btn.size;
		Vector3 size = new Vector3(btn.size.x / to.x, btn.size.y / to.y, btn.size.z / to.z);
		if ((bool)_buttonAnimationTween)
		{
			_buttonAnimationTween.PlayForward();
			btn.size = size;
			while (Input.GetMouseButton(0))
			{
				yield return null;
			}
			_buttonAnimationTween.PlayReverse();
			btn.size = defaultSize;
		}
	}

	public static string GetCardMaterialPath(int cardId)
	{
		CardMaster instance = CardMaster.GetInstance(CardMaster.CardMasterId.Default);
		CardParameter cardParameterFromId = instance.GetCardParameterFromId(cardId);
		CardParameter cardParameterFromId2 = instance.GetCardParameterFromId(cardParameterFromId.NormalCardId);
		ResourcesManager.AssetLoadPathType type = ResourcesManager.AssetLoadPathType.UnitCardMaterial;
		if (instance.GetCardParameterFromId(cardId).CharType != CardBasePrm.CharaType.NORMAL && !CardMaster.IsMutationCardCheck(instance.GetCardParameterFromId(cardId).BaseCardId))
		{
			type = ResourcesManager.AssetLoadPathType.SpellCardMaterial;
		}
		return Toolbox.ResourcesManager.GetAssetTypePath(cardParameterFromId2.ResourceCardId.ToString(), type);
	}

	private static string GetBattleLogTexturePath(int cardId)
	{
		CardMaster instance = CardMaster.GetInstance(CardMaster.CardMasterId.Default);
		ResourcesManager.AssetLoadPathType assetLoadPathType = ResourcesManager.AssetLoadPathType.UnitHeader;
		string path = instance.GetCardParameterFromId(cardId).ResourceCardId + "0";
		assetLoadPathType = ((instance.GetCardParameterFromId(cardId).CharType != CardBasePrm.CharaType.NORMAL && !CardMaster.IsMutationCardCheck(instance.GetCardParameterFromId(cardId).BaseCardId)) ? ResourcesManager.AssetLoadPathType.OtherHeader : ResourcesManager.AssetLoadPathType.UnitHeader);
		return Toolbox.ResourcesManager.GetAssetTypePath(path, assetLoadPathType, isfetch: true);
	}

	private void GetCardCountEachType(DeckData deck, out int followerCount, out int spellCount, out int amuletCount)
	{
		CardMaster instance = CardMaster.GetInstance(CardMaster.CardMasterId.Default);
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		foreach (int cardId in deck.GetCardIdList())
		{
			switch (instance.GetCardParameterFromId(cardId).CharType)
			{
			case CardBasePrm.CharaType.NORMAL:
				num++;
				break;
			case CardBasePrm.CharaType.SPELL:
				num2++;
				break;
			case CardBasePrm.CharaType.FIELD:
			case CardBasePrm.CharaType.CHANT_FIELD:
				num3++;
				break;
			}
		}
		followerCount = num;
		spellCount = num2;
		amuletCount = num3;
	}

	private void OnClickButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		OnClick.Call(_data);
	}
}
