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

public class AreaSelInfo : MonoBehaviour
{
	private enum eTableCategory
	{
		CARD,
		SLEEVE,
		OTHER,
		MAX
	}

	private const float LEFTSTAGEINFO_X_IN = 0f;

	private const float LEFTSTAGEINFO_X_OUT = -1120f;

	private const int CLEARPRESENT_MAX = 3;

	private static readonly Vector3 CLEARPRESENT_CARD_COLLISIONSIZE = new Vector3(175f, 230f, 1f);

	private const int CLEARPRESENT_CARD_DEPTHOFFSET = 50;

	private const int CLEARPRESENT_RESOURCELIST_CAPACITY = 2;

	private static readonly string[] CLEARPRESENT_NAME = new string[10] { "", "Common_0205", "", "Common_0201", "", "", "", "", "", "Common_0115" };

	private static readonly string[] CLEARPRESENT_THUMBNAIL_SPRITENAME = new string[10] { "", "thumbnail_liquid", "", "thumbnail_crystal", "", "", "thumbnail_card", "thumbnail_emblem", "thumbnail_title", "thumbnail_rupy" };

	private readonly Vector3 REWARD_TABLE_DEFAULT_POSITION = new Vector3(45f, -85.3f, 0f);

	private readonly Vector3 REWARD_TABLE_CARD_POSITION = new Vector3(28.5f, -85.3f, 0f);

	private const float TABLE_CONTAINS_CARD_REWARD_OFFSET_X = -16f;

	private const int REWARD_BG_BASIC_WIDTH = 250;

	private const int REWARD_BG_OFFSET_WIDTH_PER_GOODS = 90;

	private readonly Dictionary<UserGoods.Type, float> REWARD_BG_OFFSET_MAGNIFICATION = new Dictionary<UserGoods.Type, float>
	{
		{
			UserGoods.Type.Degree,
			2f
		},
		{
			UserGoods.Type.Card,
			1.2f
		},
		{
			UserGoods.Type.Sleeve,
			1.2f
		},
		{
			UserGoods.Type.Skin,
			1.2f
		},
		{
			UserGoods.Type.RedEther,
			1f
		},
		{
			UserGoods.Type.Rupy,
			1f
		},
		{
			UserGoods.Type.Item,
			1f
		},
		{
			UserGoods.Type.Emblem,
			1f
		}
	};

	private const float LABEL_ONLY_DEGREE_OFFSET_Y = -10f;

	[SerializeField]
	private GameObject _clearRewardPrefab;

	private List<AreaSelectClearReward> _clearRewardList = new List<AreaSelectClearReward>();

	[SerializeField]
	private UITable _tableRoot;

	[SerializeField]
	private UITable[] _tableRewardsCategory = new UITable[3];

	[SerializeField]
	private GameObject _cardObjEvacuationRoot;

	[SerializeField]
	private UISprite _spriteRewardBackground;

	[SerializeField]
	private UILabel _labelAcquired;

	private List<UIBase_CardManager.CardObjData> _cardObjList = new List<UIBase_CardManager.CardObjData>();

	[SerializeField]
	private GameObject CardDetailRoot;

	[SerializeField]
	private CardDetailUI CardDetailPrefab;

	private CardDetailUI _cardDetail;

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

	private bool _isLoadEnd = true;

	public void SetClearPresent(StoryChapterData chapterData)
	{
		if (chapterData == null || chapterData.Rewards == null)
		{
			return;
		}
		if (chapterData.Rewards.Length != 0)
		{
			base.gameObject.SetActive(value: true);
			bool isCleared = chapterData.IsCleared;
			for (int i = 0; i < _clearRewardList.Count; i++)
			{
				_clearRewardList[i].gameObject.SetActive(value: false);
			}
			List<long> list = new List<long>();
			for (int j = 0; j < chapterData.Rewards.Length; j++)
			{
				StoryChapterData.StoryReward storyReward = chapterData.Rewards[j];
				if (storyReward == null)
				{
					continue;
				}
				if (j >= _clearRewardList.Count)
				{
					break;
				}
				if (storyReward.RewardType == 5)
				{
					if (list.Contains(storyReward.RewardUserGoodsId))
					{
						continue;
					}
					list.Add(storyReward.RewardUserGoodsId);
				}
				_clearRewardList[j].gameObject.SetActive(value: true);
				_clearRewardList[j].ShowReward((UserGoods.Type)storyReward.RewardType, storyReward.RewardUserGoodsId, storyReward.RewardNumber, isCleared);
			}
			RepositionRewards();
			SetRewardBackgroundWidth();
			SetAcquiredLabel(isCleared);
		}
		else
		{
			base.gameObject.SetActive(value: false);
		}
	}

	private void RepositionRewards()
	{
		for (int i = 0; i < _tableRewardsCategory.Length; i++)
		{
			_tableRewardsCategory[i].gameObject.SetActive(value: false);
		}
		for (int j = 0; j < _clearRewardList.Count; j++)
		{
			if (_clearRewardList[j].gameObject.activeSelf)
			{
				int num = _clearRewardList[j].RewardGoodsType switch
				{
					UserGoods.Type.Card => 0, 
					UserGoods.Type.Sleeve => 1, 
					_ => 2, 
				};
				Transform obj = _clearRewardList[j].gameObject.transform;
				obj.SetParent(_tableRewardsCategory[num].transform);
				obj.SetAsLastSibling();
				_tableRewardsCategory[num].gameObject.SetActive(value: true);
			}
		}
		for (int k = 0; k < _tableRewardsCategory.Length; k++)
		{
			if (_tableRewardsCategory[k].gameObject.activeInHierarchy)
			{
				_tableRewardsCategory[k].Reposition();
			}
		}
		_tableRoot.Reposition();
		if (_tableRewardsCategory[0].gameObject.activeInHierarchy)
		{
			_tableRoot.transform.localPosition = REWARD_TABLE_CARD_POSITION;
			for (int l = 0; l < _tableRewardsCategory.Length; l++)
			{
				if (l != 0)
				{
					Vector3 localPosition = _tableRewardsCategory[l].transform.localPosition;
					localPosition.x += -16f;
					_tableRewardsCategory[l].transform.localPosition = localPosition;
				}
			}
		}
		else
		{
			_tableRoot.transform.localPosition = REWARD_TABLE_DEFAULT_POSITION;
		}
	}

	private void SetRewardBackgroundWidth()
	{
		float num = 0f;
		for (int i = 0; i < _clearRewardList.Count; i++)
		{
			if (_clearRewardList[i].gameObject.activeInHierarchy)
			{
				UserGoods.Type rewardGoodsType = _clearRewardList[i].RewardGoodsType;
				num += REWARD_BG_OFFSET_MAGNIFICATION[rewardGoodsType];
			}
		}
		int width = 250 + (int)(90f * num);
		_spriteRewardBackground.width = width;
	}

	private void SetAcquiredLabel(bool isAcquired)
	{
		_labelAcquired.gameObject.SetActive(isAcquired);
		if (!isAcquired)
		{
			return;
		}
		float a = float.MaxValue;
		float num = float.MinValue;
		bool flag = true;
		for (int i = 0; i < _clearRewardList.Count; i++)
		{
			if (_clearRewardList[i].gameObject.activeInHierarchy)
			{
				Transform rewardTransform = _clearRewardList[i].GetRewardTransform();
				a = Mathf.Min(a, rewardTransform.position.x);
				num = Mathf.Max(num, rewardTransform.position.x);
				if (_clearRewardList[i].RewardGoodsType != UserGoods.Type.Degree)
				{
					flag = false;
				}
			}
		}
		Vector3 position = _labelAcquired.transform.position;
		position.x = Mathf.Lerp(a, num, 0.5f);
		_labelAcquired.transform.position = position;
		Vector3 localPosition = _labelAcquired.transform.localPosition;
		localPosition.y = _tableRoot.transform.localPosition.y + (flag ? (-10f) : 0f);
		_labelAcquired.transform.localPosition = localPosition;
	}

	public void LoadClearPresent(IReadOnlyList<StoryChapterData> stageDataList)
	{
		if (!_isLoadEnd)
		{
			return;
		}
		_isLoadEnd = false;
		ReleaseClearPresent();
		if (CardDetailPrefab != null)
		{
			_cardDetail = Object.Instantiate(CardDetailPrefab);
			_cardDetail.transform.parent = CardDetailRoot.transform;
			_cardDetail.transform.localPosition = Vector3.zero;
			_cardDetail.transform.localScale = Vector3.one;
			_cardDetail.OnClose = OnCardDetailClose;
			_cardDetail.gameObject.SetActive(value: false);
			_cardDetail.Initialize(_cardDetail.gameObject.layer, CardMaster.CardMasterId.Default);
			_cardDetail.IsShowFlavorTextButton = true;
			_cardDetail.IsShowVoiceButton = true;
			_cardDetail.IsShowEvolutionButton = true;
		}
		_clearRewardList.Clear();
		for (int i = 0; i < 3; i++)
		{
			AreaSelectClearReward component = NGUITools.AddChild(_tableRoot.gameObject, _clearRewardPrefab).GetComponent<AreaSelectClearReward>();
			_clearRewardList.Add(component);
		}
		List<int> list = new List<int>(2);
		List<string> loadPath = new List<string>();
		StoryChapterData.StoryReward storyReward = null;
		for (int j = 0; j < stageDataList.Count; j++)
		{
			for (int k = 0; k < stageDataList[j].Rewards.Length; k++)
			{
				string text = string.Empty;
				storyReward = stageDataList[j].Rewards[k];
				if (storyReward == null)
				{
					continue;
				}
				if (storyReward.RewardType == 5)
				{
					if (!list.Contains((int)storyReward.RewardUserGoodsId))
					{
						list.Add((int)storyReward.RewardUserGoodsId);
					}
				}
				else if (storyReward.RewardType == 4)
				{
					string userGoodsImageName = UserGoods.GetUserGoodsImageName(UserGoods.Type.Item, storyReward.RewardUserGoodsId);
					text = Toolbox.ResourcesManager.GetAssetTypePath(userGoodsImageName, ResourcesManager.AssetLoadPathType.Item);
				}
				else if (storyReward.RewardType == 6)
				{
					long existingSleeveId = Toolbox.ResourcesManager.GetExistingSleeveId(storyReward.RewardUserGoodsId);
					text = Toolbox.ResourcesManager.GetAssetTypePath(existingSleeveId.ToString(), ResourcesManager.AssetLoadPathType.SleeveTexture);
					Sleeve sleeve = Data.Master.SleeveMgr.Get(existingSleeveId);
					if (sleeve.IsPremiumSleeve)
					{
						UIManager.GetInstance().getUIBase_CardManager().AddPremireSleevePath(ref loadPath, sleeve);
					}
				}
				else if (storyReward.RewardType == 8)
				{
					foreach (string degreeResource in DegreeHelper.GetDegreeResourceList(storyReward.RewardUserGoodsId, DegreeHelper.DegreeType.SMALL, isFetch: false))
					{
						if (!loadPath.Contains(degreeResource))
						{
							loadPath.Add(degreeResource);
						}
					}
				}
				else if (storyReward.RewardType == 7)
				{
					text = Toolbox.ResourcesManager.GetAssetTypePath(storyReward.RewardUserGoodsId.ToString(), ResourcesManager.AssetLoadPathType.Emblem_M);
				}
				else if (storyReward.RewardType == 10)
				{
					text = Toolbox.ResourcesManager.GetAssetTypePath(storyReward.RewardUserGoodsId.ToString(), ResourcesManager.AssetLoadPathType.ClassCharaSkinThumbnail);
				}
				if (text != string.Empty && !loadPath.Contains(text))
				{
					loadPath.Add(text);
				}
			}
		}
		if (list.Count == 0 && loadPath.Count == 0)
		{
			_isLoadEnd = true;
		}
		else
		{
			StartCoroutine(LoadClearPresentInner(list, loadPath));
		}
	}

	private IEnumerator LoadClearPresentInner(List<int> cardidlist, List<string> rewardPathList)
	{
		UIManager uiMgr = UIManager.GetInstance();
		UIBase_CardManager uiCardMgr = uiMgr.getUIBase_CardManager();
		bool isLoadRewardEnd = false;
		_loadFileList.Clear();
		bool isLoadCard = cardidlist.Count > 0;
		if (isLoadCard)
		{
			int layer = LayerMask.NameToLayer("FrontUI");
			uiMgr.CardLoadSelect(base.gameObject, cardidlist, layer, is2D: true);
		}
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(rewardPathList, delegate
		{
			_loadFileList.AddRange(rewardPathList);
			isLoadRewardEnd = true;
		}));
		while ((isLoadCard && (!uiCardMgr.getCreateEndFlag() || !uiCardMgr.isAssetAllReady)) || !isLoadRewardEnd)
		{
			yield return null;
		}
		_cardObjList = uiMgr.getCardList2DObjs();
		if (_cardObjList != null)
		{
			for (int num = 0; num < _cardObjList.Count; num++)
			{
				GameObject cardObj = _cardObjList[num].CardObj;
				if (!(cardObj == null))
				{
					cardObj.SetActive(value: false);
					UITexture[] componentsInChildren = cardObj.GetComponentsInChildren<UITexture>(includeInactive: true);
					for (int num2 = 0; num2 < componentsInChildren.Length; num2++)
					{
						componentsInChildren[num2].depth += 50;
					}
					UILabel[] componentsInChildren2 = cardObj.GetComponentsInChildren<UILabel>(includeInactive: true);
					for (int num3 = 0; num3 < componentsInChildren2.Length; num3++)
					{
						componentsInChildren2[num3].depth += 50;
					}
					UISprite[] componentsInChildren3 = cardObj.GetComponentsInChildren<UISprite>(includeInactive: true);
					for (int num4 = 0; num4 < componentsInChildren3.Length; num4++)
					{
						componentsInChildren3[num4].depth += 50;
					}
					cardObj.GetComponent<CardListTemplate>().HideNum();
					cardObj.AddComponent<BoxCollider>().size = CLEARPRESENT_CARD_COLLISIONSIZE;
					cardObj.AddComponent<UIEventListener>().onClick = _cardDetail.OnPushCardDetailOn;
				}
			}
		}
		for (int num5 = 0; num5 < 3; num5++)
		{
			_clearRewardList[num5].Init(_cardObjList, _cardObjEvacuationRoot);
		}
		_isLoadEnd = true;
	}

	public void ReleaseClearPresent()
	{
		if (_cardDetail != null)
		{
			Object.Destroy(_cardDetail.gameObject);
			_cardDetail = null;
		}
		if (_cardObjList != null)
		{
			for (int i = 0; i < _cardObjList.Count; i++)
			{
				Object.Destroy(_cardObjList[i].CardObj.gameObject);
			}
			_cardObjList.Clear();
		}
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadFileList);
		_loadFileList.Clear();
	}

	private void OnCardDetailClose()
	{
	}

	public bool GetLoadEnd()
	{
		return _isLoadEnd;
	}

	public void ResetInfoPosition()
	{
		base.gameObject.transform.localPosition = new Vector3(0f, base.gameObject.transform.localPosition.y, base.gameObject.transform.localPosition.z);
	}

	public void MoveToScreen(bool isIn, bool isImmediate)
	{
		MoveToScreenObj(base.gameObject, isIn ? 0f : (-1120f), isImmediate ? 0f : 0.5f);
	}

	public void MoveToScreenObj(GameObject target, float localPosX, float time)
	{
		if (Mathf.Approximately(time, 0f))
		{
			Vector3 localPosition = target.transform.localPosition;
			localPosition.x = localPosX;
			target.transform.localPosition = localPosition;
			return;
		}
		iTween.MoveTo(target, iTween.Hash("islocal", true, "x", localPosX, "time", time));
	}

	public static string GetPresentItemName(int itemID, long userGoodsId)
	{
		switch ((UserGoods.Type)itemID)
		{
		case UserGoods.Type.RedEther:
		case UserGoods.Type.Rupy:
			return Data.SystemText.Get(CLEARPRESENT_NAME[itemID]);
		case UserGoods.Type.Item:
		{
			Item item = Data.Master.ItemList.Find((Item data) => data.UserGoodsId == userGoodsId);
			if (item == null)
			{
				return string.Empty;
			}
			return item.name;
		}
		case UserGoods.Type.Sleeve:
		{
			Sleeve sleeve = Data.Master.SleeveMgr.Get(userGoodsId);
			if (sleeve == null)
			{
				return string.Empty;
			}
			return sleeve.sleeve_name;
		}
		case UserGoods.Type.Emblem:
		{
			Emblem emblem = Data.Master.EmblemMgr.Get(userGoodsId);
			if (emblem == null)
			{
				return string.Empty;
			}
			return emblem._name;
		}
		case UserGoods.Type.Degree:
		{
			Degree degree = Data.Master.DegreeMgr.Get((int)userGoodsId);
			if (degree == null)
			{
				return string.Empty;
			}
			return degree._name;
		}
		case UserGoods.Type.Skin:
		{
			ClassCharacterMasterData charaPrmByCharaId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId((int)userGoodsId);
			if (charaPrmByCharaId == null)
			{
				return string.Empty;
			}
			return charaPrmByCharaId.chara_name;
		}
		case UserGoods.Type.SpotCardPoint:
			return Data.SystemText.Get("Common_0161");
		case UserGoods.Type.MyPageBG:
			return Data.Master.MyPageCustomBGMaster[userGoodsId.ToString()].Name;
		default:
			return string.Empty;
		}
	}

	public static string GetPresentItemSpriteName(int itemID)
	{
		if (itemID < 0 || itemID >= CLEARPRESENT_THUMBNAIL_SPRITENAME.Length)
		{
			return string.Empty;
		}
		return CLEARPRESENT_THUMBNAIL_SPRITENAME[itemID];
	}
}
