using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.Arena;

namespace Wizard;

public class RewardConfirmView : MonoBehaviour
{
	[SerializeField]
	private UIGrid _gridRewardViewFor4;

	[SerializeField]
	private RewardConfirmViewItem _itemOriginal;

	private List<string> _loadedResourceList;

	public static bool IsSpecialEmblemLayout(List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward> rewardList)
	{
		if (rewardList.Count() == 5)
		{
			return rewardList.Count((Wizard.Scripts.Network.Data.TaskData.Arena.Reward n) => n.UserGoodsData.GoodsType == UserGoods.Type.Emblem) == 2;
		}
		return false;
	}

	public void Initialize(List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward> rewardList)
	{
		_gridRewardViewFor4.gameObject.SetActive(value: true);
		UIManager.GetInstance().createInSceneCenterLoading();
		LoadResources(rewardList, delegate
		{
			UIManager.GetInstance().closeInSceneCenterLoading();
			if (IsSpecialEmblemLayout(rewardList))
			{
				List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward> list = new List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward>();
				foreach (Wizard.Scripts.Network.Data.TaskData.Arena.Reward reward in rewardList)
				{
					if (reward.UserGoodsData.GoodsType == UserGoods.Type.Emblem)
					{
						list.Add(reward);
						if (list.Count() == 2)
						{
							NGUITools.AddChild(_gridRewardViewFor4.gameObject, _itemOriginal.gameObject).GetComponent<RewardConfirmViewItem>().InitializeForTwoEmblem(list[0], list[1]);
						}
					}
					else
					{
						NGUITools.AddChild(_gridRewardViewFor4.gameObject, _itemOriginal.gameObject).GetComponent<RewardConfirmViewItem>().Initialize(reward.UserGoodsData, reward.num);
					}
				}
			}
			else
			{
				foreach (Wizard.Scripts.Network.Data.TaskData.Arena.Reward reward2 in rewardList)
				{
					NGUITools.AddChild(_gridRewardViewFor4.gameObject, _itemOriginal.gameObject).GetComponent<RewardConfirmViewItem>().Initialize(reward2.UserGoodsData, reward2.num);
				}
			}
			_itemOriginal.gameObject.SetActive(value: false);
			_gridRewardViewFor4.repositionNow = true;
		});
	}

	private void LoadResources(List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward> rewardList, Action onFinish)
	{
		List<string> rewardPathList = new List<string>();
		foreach (Wizard.Scripts.Network.Data.TaskData.Arena.Reward reward in rewardList)
		{
			UserGoods userGoodsData = reward.UserGoodsData;
			switch (userGoodsData.GoodsType)
			{
			case UserGoods.Type.Sleeve:
			{
				long existingSleeveId = Toolbox.ResourcesManager.GetExistingSleeveId(userGoodsData.Id);
				rewardPathList.Add(Toolbox.ResourcesManager.GetAssetTypePath(existingSleeveId.ToString(), ResourcesManager.AssetLoadPathType.SleeveTexture));
				Sleeve sleeve = Data.Master.SleeveMgr.Get(existingSleeveId);
				if (sleeve.IsPremiumSleeve)
				{
					UIManager.GetInstance().getUIBase_CardManager().AddPremireSleevePath(ref rewardPathList, sleeve);
				}
				break;
			}
			case UserGoods.Type.Emblem:
				rewardPathList.Add(Toolbox.ResourcesManager.GetAssetTypePath(userGoodsData.Id.ToString(), ResourcesManager.AssetLoadPathType.Emblem_M));
				break;
			case UserGoods.Type.Skin:
				rewardPathList.Add(Toolbox.ResourcesManager.GetAssetTypePath(userGoodsData.Id.ToString(), ResourcesManager.AssetLoadPathType.ClassCharaSkinThumbnail));
				break;
			case UserGoods.Type.Degree:
				rewardPathList.Add(Toolbox.ResourcesManager.GetAssetTypePath(userGoodsData.Thumbnail, ResourcesManager.AssetLoadPathType.Item));
				break;
			case UserGoods.Type.MyPageBG:
				rewardPathList.Add(Toolbox.ResourcesManager.GetAssetTypePath("thumbnail_mypage_custom_bg", ResourcesManager.AssetLoadPathType.Item));
				break;
			}
		}
		rewardPathList.Distinct();
		UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(rewardPathList, delegate
		{
			_loadedResourceList = rewardPathList;
			onFinish.Call();
		}));
	}

	private void OnDestroy()
	{
		if (_loadedResourceList != null && _loadedResourceList.Count > 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
		}
	}
}
