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

namespace Wizard;

public class TreasureBoxCpDialog : MonoBehaviour
{
	private UIAtlas _arenaAtlas;

	private const string GRADE_STAR_ON_SPRITE_NAME = "icon_diamond_mark_on";

	private const string GRADE_STAR_OFF_SPRITE_NAME = "icon_diamond_mark_off";

	private const string GRADE_GAUGE_NAME = "gauge_bg_scale_{0}";

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

	[SerializeField]
	private UISprite _currentBoxSprite;

	[SerializeField]
	private UISprite _nextBoxSprite;

	[SerializeField]
	private UISprite _openedBoxSprite;

	[SerializeField]
	private UISprite[] _gradeStepSprites;

	[SerializeField]
	private UIGauge _gradeGauge;

	[SerializeField]
	private UILabel _nextGradeLabel;

	[SerializeField]
	private UILabel _nextGradeNumLabel;

	[SerializeField]
	private GameObject _openedBoxParts;

	[SerializeField]
	private GameObject _notOpenedBoxParts;

	[SerializeField]
	private UISprite _gaugeBackground;

	[SerializeField]
	private UIButton _treasureBoxGradeUpMethodButton;

	[SerializeField]
	private UISprite _arrowSprite;

	private readonly float[][] _memory_value = new float[5][]
	{
		new float[0],
		new float[2] { 0f, 1f },
		new float[7] { 0f, 0.156f, 0.33f, 0.503f, 0.676f, 0.848f, 1f },
		new float[9] { 0f, 0.114f, 0.244f, 0.373f, 0.502f, 0.631f, 0.761f, 0.892f, 1f },
		new float[11]
		{
			0f, 0.087f, 0.192f, 0.294f, 0.397f, 0.5f, 0.605f, 0.709f, 0.812f, 0.917f,
			1f
		}
	};

	public static DialogBase CreateDialog()
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialog = UIManager.GetInstance().CreateDialogClose();
		dialog.SetButtonLayout(DialogBase.ButtonLayout.GrayBtn);
		dialog.SetSize(DialogBase.Size.M);
		dialog.SetButtonText(systemText.Get("TreasureBoxCp_0023"));
		dialog.SetTitleLabel(systemText.Get("TreasureBoxCp_0001"));
		GameObject gameObject = Object.Instantiate(LoadPrefab("UI/layoutParts/TreasureBoxCpDialog"));
		TreasureBoxCpDialog treasureBoxCpDialog = gameObject.GetComponent<TreasureBoxCpDialog>();
		treasureBoxCpDialog._treasureBoxGradeUpMethodButton.onClick.Add(new EventDelegate(delegate
		{
			dialog.SetDisp(inDisp: false);
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			treasureBoxCpDialog.CreateDetailDialog(dialog);
		}));
		treasureBoxCpDialog.SetData();
		dialog.SetObj(gameObject);
		return dialog;
	}

	private void CreateDetailDialog(DialogBase parentDialog)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetSize(DialogBase.Size.M);
		GameObject obj = Object.Instantiate(LoadPrefab("UI/layoutParts/TreasureBoxCpDetail"));
		dialogBase.SetTitleLabel(systemText.Get("TreasureBoxCp_0007"));
		dialogBase.SetObj(obj);
		dialogBase.OnClose = delegate
		{
			if (parentDialog != null)
			{
				parentDialog.SetDisp(inDisp: true);
			}
		};
		if (RoomBase.GetInstance() != null)
		{
			RoomBase.GetInstance().TreasureCpGradeUpConditionDialog = dialogBase;
		}
	}

	private static GameObject LoadPrefab(string path)
	{
		PrefabMgr prefabMgr = GameMgr.GetIns().GetPrefabMgr();
		prefabMgr.Load(path);
		return prefabMgr.Get(path);
	}

	public void SetData()
	{
		_openedBoxParts.SetActive(value: false);
		_notOpenedBoxParts.SetActive(value: false);
		TreasureBoxCp data = Data.TreasureBoxCp;
		_gradeGauge.Value = ((data.NextGrade == 0) ? 1f : _memory_value[data.NextGrade][data.CurrentMemory]);
		_nextGradeLabel.text = Data.SystemText.Get("TreasureBoxCp_0005");
		int num = data.MaxMemory - data.CurrentMemory;
		_nextGradeNumLabel.text = Data.SystemText.Get("TreasureBoxCp_0027", num.ToString());
		SetGaugeBackground(data.NextGrade, data.MaxGrade);
		string atlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Arena, null);
		_loadFileList.Add(atlasName);
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetAsync(atlasName, delegate
		{
			atlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Arena, null, isload: true);
			_arenaAtlas = Toolbox.ResourcesManager.LoadObject<GameObject>(atlasName).GetComponent<UIAtlas>();
			_currentBoxSprite.atlas = _arenaAtlas;
			_nextBoxSprite.atlas = _arenaAtlas;
			_openedBoxSprite.atlas = _arenaAtlas;
			_gaugeBackground.atlas = _arenaAtlas;
			_arrowSprite.atlas = _arenaAtlas;
			if (data.IsTreasureBoxOpened)
			{
				_openedBoxSprite.spriteName = $"box_2pick_0{TreasureBoxCp.GRADE_TO_TREASURE_BOX_ID[data.CurrentGrade]}_open";
				UIManager.SetObjectToGrey(_openedBoxSprite.gameObject, b: true);
				_openedBoxParts.SetActive(value: true);
			}
			else
			{
				_currentBoxSprite.spriteName = $"box_2pick_0{TreasureBoxCp.GRADE_TO_TREASURE_BOX_ID[data.CurrentGrade]}_close";
				_nextBoxSprite.spriteName = $"box_2pick_0{TreasureBoxCp.GRADE_TO_TREASURE_BOX_ID[data.NextGrade]}_close";
				_arrowSprite.spriteName = "arrow";
				_notOpenedBoxParts.SetActive(value: true);
			}
			for (int i = 0; i < _gradeStepSprites.Length; i++)
			{
				_gradeStepSprites[i].atlas = _arenaAtlas;
				if (i < data.CurrentGrade)
				{
					_gradeStepSprites[i].spriteName = "icon_diamond_mark_on";
				}
				else
				{
					_gradeStepSprites[i].spriteName = "icon_diamond_mark_off";
				}
			}
		}));
	}

	private void SetGaugeBackground(int nextGrade, int maxGrade)
	{
		int num = ((nextGrade == 0) ? maxGrade : nextGrade);
		_gaugeBackground.spriteName = $"gauge_bg_scale_{num}";
	}

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