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

namespace Wizard;

public class CardSleeveRewardView : MonoBehaviour
{
	private const int MAX_VIEW_REWARD = 5;

	private readonly Vector3[] SPECIAL_LAYOUT_POSITION = new Vector3[5]
	{
		new Vector3(-303f, 0f, 0f),
		new Vector3(-122f, 46f, 0f),
		new Vector3(-51f, -51f, 0f),
		new Vector3(127f, 0f, 0f),
		new Vector3(303f, 0f, 0f)
	};

	private readonly Vector3[] SPECIAL_LAYOUT_SCALE = new Vector3[5]
	{
		Vector3.one,
		new Vector3(0.9f, 0.9f, 1f),
		new Vector3(0.9f, 0.9f, 1f),
		Vector3.one,
		Vector3.one
	};

	private readonly int[] LABEL_OVERRIDE_INDEX = new int[2] { 1, 2 };

	private readonly Vector3[] LABEL_OVERRIDE_POSITION = new Vector3[2]
	{
		new Vector3(38f, -181f, 0f),
		new Vector3(-41f, -124f, 0f)
	};

	private readonly int[] LABEL_OVERRIDE_WIDTH = new int[2] { 170, 170 };

	[SerializeField]
	private CardSleeveRewardItem _sleeveItemOriginal;

	[SerializeField]
	private CardSleeveRewardItem _emblemItemOriginal;

	[SerializeField]
	private CardSleeveRewardItem _cardItemOriginal;

	[SerializeField]
	private bool _enableAlreadyGetCheck;

	[SerializeField]
	private bool _isEnablePremiumShader;

	[SerializeField]
	private bool _isEnableCardClickCollider;

	[SerializeField]
	private int _cardDepthOffset;

	[SerializeField]
	private bool _isEnableHaveCardCount = true;

	private List<GameObject> _cloneCardList = new List<GameObject>();

	[SerializeField]
	private GameObject _cardDetailPrefab;

	private static int DetailLayer;

	private CardDetailUI _cardDetail;

	private List<CardSleeveRewardItem> _sleeveItemList = new List<CardSleeveRewardItem>();

	private List<CardSleeveRewardItem> _emblemItemList = new List<CardSleeveRewardItem>();

	private List<CardSleeveRewardItem> _cardItemList = new List<CardSleeveRewardItem>();

	public List<CardSleeveRewardItem> SetRewardItems(List<ShopCommonRewardInfo> infoList, GameObject parent, Dictionary<int, UIBase_CardManager.CardObjData> cardPool)
	{
		List<ShopCommonRewardInfo> viewRewardList = GetViewRewardList(infoList);
		List<ShopCommonRewardInfo> list = SortRewardList(viewRewardList);
		HideAllItems();
		int num = 0;
		int num2 = 0;
		List<CardSleeveRewardItem> list2 = new List<CardSleeveRewardItem>();
		int num3 = 0;
		for (int i = 0; i < list.Count; i++)
		{
			CardSleeveRewardItem cardSleeveRewardItem = null;
			switch ((UserGoods.Type)list[i].Type)
			{
			case UserGoods.Type.Card:
				cardSleeveRewardItem = CreateCard(parent, viewRewardList[i], num3++, cardPool);
				break;
			case UserGoods.Type.Sleeve:
				if (num < _sleeveItemList.Count)
				{
					cardSleeveRewardItem = _sleeveItemList[num];
				}
				else
				{
					cardSleeveRewardItem = NGUITools.AddChild(parent, _sleeveItemOriginal.gameObject).GetComponent<CardSleeveRewardItem>();
					cardSleeveRewardItem.transform.localPosition = new Vector3(cardSleeveRewardItem.transform.localPosition.x, cardSleeveRewardItem.transform.localPosition.y, 10f);
					_sleeveItemList.Add(cardSleeveRewardItem);
				}
				if (_enableAlreadyGetCheck)
				{
					cardSleeveRewardItem.SetArleadyGetVisible(Data.Master.SleeveMgr.Get(list[i].UserGoodsId).IsAcquired);
				}
				num++;
				break;
			case UserGoods.Type.Emblem:
				if (num2 < _emblemItemList.Count)
				{
					cardSleeveRewardItem = _emblemItemList[num2];
				}
				else
				{
					cardSleeveRewardItem = NGUITools.AddChild(parent, _emblemItemOriginal.gameObject).GetComponent<CardSleeveRewardItem>();
					_emblemItemList.Add(cardSleeveRewardItem);
				}
				if (_enableAlreadyGetCheck)
				{
					Emblem emblem = Data.Master.EmblemMgr.Get(list[i].UserGoodsId);
					cardSleeveRewardItem.SetArleadyGetVisible(emblem.IsAcquired);
				}
				num2++;
				break;
			}
			if (cardSleeveRewardItem != null)
			{
				if (list[i].Type != 5)
				{
					cardSleeveRewardItem.gameObject.SetActive(value: true);
				}
				cardSleeveRewardItem.SetReward(list[i]);
				list2.Add(cardSleeveRewardItem);
			}
		}
		return list2;
	}

	public bool IsSpecialLayout(List<ShopCommonRewardInfo> infoList)
	{
		List<ShopCommonRewardInfo> viewRewardList = GetViewRewardList(infoList);
		SortRewardList(viewRewardList);
		int num = viewRewardList.Count((ShopCommonRewardInfo reward) => reward.Type == 6);
		int num2 = viewRewardList.Count((ShopCommonRewardInfo reward) => reward.Type == 7);
		int num3 = viewRewardList.Count((ShopCommonRewardInfo reward) => reward.Type == 5);
		if (num == 1 && num2 == 2)
		{
			return num3 == 2;
		}
		return false;
	}

	public void CheckSpecialLayout(GameObject parent, List<ShopCommonRewardInfo> infoList)
	{
		if (!IsSpecialLayout(infoList))
		{
			return;
		}
		List<ShopCommonRewardInfo> viewRewardList = GetViewRewardList(infoList);
		List<ShopCommonRewardInfo> list = SortRewardList(viewRewardList);
		for (int i = 0; i < SPECIAL_LAYOUT_POSITION.Length; i++)
		{
			Transform child = parent.transform.GetChild(i);
			child.localPosition = SPECIAL_LAYOUT_POSITION[i];
			child.localScale = SPECIAL_LAYOUT_SCALE[i];
		}
		for (int j = 0; j < LABEL_OVERRIDE_INDEX.Length; j++)
		{
			int index = LABEL_OVERRIDE_INDEX[j];
			CardSleeveRewardItem component = parent.transform.GetChild(index).gameObject.GetComponent<CardSleeveRewardItem>();
			if (component != null)
			{
				component.ChangeNameLabelPosition(LABEL_OVERRIDE_POSITION[j]);
				component.ChangeNameLabelWidth(LABEL_OVERRIDE_WIDTH[j]);
				component.SetReward(list[index]);
			}
		}
	}

	private void Start()
	{
		InitializeCardDetail();
	}

	private CardSleeveRewardItem CreateCard(GameObject parent, ShopCommonRewardInfo viewReward, int cardCount, Dictionary<int, UIBase_CardManager.CardObjData> cardPool)
	{
		CardSleeveRewardItem cardSleeveRewardItem;
		if (cardCount < _cardItemList.Count)
		{
			cardSleeveRewardItem = _cardItemList[cardCount];
		}
		else
		{
			cardSleeveRewardItem = NGUITools.AddChild(parent, _cardItemOriginal.gameObject).GetComponent<CardSleeveRewardItem>();
			cardSleeveRewardItem.transform.localPosition = new Vector3(cardSleeveRewardItem.transform.localPosition.x, cardSleeveRewardItem.transform.localPosition.y, 10f);
			_cardItemList.Add(cardSleeveRewardItem);
		}
		cardSleeveRewardItem.gameObject.SetActive(value: true);
		UIBase_CardManager.CardObjData cardObjOriginal = cardPool[(int)viewReward.UserGoodsId];
		GameObject gameObject = NGUITools.AddChild(cardSleeveRewardItem.CardObjectParent, cardObjOriginal.CardObj);
		gameObject.SetActive(value: true);
		_cloneCardList.Add(gameObject);
		CardListTemplate component = gameObject.GetComponent<CardListTemplate>();
		component.AddDepth(_cardDepthOffset);
		if (!_isEnablePremiumShader)
		{
			component.ReplaceMaterialToTexture();
		}
		int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum((int)viewReward.UserGoodsId, isIncludingSpotCard: false);
		if (_isEnableHaveCardCount)
		{
			component.SetNum(viewReward.Num);
		}
		else
		{
			component.HideNum();
		}
		component._newLabel.gameObject.SetActive(value: false);
		if (_enableAlreadyGetCheck)
		{
			bool flag = possessionCardNum > 0;
			cardSleeveRewardItem.SetArleadyGetVisible(flag);
			UIManager.SetObjectToGrey(gameObject, flag);
			component._frameSprite.depth = component._frameSprite.depth + 1;
		}
		component.RotationOnlyIconVisible = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(cardObjOriginal.ids).IsResurgentCard;
		if (_isEnableCardClickCollider)
		{
			GameObject obj = component._frameSprite.gameObject;
			obj.AddComponent<BoxCollider>().size = component._frameSprite.localSize;
			UIEventListener uIEventListener = UIEventListener.Get(obj);
			uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, (UIEventListener.VoidDelegate)delegate
			{
				if (_cardDetail != null)
				{
					GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_CARD_INFO);
					_cardDetail.IsShowFlavorTextButton = false;
					_cardDetail.IsShowEvolutionButton = false;
					_cardDetail.IsShowVoiceButton = false;
					_cardDetail.gameObject.SetActive(value: true);
					_cardDetail.ShowCardDetail(cardObjOriginal.CardObj);
				}
			});
		}
		return cardSleeveRewardItem;
	}

	private void InitializeCardDetail()
	{
		if (!(_cardDetailPrefab == null))
		{
			DetailLayer = LayerMask.NameToLayer("Detail");
			_cardDetail = UnityEngine.Object.Instantiate(_cardDetailPrefab).GetComponent<CardDetailUI>();
			_cardDetail.transform.parent = base.transform;
			_cardDetail.transform.localPosition = Vector3.zero;
			_cardDetail.transform.localScale = Vector3.one;
			_cardDetail.Initialize(DetailLayer, CardMaster.CardMasterId.Default);
			_cardDetail.IsShowFlavorTextButton = true;
			_cardDetail.IsShowVoiceButton = true;
			_cardDetail.IsShowEvolutionButton = true;
			_cardDetail.gameObject.SetActive(value: false);
		}
	}

	private List<ShopCommonRewardInfo> GetViewRewardList(List<ShopCommonRewardInfo> infoList)
	{
		List<ShopCommonRewardInfo> list = new List<ShopCommonRewardInfo>();
		for (int i = 0; i < infoList.Count && i < 5; i++)
		{
			list.Add(infoList[i]);
		}
		return list;
	}

	private List<ShopCommonRewardInfo> SortRewardList(List<ShopCommonRewardInfo> infoList)
	{
		List<ShopCommonRewardInfo> list = new List<ShopCommonRewardInfo>();
		list.AddRange(infoList.FindAll((ShopCommonRewardInfo info) => info.Type == 6));
		list.AddRange(infoList.FindAll((ShopCommonRewardInfo info) => info.Type == 7));
		list.AddRange(infoList.FindAll((ShopCommonRewardInfo info) => info.Type == 5));
		return list;
	}

	private void HideAllItems()
	{
		_sleeveItemOriginal.gameObject.SetActive(value: false);
		_emblemItemOriginal.gameObject.SetActive(value: false);
		if (_cardItemOriginal != null)
		{
			_cardItemOriginal.gameObject.SetActive(value: false);
		}
		for (int i = 0; i < _sleeveItemList.Count; i++)
		{
			_sleeveItemList[i].gameObject.SetActive(value: false);
		}
		for (int j = 0; j < _emblemItemList.Count; j++)
		{
			_emblemItemList[j].gameObject.SetActive(value: false);
		}
		foreach (CardSleeveRewardItem cardItem in _cardItemList)
		{
			cardItem.gameObject.SetActive(value: false);
		}
		foreach (GameObject cloneCard in _cloneCardList)
		{
			UnityEngine.Object.Destroy(cloneCard);
		}
		_cloneCardList.Clear();
	}
}
