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

namespace Wizard;

public class PurchaseRewardItem : MonoBehaviour
{
	private readonly Quaternion CARDOBJECT_ROTATION_QUATERNION = new Quaternion(0f, 0f, 0f, 0f);

	[SerializeField]
	private Vector3 _sleeveAndEmblemColliderSize = new Vector3(200f, 200f, 0f);

	[SerializeField]
	private Vector3 _sleeveAndEmblemColliderCenter = new Vector3(0f, 0f, 0f);

	[SerializeField]
	private UILabel _labelPurchaseNth;

	[SerializeField]
	private GameObject _objCardLayout;

	[SerializeField]
	private GameObject _objCardRoot;

	[SerializeField]
	private GameObject _objSleeveLayout;

	[SerializeField]
	private UITexture _textureSleeve;

	[SerializeField]
	private GameObject _degreeLayout;

	[SerializeField]
	private UITexture _degreeTexture;

	[SerializeField]
	private GameObject _objEmblemLayout;

	[SerializeField]
	private UITexture _textureEmblem;

	[SerializeField]
	private GameObject _objTwoEmblemsLayout;

	[SerializeField]
	private UITexture[] _textureTwoEmblems;

	[SerializeField]
	private GameObject _objComboSleeveEmblemLayout;

	[SerializeField]
	private UITexture _textureComboSleeve;

	[SerializeField]
	private UITexture _textureComboEmblem;

	[SerializeField]
	private GameObject _objComboMultiEmblemsParent;

	[SerializeField]
	private UITexture[] _textureComboMultiEmblems;

	[SerializeField]
	private GameObject _objItemLayout;

	[SerializeField]
	private UITexture _textureItem;

	[SerializeField]
	private UILabel _labelAcquired;

	[SerializeField]
	private UISprite _spriteAcquiredCardMask;

	[SerializeField]
	private GameObject _objItemNum;

	[SerializeField]
	private UILabel _labelItemNum;

	[SerializeField]
	private GameObject[] _objLayouts;

	private const string PATH_REWARD_DETAIL_NORMAL = "UI/layoutParts/Dialog/DialogSleeveDetail";

	private const string PATH_REWARD_DETAIL_LARGE = "UI/layoutParts/Dialog/DialogSleeveDetailLarge";

	public string DetailDialogTitleOverride { get; set; }

	private static CardSleeveDetailWindow InstantiateDetailPrefab(bool useLargeDetailDialog)
	{
		return (Object.Instantiate(Resources.Load(useLargeDetailDialog ? "UI/layoutParts/Dialog/DialogSleeveDetailLarge" : "UI/layoutParts/Dialog/DialogSleeveDetail")) as GameObject).GetComponent<CardSleeveDetailWindow>();
	}

	public void SetUserGoods(PurchaseRewardInfo purchaseReward, bool useLargeDetailDialog, GameObject cardObj, bool isPaging, bool isEnableItemNumber)
	{
		SetPurchaseNthLabel(purchaseReward.PurchaseNthText);
		GameObject[] objLayouts = _objLayouts;
		for (int i = 0; i < objLayouts.Length; i++)
		{
			objLayouts[i].SetActive(value: false);
		}
		List<ShopCommonRewardInfo> rewardInfoList = purchaseReward.RewardInfoList;
		if (rewardInfoList.Count == 1)
		{
			ShopCommonRewardInfo shopCommonRewardInfo = rewardInfoList[0];
			switch ((UserGoods.Type)shopCommonRewardInfo.Type)
			{
			case UserGoods.Type.Card:
				SetCardLayout(shopCommonRewardInfo, cardObj);
				ItemNumVisible(visible: true);
				SetItemNum(shopCommonRewardInfo);
				SetAcquired(_objCardLayout, purchaseReward.IsGet);
				if (purchaseReward.IsGet)
				{
					_spriteAcquiredCardMask.gameObject.SetActive(value: true);
					cardObj.GetComponent<BoxCollider>().enabled = true;
				}
				_objCardLayout.gameObject.SetActive(value: true);
				break;
			case UserGoods.Type.Sleeve:
				SetTexture(shopCommonRewardInfo, _textureSleeve);
				ItemNumVisible(visible: false);
				SetAcquired(_objSleeveLayout, purchaseReward.IsGet);
				_objSleeveLayout.gameObject.SetActive(value: true);
				SetRewardDetailDialog(_objSleeveLayout, rewardInfoList, useLargeDetailDialog, isPaging);
				break;
			case UserGoods.Type.Emblem:
				SetTexture(shopCommonRewardInfo, _textureEmblem);
				ItemNumVisible(visible: false);
				SetAcquired(_objEmblemLayout, purchaseReward.IsGet);
				_objEmblemLayout.gameObject.SetActive(value: true);
				SetRewardDetailDialog(_objEmblemLayout, rewardInfoList, useLargeDetailDialog, isPaging);
				break;
			case UserGoods.Type.Item:
				SetTexture(shopCommonRewardInfo, _textureItem);
				ItemNumVisible(isEnableItemNumber);
				SetItemNum(shopCommonRewardInfo);
				SetAcquired(_objItemLayout, purchaseReward.IsGet);
				_objItemLayout.gameObject.SetActive(value: true);
				break;
			case UserGoods.Type.Degree:
				SetTexture(shopCommonRewardInfo, _degreeTexture);
				ItemNumVisible(visible: false);
				SetAcquired(_degreeLayout, purchaseReward.IsGet);
				_degreeLayout.gameObject.SetActive(value: true);
				break;
			}
		}
		else if (rewardInfoList.Count == 2)
		{
			List<ShopCommonRewardInfo> list = rewardInfoList.Where((ShopCommonRewardInfo reward) => reward.Type == 6).ToList();
			List<ShopCommonRewardInfo> list2 = rewardInfoList.Where((ShopCommonRewardInfo reward) => reward.Type == 7).ToList();
			int count = list.Count;
			int count2 = list2.Count;
			if (count2 == 1 && count == 1)
			{
				SetTexture(list[0], _textureComboSleeve);
				SetTexture(list2[0], _textureComboEmblem);
				ItemNumVisible(visible: false);
				SetAcquired(_objComboSleeveEmblemLayout, purchaseReward.IsGet);
				_objComboSleeveEmblemLayout.SetActive(value: true);
				SetRewardDetailDialog(_objComboSleeveEmblemLayout, rewardInfoList, useLargeDetailDialog, isPaging);
			}
			else if (count2 == 2 && count == 0)
			{
				SetTexture(list2[0], _textureTwoEmblems[0]);
				SetTexture(list2[1], _textureTwoEmblems[1]);
				ItemNumVisible(visible: false);
				SetAcquired(_objTwoEmblemsLayout, purchaseReward.IsGet);
				_objTwoEmblemsLayout.SetActive(value: true);
				SetRewardDetailDialog(_objTwoEmblemsLayout, rewardInfoList, useLargeDetailDialog, isPaging);
			}
		}
		else
		{
			if (rewardInfoList.Count <= 2)
			{
				return;
			}
			List<ShopCommonRewardInfo> list3 = rewardInfoList.Where((ShopCommonRewardInfo reward) => reward.Type == 6).ToList();
			List<ShopCommonRewardInfo> list4 = rewardInfoList.Where((ShopCommonRewardInfo reward) => reward.Type == 7).ToList();
			int count3 = list3.Count;
			int count4 = list4.Count;
			int num = rewardInfoList.Count - count3 - count4;
			if (count3 != 1 || count4 < 2 || count4 > 4 || num != 0)
			{
				return;
			}
			SetTexture(list3[0], _textureComboSleeve);
			for (int num2 = 0; num2 < _textureComboMultiEmblems.Length; num2++)
			{
				if (num2 > count4 - 1)
				{
					_textureComboMultiEmblems[num2].gameObject.SetActive(value: false);
					continue;
				}
				SetTexture(list4[num2], _textureComboMultiEmblems[num2]);
				_textureComboMultiEmblems[num2].gameObject.SetActive(value: true);
			}
			ItemNumVisible(visible: false);
			SetAcquired(_objComboSleeveEmblemLayout, purchaseReward.IsGet);
			_objComboSleeveEmblemLayout.gameObject.SetActive(value: true);
			_objComboMultiEmblemsParent.SetActive(value: true);
			_textureComboEmblem.gameObject.SetActive(value: false);
			SetRewardDetailDialog(_objComboSleeveEmblemLayout, rewardInfoList, useLargeDetailDialog, isPaging);
		}
	}

	private void SetPurchaseNthLabel(string purchaseNthText)
	{
		_labelPurchaseNth.SetWrapText(purchaseNthText);
	}

	private void SetCardLayout(ShopCommonRewardInfo rewardCardInfo, GameObject cardObj)
	{
		cardObj.transform.parent = _objCardRoot.transform;
		cardObj.transform.localPosition = Vector3.zero;
		cardObj.transform.localScale = Vector3.one;
		cardObj.transform.rotation = CARDOBJECT_ROTATION_QUATERNION;
		cardObj.SetActive(value: true);
	}

	private void SetTexture(ShopCommonRewardInfo rewardInfo, UITexture uiTexture)
	{
		string empty = string.Empty;
		long id = 3000011L;
		switch ((UserGoods.Type)rewardInfo.Type)
		{
		default:
			return;
		case UserGoods.Type.Sleeve:
			id = Toolbox.ResourcesManager.GetExistingSleeveId(rewardInfo.UserGoodsId);
			empty = Toolbox.ResourcesManager.GetAssetTypePath(id.ToString(), ResourcesManager.AssetLoadPathType.SleeveTexture, isfetch: true);
			break;
		case UserGoods.Type.Emblem:
			empty = Toolbox.ResourcesManager.GetAssetTypePath(rewardInfo.UserGoodsId.ToString(), ResourcesManager.AssetLoadPathType.Emblem_M, isfetch: true);
			break;
		case UserGoods.Type.Item:
		{
			Item item = Data.Master.ItemList.Find((Item data) => data.UserGoodsId == rewardInfo.UserGoodsId);
			if (item == null)
			{
				return;
			}
			empty = Toolbox.ResourcesManager.GetAssetTypePath(item.thumbnail, ResourcesManager.AssetLoadPathType.Item, isfetch: true);
			break;
		}
		case UserGoods.Type.Degree:
			empty = Toolbox.ResourcesManager.GetAssetTypePath(UserGoods.GetUserGoodsImageName(UserGoods.Type.Degree, 0L), ResourcesManager.AssetLoadPathType.Item, isfetch: true);
			break;
		case UserGoods.Type.Card:
			return;
		}
		Texture mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(empty);
		uiTexture.mainTexture = mainTexture;
		uiTexture.material = null;
		if (rewardInfo.Type == 6)
		{
			Sleeve sleeve = Data.Master.SleeveMgr.Get(id);
			if (sleeve.IsPremiumSleeve)
			{
				UIManager.GetInstance().getUIBase_CardManager().SetSleeveTexture(uiTexture, sleeve.sleeve_id);
			}
		}
	}

	private void ItemNumVisible(bool visible)
	{
		_objItemNum.gameObject.SetActive(visible);
	}

	private void SetItemNum(ShopCommonRewardInfo rewardInfo)
	{
		_labelItemNum.text = rewardInfo.Num.ToString();
	}

	private void SetAcquired(GameObject layout, bool isAcquired)
	{
		_labelAcquired.gameObject.SetActive(isAcquired);
		UIManager.SetObjectToGrey(layout, isAcquired);
		UIManager.SetObjectToGrey(_objItemNum, isAcquired);
	}

	private void SetRewardDetailDialog(GameObject root, List<ShopCommonRewardInfo> rewardList, bool useLargeDetailDialog, bool isPaging)
	{
		BoxCollider boxCollider = root.AddComponent<BoxCollider>();
		boxCollider.size = _sleeveAndEmblemColliderSize;
		boxCollider.center = _sleeveAndEmblemColliderCenter;
		UIEventListener.Get(root).onClick = delegate
		{
			DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
			dialogBase.SetSize(DialogBase.Size.M);
			dialogBase.SetTitleLabel(string.IsNullOrEmpty(DetailDialogTitleOverride) ? Data.SystemText.Get("Shop_0185") : DetailDialogTitleOverride);
			if (isPaging)
			{
				dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
				dialogBase.SetLayer("Loading");
				RewardBase component = NGUITools.AddChild(dialogBase.gameObject, UIManager.GetInstance().GetRewardDialogPrefab().gameObject).GetComponent<RewardBase>();
				for (int i = 0; i < rewardList.Count; i++)
				{
					component.AddReward(rewardList[i]);
				}
				component.SetActiveRewardLabel(isShow: false);
				component.EndCreate();
			}
			else
			{
				dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
				CardSleeveDetailWindow cardSleeveDetailWindow = InstantiateDetailPrefab(useLargeDetailDialog);
				dialogBase.SetObj(cardSleeveDetailWindow.gameObject);
				cardSleeveDetailWindow.SetData(rewardList, string.Empty, null);
			}
		};
	}

	public void SetScale(float scale)
	{
		base.transform.localScale = new Vector3(scale, scale, 1f);
		Vector3 localScale = new Vector3(1f / scale, 1f / scale, 1f);
		_labelPurchaseNth.transform.localScale = localScale;
		_labelItemNum.transform.parent.localScale = localScale;
	}
}
