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

namespace Wizard;

public class StarterClassSelectDialog : MonoBehaviour
{
	[SerializeField]
	public GameObject StarterPurchaseConfirmationDialog;

	[SerializeField]
	private List<GameObject> _class;

	private List<UISprite> _classSprites;

	private int _selectedClassId = -1;

	[SerializeField]
	private GameObject _selectFrame;

	[SerializeField]
	private UITexture _logoTexture;

	public void Init(DialogBase inDialog, PackConfig packConfig, PackChildGachaInfo info, int localSelectedClassId, Action<PackConfig, PackChildGachaInfo, int, int[], CardBasePrm.ClanType, int?> onPurchase, Action<int> onPushOk)
	{
		inDialog.SetSize(DialogBase.Size.M);
		inDialog.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		inDialog.SetTitleLabel(Data.SystemText.Get("Shop_0239"));
		inDialog.onPushButton1 = delegate
		{
			PackSetRotationStarterClassTask packSetRotationStarterClassTask = new PackSetRotationStarterClassTask();
			packSetRotationStarterClassTask.SetParameter(packConfig.PackId, _selectedClassId);
			StartCoroutine(Toolbox.NetworkManager.Connect(packSetRotationStarterClassTask, delegate
			{
				onPushOk.Call(_selectedClassId);
				DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
				GameObject gameObject = UnityEngine.Object.Instantiate(StarterPurchaseConfirmationDialog);
				dialogBase.SetObj(gameObject);
				gameObject.GetComponent<StarterPurchaseConfirmationDialog>().Init(dialogBase, _selectedClassId, packConfig, info, onPurchase);
			}));
		};
		_logoTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(packConfig.GetPackTitleLogoTexturePath(isFetch: true)) as Texture;
		UIManager.GetInstance().AttachAtlas(_class);
		_classSprites = new List<UISprite>();
		for (int num = 0; num < _class.Count; num++)
		{
			_classSprites.Add(_class[num].GetComponent<UISprite>());
			SetEventOnClickBtn(_class[num], num + 1);
		}
		int classId = ((localSelectedClassId == 10) ? packConfig.SelectedClassId : localSelectedClassId);
		SelectClass(classId, playSE: false);
	}

	private void SetEventOnClickBtn(GameObject btn, int index)
	{
		UIEventListener.Get(btn).onClick = delegate
		{
			SelectClass(index);
		};
	}

	private void SelectClass(int classId, bool playSE = true)
	{
		if (classId < 9 && classId >= 1)
		{
			if (playSE)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			}
			if (classId != _selectedClassId)
			{
				_selectedClassId = classId;
				SetSelectedFrame(_classSprites[classId - 1].gameObject);
			}
		}
	}

	private void SetSelectedFrame(GameObject selectedClass)
	{
		_selectFrame.transform.localPosition = selectedClass.transform.localPosition;
	}
}
