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

namespace Wizard;

public class ArenaConfigDialog : MonoBehaviour
{
	[SerializeField]
	private UITexture[] _sleeveTextureListTwoPick;

	[SerializeField]
	private UIButton _sleeveChangeButtonTwoPick;

	[SerializeField]
	private UIToggle _usePremiumCardToggleTwoPick;

	[SerializeField]
	private FilteringSleeveSelection _sleeveSelectionPrefab;

	private const int SLEEVE_SELECTION_DIALOG_DEPTH = 400;

	private const long INVALID_SLEEVE_ID = -1L;

	private List<long> _loadedSleeveIdList = new List<long>();

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

	private FilteringImageSelection _sleeveSelection;

	private long _oldSleeveId = -1L;

	private bool _startUsePremiumCardToggleTwoPick;

	private bool _isFirstToggleChange = true;

	public void Initialize()
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		bool twoPickSleeveLoading = true;
		UIEventListener.Get(_sleeveChangeButtonTwoPick.gameObject).onClick = OnClickSleeveChangeButtonTwoPick;
		_isFirstToggleChange = true;
		_usePremiumCardToggleTwoPick.value = Data.Load.data._challengeConfig.UseTwoPickPremiumCard;
		_usePremiumCardToggleTwoPick.onChange.Add(new EventDelegate(OnChangePremiumCardToggle));
		_startUsePremiumCardToggleTwoPick = Data.Load.data._challengeConfig.UseTwoPickPremiumCard;
		UIManager.SetObjectToGrey(_usePremiumCardToggleTwoPick.gameObject, Data.ArenaData.TwoPickData.isJoin);
		LoadSleeve(GetCurrentSleeveId(DataMgr.BattleType.ColosseumTwoPick), delegate
		{
			StartCoroutine(UpdateSleeveTexture(DataMgr.BattleType.ColosseumTwoPick));
			twoPickSleeveLoading = false;
			if (!twoPickSleeveLoading)
			{
				UIManager.GetInstance().closeInSceneCenterLoading();
			}
		});
		InitSleeveSelection();
	}

	public void Final()
	{
		if (_startUsePremiumCardToggleTwoPick != Data.Load.data._challengeConfig.UseTwoPickPremiumCard)
		{
			ArenaConfigUpdateTask arenaConfigUpdateTask = new ArenaConfigUpdateTask();
			arenaConfigUpdateTask.SetParameter(Data.Load.data._challengeConfig.UseTwoPickPremiumCard, Data.Load.data._challengeConfig.TwoPickSleeveId);
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(arenaConfigUpdateTask));
		}
		UnloadAllSleeves();
	}

	private void OnClickSleeveChangeButtonTwoPick(GameObject g)
	{
		OnClickSleeveChangeButton(DataMgr.BattleType.ColosseumTwoPick);
	}

	private void OnClickSleeveChangeButton(DataMgr.BattleType type)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		OpenSleeveSelectionDialog(type);
	}

	private void OnChangePremiumCardToggle()
	{
		if (!_isFirstToggleChange)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(_usePremiumCardToggleTwoPick.value ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
		}
		_isFirstToggleChange = false;
		Data.Load.data._challengeConfig.UseTwoPickPremiumCard = _usePremiumCardToggleTwoPick.value;
	}

	private void LoadSleeve(long sleeveId, Action onFinish = null)
	{
		List<string> first = CollectSleeveResourcePaths(sleeveId);
		List<string> loadPathList = first.Except(_loadedSleeveTexturePathList).ToList();
		if (loadPathList.Count == 0)
		{
			onFinish.Call();
			return;
		}
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadPathList, delegate
		{
			_loadedSleeveIdList.Add(sleeveId);
			_loadedSleeveTexturePathList.AddRange(loadPathList);
			onFinish.Call();
		}));
	}

	private void UnloadAllSleeves()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedSleeveTexturePathList);
		_loadedSleeveIdList.Clear();
		_loadedSleeveTexturePathList.Clear();
	}

	private List<string> CollectSleeveResourcePaths(long sleeveId)
	{
		sleeveId = Toolbox.ResourcesManager.GetExistingSleeveId(sleeveId);
		string path = sleeveId.ToString();
		List<string> loadPath = new List<string>();
		loadPath.Add(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.SleeveTexture));
		Sleeve sleeve = Data.Master.SleeveMgr.Get(sleeveId);
		if (sleeve.IsPremiumSleeve)
		{
			UIManager.GetInstance().getUIBase_CardManager().AddPremireSleevePath(ref loadPath, sleeve);
		}
		return loadPath;
	}

	private void InitSleeveSelection()
	{
		_sleeveSelection = NGUITools.AddChild(base.gameObject, _sleeveSelectionPrefab.gameObject).GetComponent<FilteringImageSelection>();
		_sleeveSelection.gameObject.SetActive(value: false);
		ResourcesManager resourcesManager = Toolbox.ResourcesManager;
		List<Sleeve> acquiredList = Data.Master.SleeveMgr.GetAcquiredList();
		List<SleeveCategory> list = Data.Master.SleeveCategoryIdDic.Values.OrderBy((SleeveCategory category) => category.Id).ToList();
		_sleeveSelection.Initialize(acquiredList.Count, list.Count);
		foreach (SleeveCategory item in list)
		{
			_sleeveSelection.AddSeries(item.Id, item.Name);
		}
		foreach (Sleeve sleeve in acquiredList)
		{
			string key = sleeve.sleeve_id.ToString();
			long existingSleeveId = Toolbox.ResourcesManager.GetExistingSleeveId(sleeve.sleeve_id);
			Sleeve sleeve2 = Data.Master.SleeveMgr.Get(existingSleeveId);
			List<string> loadPath = new List<string>();
			loadPath.Add(resourcesManager.GetAssetTypePath(existingSleeveId.ToString(), ResourcesManager.AssetLoadPathType.SleeveTexture));
			if (sleeve2.IsPremiumSleeve)
			{
				UIManager.GetInstance().getUIBase_CardManager().AddPremireSleevePath(ref loadPath, sleeve2);
			}
			_sleeveSelection.AddItem(key, sleeve._categoryId, isSelectable: true, loadPath, null, isDisplaySprite: false, sleeve.sleeve_name, null, () => sleeve.IsNew, delegate
			{
				Data.Master.SleeveMgr.UnsetNew(sleeve.sleeve_id);
			}, null, null, sleeve.IsFavorite);
		}
	}

	private void OpenSleeveSelectionDialog(DataMgr.BattleType type)
	{
		_oldSleeveId = GetCurrentSleeveId(type);
		_sleeveSelection.SelectItemWithKey(GetCurrentSleeveId(type).ToString());
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogBase dialogBase = DialogBase.CreateFilteringImageSelectionDialog(_sleeveSelection, "Card_0146");
		dialogBase.SetPanelDepth(400);
		dialogBase.onPushButton1 = (Action)Delegate.Combine(dialogBase.onPushButton1, (Action)delegate
		{
			if (long.TryParse(_sleeveSelection.GetSelectedItemKey(), out var id) && id != _oldSleeveId)
			{
				LoadSleeve(id);
				SleeveConfigUpdate(type, id, delegate(NetworkTask.ResultCode code)
				{
					OnSuccessChangeSleeve(code, type, id);
				});
			}
		});
	}

	private void SleeveConfigUpdate(DataMgr.BattleType type, long id, Action<NetworkTask.ResultCode> callBack = null)
	{
		ArenaConfigUpdateTask arenaConfigUpdateTask = new ArenaConfigUpdateTask();
		long useChallenge2pickSleeveId = ((type == DataMgr.BattleType.ColosseumTwoPick) ? id : Data.Load.data._challengeConfig.TwoPickSleeveId);
		arenaConfigUpdateTask.SetParameter(Data.Load.data._challengeConfig.UseTwoPickPremiumCard, useChallenge2pickSleeveId);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(arenaConfigUpdateTask, callBack));
	}

	private void OnSuccessChangeSleeve(NetworkTask.ResultCode code, DataMgr.BattleType type, long id)
	{
		SetCurrentSleeveId(type, id);
		StartCoroutine(UpdateSleeveTexture(type));
	}

	private IEnumerator UpdateSleeveTexture(DataMgr.BattleType battleType)
	{
		long sleeveId = GetCurrentSleeveId(battleType);
		while (!_loadedSleeveIdList.Contains(sleeveId))
		{
			yield return null;
		}
		UITexture[] currentSleeveTextureList = GetCurrentSleeveTextureList(battleType);
		for (int i = 0; i < currentSleeveTextureList.Length; i++)
		{
			UIManager.GetInstance().getUIBase_CardManager().SetSleeveTextureWithoutPremium(currentSleeveTextureList[i], sleeveId);
		}
	}

	private long GetCurrentSleeveId(DataMgr.BattleType battleType)
	{
		if (battleType == DataMgr.BattleType.ColosseumTwoPick)
		{
			return Data.Load.data._challengeConfig.TwoPickSleeveId;
		}
		return -1L;
	}

	private void SetCurrentSleeveId(DataMgr.BattleType battleType, long sleeveId)
	{
		if (battleType == DataMgr.BattleType.ColosseumTwoPick)
		{
			Data.Load.data._challengeConfig.TwoPickSleeveId = sleeveId;
		}
	}

	private UITexture[] GetCurrentSleeveTextureList(DataMgr.BattleType battleType)
	{
		if (battleType == DataMgr.BattleType.ColosseumTwoPick)
		{
			return _sleeveTextureListTwoPick;
		}
		return null;
	}
}
