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

namespace Wizard;

public class MyPageHomeDialog : MonoBehaviour
{
	private DialogBase _dialog;

	private MyPageHomeDialogData _homeDialogData;

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

	[SerializeField]
	private UITexture _image;

	public static void Create(GameObject prefab, MyPageHomeDialogData data, Action onFinish)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		GameObject obj = UnityEngine.Object.Instantiate(prefab);
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(data.DialogTitle);
		dialogBase.gameObject.SetActive(value: false);
		dialogBase.OnClose = delegate
		{
			data.Clear();
			onFinish.Call();
		};
		MyPageHomeDialog component = obj.GetComponent<MyPageHomeDialog>();
		component._dialog = dialogBase;
		component._homeDialogData = data;
		UIManager.GetInstance().createInSceneCenterLoading();
	}

	private void Start()
	{
		StartCoroutine(LoadResource(delegate
		{
			_dialog.SetObj(base.gameObject);
			_dialog.gameObject.SetActive(value: true);
			InitializeButtonAction();
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private void InitializeButtonAction()
	{
		List<MyPageHomeDialogData.TransitionData> transitionList = _homeDialogData.TransitionList;
		switch (transitionList.Count)
		{
		case 0:
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
			break;
		case 1:
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.BlueButton);
			_dialog.SetButtonText(transitionList[0].ButtonText);
			break;
		case 2:
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.BlueButton_BlueButton);
			_dialog.SetButtonText(transitionList[0].ButtonText, transitionList[1].ButtonText);
			break;
		case 3:
			_dialog.SetButtonLayout(DialogBase.ButtonLayout.BlueButton_BlueButton_BlueButton);
			_dialog.SetButtonText(transitionList[0].ButtonText, transitionList[1].ButtonText, transitionList[2].ButtonText);
			break;
		}
		if (_homeDialogData.TransitionList.Count >= 1)
		{
			_dialog.onPushButton1 = delegate
			{
				OnClickButton(_homeDialogData.TransitionList[0]);
			};
		}
		if (_homeDialogData.TransitionList.Count >= 2)
		{
			_dialog.onPushButton2 = delegate
			{
				OnClickButton(_homeDialogData.TransitionList[1]);
			};
		}
		if (_homeDialogData.TransitionList.Count >= 3)
		{
			_dialog.onPushButton3 = delegate
			{
				OnClickButton(_homeDialogData.TransitionList[2]);
			};
		}
	}

	private void OnDestroy()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadPath);
		_loadPath.Clear();
	}

	private string GetImageFilePath(bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath(_homeDialogData.FilePath, ResourcesManager.AssetLoadPathType.UiDownLoad, isFetch);
	}

	private IEnumerator LoadResource(Action onFinish)
	{
		_loadPath.Add(GetImageFilePath(isFetch: false));
		yield return UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadPath, null));
		_image.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(GetImageFilePath(isFetch: true));
		onFinish.Call();
	}

	private void OnClickButton(MyPageHomeDialogData.TransitionData transitionData)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		MyPageBannerBase.SceneChangeBySetting(transitionData.TransitionTarget, transitionData.Status);
	}
}
