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

namespace Wizard;

internal class AreaSelectClearReward : MonoBehaviour
{
	private class TextureTransformParam
	{
		public int Width { get; private set; }

		public int Height { get; private set; }

		public Vector3 Position { get; private set; }

		public Quaternion Rotation { get; private set; }

		public TextureTransformParam(int width, int height, Vector3 position, Quaternion rotation)
		{
			Width = width;
			Height = height;
			Position = position;
			Rotation = rotation;
		}
	}

	private readonly Dictionary<UserGoods.Type, TextureTransformParam> USERGOODS_TEXTURE_LAYOUT_DICT = new Dictionary<UserGoods.Type, TextureTransformParam>
	{
		{
			UserGoods.Type.Item,
			new TextureTransformParam(80, 80, Vector3.zero, Quaternion.identity)
		},
		{
			UserGoods.Type.Sleeve,
			new TextureTransformParam(80, 108, new Vector3(12f, 12f, 0f), Quaternion.Euler(0f, 0f, -15f))
		},
		{
			UserGoods.Type.Emblem,
			new TextureTransformParam(80, 80, Vector3.zero, Quaternion.identity)
		},
		{
			UserGoods.Type.Degree,
			new TextureTransformParam(169, 43, new Vector3(44f, -10f, 0f), Quaternion.identity)
		},
		{
			UserGoods.Type.Skin,
			new TextureTransformParam(100, 80, Vector3.zero, Quaternion.identity)
		}
	};

	private readonly Quaternion CARDOBJECT_ROTATION_QUATERNION = new Quaternion(0f, 0f, 0f, 0f);

	private const int MASK_DEPTHOFFSET_FROM_FRAME = -1;

	private const int CARD_DEPTHOFFSET_FROM_FRAME = -2;

	private readonly Color32 ACQUIRED_GRAY_COLOR = new Color32(144, 144, 144, byte.MaxValue);

	private readonly Vector3 DEFAULT_NUM_POSITION = new Vector3(48f, -25f, 0f);

	private readonly Vector3 CARD_NUM_POSITION = new Vector3(66f, -25f, 0f);

	[SerializeField]
	private GameObject _objLayoutCard;

	[SerializeField]
	private GameObject _objAttachCard;

	[SerializeField]
	private UISprite _spriteCardAcquiredMask;

	[SerializeField]
	private GameObject _objLayoutSprite;

	[SerializeField]
	private UISprite _spriteGoods;

	[SerializeField]
	private GameObject _objLayoutTexture;

	[SerializeField]
	private UITexture _textureGoods;

	[SerializeField]
	private UILabel _labelNum;

	private List<UIBase_CardManager.CardObjData> _cardObjList;

	private GameObject _cardObjEvacuationRoot;

	private GameObject _displayedCardObj;

	public UserGoods.Type RewardGoodsType { get; private set; }

	public void Init(List<UIBase_CardManager.CardObjData> cardObjList, GameObject cardObjEvacuationRoot)
	{
		_cardObjList = cardObjList;
		_cardObjEvacuationRoot = cardObjEvacuationRoot;
		HideAllLayout();
	}

	public void ShowReward(UserGoods.Type goodsType, long goodsId, int goodsCount, bool isAcquired)
	{
		HideAllLayout();
		switch (goodsType)
		{
		case UserGoods.Type.Card:
			ShowCard((int)goodsId, goodsCount, isAcquired);
			break;
		case UserGoods.Type.RedEther:
		case UserGoods.Type.Rupy:
			ShowSprite(goodsType, goodsCount, isAcquired);
			break;
		case UserGoods.Type.Item:
		case UserGoods.Type.Sleeve:
		case UserGoods.Type.Emblem:
		case UserGoods.Type.Degree:
		case UserGoods.Type.Skin:
			ShowTexture(goodsType, goodsId, goodsCount, isAcquired);
			break;
		}
		RewardGoodsType = goodsType;
	}

	public Transform GetRewardTransform()
	{
		switch (RewardGoodsType)
		{
		case UserGoods.Type.Card:
			return _objAttachCard.transform;
		case UserGoods.Type.RedEther:
		case UserGoods.Type.Rupy:
			return _spriteGoods.transform;
		case UserGoods.Type.Item:
		case UserGoods.Type.Sleeve:
		case UserGoods.Type.Emblem:
		case UserGoods.Type.Degree:
		case UserGoods.Type.Skin:
			return _textureGoods.transform;
		default:
			return null;
		}
	}

	private void HideAllLayout()
	{
		_objLayoutCard.SetActive(value: false);
		_objLayoutSprite.SetActive(value: false);
		_objLayoutTexture.SetActive(value: false);
	}

	private void ShowCard(int cardId, int cardCount, bool isAcquired)
	{
		if (_displayedCardObj != null)
		{
			_displayedCardObj.SetActive(value: false);
			_displayedCardObj.transform.SetParent(_cardObjEvacuationRoot.transform);
		}
		_objLayoutCard.SetActive(value: true);
		UIBase_CardManager.CardObjData cardObjData = GetCardObjData(cardId);
		if (cardObjData == null)
		{
			_objLayoutCard.SetActive(value: false);
			return;
		}
		_displayedCardObj = cardObjData.CardObj;
		_displayedCardObj.transform.SetParent(_objAttachCard.transform);
		_displayedCardObj.transform.localPosition = Vector3.zero;
		_displayedCardObj.transform.localScale = Vector3.one;
		_displayedCardObj.transform.rotation = CARDOBJECT_ROTATION_QUATERNION;
		_displayedCardObj.SetActive(value: true);
		CardListTemplate component = _displayedCardObj.GetComponent<CardListTemplate>();
		component._newLabel.gameObject.SetActive(value: false);
		_labelNum.text = Data.SystemText.Get("Common_0040", cardCount.ToString());
		_labelNum.transform.localPosition = CARD_NUM_POSITION;
		_labelNum.gameObject.SetActive(value: true);
		_spriteCardAcquiredMask.gameObject.SetActive(isAcquired);
		UIManager.SetObjectToGrey(_displayedCardObj, isAcquired);
		if (isAcquired)
		{
			_spriteCardAcquiredMask.depth = component._frameSprite.depth + -1;
			component._cardTexture.depth = component._frameSprite.depth + -2;
			_displayedCardObj.GetComponent<BoxCollider>().enabled = true;
		}
	}

	private UIBase_CardManager.CardObjData GetCardObjData(int cardId)
	{
		for (int i = 0; i < _cardObjList.Count; i++)
		{
			UIBase_CardManager.CardObjData cardObjData = _cardObjList[i];
			if (cardObjData != null && cardObjData.ids == cardId)
			{
				return cardObjData;
			}
		}
		return null;
	}

	private void ShowSprite(UserGoods.Type goodsType, int goodsCount, bool isAcquired)
	{
		_objLayoutSprite.SetActive(value: true);
		_spriteGoods.spriteName = AreaSelInfo.GetPresentItemSpriteName((int)goodsType);
		_spriteGoods.gameObject.SetActive(value: true);
		_labelNum.text = Data.SystemText.Get("Common_0040", goodsCount.ToString());
		_labelNum.transform.localPosition = DEFAULT_NUM_POSITION;
		_labelNum.gameObject.SetActive(value: true);
		_spriteGoods.color = GetAcquiredColor(isAcquired);
	}

	private void ShowTexture(UserGoods.Type goodsType, long goodsId, int goodsCount, bool isAcquired)
	{
		_objLayoutTexture.SetActive(value: true);
		InitializeGoodsTexture(_textureGoods, goodsType, goodsId);
		TextureTransformParam textureTransformParam = USERGOODS_TEXTURE_LAYOUT_DICT[goodsType];
		_textureGoods.width = textureTransformParam.Width;
		_textureGoods.height = textureTransformParam.Height;
		_textureGoods.transform.localPosition = textureTransformParam.Position;
		_textureGoods.transform.localRotation = textureTransformParam.Rotation;
		if (goodsType == UserGoods.Type.Item)
		{
			_labelNum.text = Data.SystemText.Get("Common_0040", goodsCount.ToString());
			_labelNum.transform.localPosition = DEFAULT_NUM_POSITION;
			_labelNum.gameObject.SetActive(value: true);
		}
		else
		{
			_labelNum.gameObject.SetActive(value: false);
		}
		_textureGoods.color = GetAcquiredColor(isAcquired);
	}

	private void InitializeGoodsTexture(UITexture texture, UserGoods.Type goodsType, long goodsId)
	{
		string text = string.Empty;
		switch (goodsType)
		{
		default:
			return;
		case UserGoods.Type.Sleeve:
		{
			long existingSleeveId = Toolbox.ResourcesManager.GetExistingSleeveId(goodsId);
			text = Toolbox.ResourcesManager.GetAssetTypePath(existingSleeveId.ToString(), ResourcesManager.AssetLoadPathType.SleeveTexture, isfetch: true);
			break;
		}
		case UserGoods.Type.Emblem:
			text = Toolbox.ResourcesManager.GetAssetTypePath(goodsId.ToString(), ResourcesManager.AssetLoadPathType.Emblem_M, isfetch: true);
			break;
		case UserGoods.Type.Degree:
			DegreeHelper.InitializeDegree(texture, goodsId, DegreeHelper.DegreeType.SMALL);
			break;
		case UserGoods.Type.Skin:
			text = Toolbox.ResourcesManager.GetAssetTypePath(goodsId.ToString(), ResourcesManager.AssetLoadPathType.ClassCharaSkinThumbnail, isfetch: true);
			break;
		case UserGoods.Type.Item:
		{
			Item item = Data.Master.ItemList.Find((Item data) => data.UserGoodsId == goodsId);
			if (item == null)
			{
				return;
			}
			text = Toolbox.ResourcesManager.GetAssetTypePath(item.thumbnail, ResourcesManager.AssetLoadPathType.Item, isfetch: true);
			break;
		}
		case UserGoods.Type.Card:
		case UserGoods.Type.Rupy:
			return;
		}
		if (string.IsNullOrEmpty(text))
		{
			return;
		}
		texture.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(text);
		_textureGoods.material = null;
		_textureGoods.gameObject.SetActive(value: true);
		if (goodsType == UserGoods.Type.Sleeve)
		{
			long existingSleeveId2 = Toolbox.ResourcesManager.GetExistingSleeveId(goodsId);
			Sleeve sleeve = Data.Master.SleeveMgr.Get(existingSleeveId2);
			if (sleeve.IsPremiumSleeve)
			{
				UIManager.GetInstance().getUIBase_CardManager().SetSleeveTexture(_textureGoods, sleeve.sleeve_id);
			}
		}
	}

	private Color32 GetAcquiredColor(bool isAcquired)
	{
		if (isAcquired)
		{
			return ACQUIRED_GRAY_COLOR;
		}
		return Color.white;
	}
}
