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

namespace Wizard;

public class SubClassSelectDialog : MonoBehaviour
{
	[SerializeField]
	private UIGrid _subClassButtonGrid;

	[SerializeField]
	private ClassSelectionButton _classButtonParts;

	[SerializeField]
	private GameObject _selectMarkSubClass;

	private ClassSelectionButton _firstSelectClassButton;

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

	public ClassCharacterMasterData SelectCharaMasterData { get; private set; }

	public static DialogBase Create(DeckData srcDeck, SubClassSelectDialog prefab, List<int> usedClass, Action<int> onClickButton1)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Card_0282"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(Data.SystemText.Get("Card_0284"));
		dialogBase.SetPanelDepth(100);
		SubClassSelectDialog subClassSelectParts = UnityEngine.Object.Instantiate(prefab.gameObject).GetComponent<SubClassSelectDialog>();
		dialogBase.SetObj(subClassSelectParts.gameObject);
		UIManager.GetInstance().StartCoroutine(subClassSelectParts.Init(srcDeck, usedClass, delegate
		{
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
		dialogBase.onPushButton1 = delegate
		{
			onClickButton1.Call(subClassSelectParts.SelectCharaMasterData.class_id);
		};
		return dialogBase;
	}

	private IEnumerator Init(DeckData srcDeck, List<int> usedClass, Action onComplete)
	{
		int num = 9;
		_selectMarkSubClass.SetActive(value: false);
		_classButtonParts.gameObject.SetActive(value: false);
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		List<ClassCharacterMasterData> classCharacterMasterDatas = new List<ClassCharacterMasterData>();
		for (int i = 1; i < num; i++)
		{
			classCharacterMasterDatas.Add(dataMgr.GetCharaPrmByClassId(i));
		}
		ResourcesManager resMgr = Toolbox.ResourcesManager;
		foreach (ClassCharacterMasterData item in classCharacterMasterDatas)
		{
			_loadPathList.Add(resMgr.GetAssetTypePath(item.skin_id.ToString(), ResourcesManager.AssetLoadPathType.ClassCharaButton));
		}
		_loadPathList.Add(resMgr.GetAssetTypePath("empty", ResourcesManager.AssetLoadPathType.ClassCharaButton));
		yield return UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadPathList, null));
		foreach (ClassCharacterMasterData item2 in classCharacterMasterDatas)
		{
			if (item2.class_id != srcDeck.GetDeckClassID())
			{
				GameObject obj = NGUITools.AddChild(_subClassButtonGrid.gameObject, _classButtonParts.gameObject);
				bool flag = usedClass.Contains(item2.class_id);
				ClassSelectionButton component = obj.GetComponent<ClassSelectionButton>();
				component.Init(item2, resMgr.LoadObject<Texture>(resMgr.GetAssetTypePath(item2.skin_id.ToString(), ResourcesManager.AssetLoadPathType.ClassCharaButton, isfetch: true)), OnClickClassButton, isShowStoryClearLabel: false, flag, showNotificationIcon: false);
				obj.SetActive(value: true);
				if (_firstSelectClassButton == null && !flag)
				{
					_firstSelectClassButton = component;
				}
			}
		}
		_subClassButtonGrid.Reposition();
		SetSelection(_firstSelectClassButton);
		_selectMarkSubClass.SetActive(value: true);
		onComplete.Call();
	}

	private void OnClickClassButton(ClassSelectionButton classButton)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
		SelectSubClass(classButton);
	}

	private void SelectSubClass(ClassSelectionButton classButton)
	{
		if (SelectCharaMasterData != classButton.ClassCharacterMasterData)
		{
			SetSelection(classButton);
		}
	}

	private void SetSelection(ClassSelectionButton classButton)
	{
		SelectCharaMasterData = classButton.ClassCharacterMasterData;
		_selectMarkSubClass.transform.position = classButton.transform.position;
	}

	private void OnDestroy()
	{
		if (_loadPathList.Count > 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadPathList);
			_loadPathList.Clear();
		}
	}
}
