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

public class TopBar : MonoBehaviour
{
	private const int TITLE_SIZE_MIN = 294;

	private const int TITLE_SIZE_MAX = 504;

	private const int TITLE_SIZE_MAX_WIDEMODE = 543;

	private const int TITLE_BORDER_ZENKAKU = 9;

	private const int TITLE_BORDER_HANKAKU = 16;

	private const int TITLE_TEXT_SIZE_ZENKAKU = 30;

	private const int TITLE_TEXT_SIZE_HANKAKU = 15;

	private const int TITLE_SIZE_LABEL_OFFSET = -30;

	[SerializeField]
	private UIPanel _panel;

	[SerializeField]
	public GameObject TitleObject;

	[SerializeField]
	public GameObject NameWindowObject;

	[SerializeField]
	public UIButton BuyCrystalButton;

	[SerializeField]
	public GameObject NameWindowBg;

	[SerializeField]
	public UILabel NameLabel;

	[SerializeField]
	private UILabel BattlePointLabel;

	[SerializeField]
	private UILabel BattlePointTitleLabel;

	[SerializeField]
	public UITexture RankTexture;

	[SerializeField]
	public UITexture EmblemTexture;

	[SerializeField]
	public UIButton BackButton;

	[SerializeField]
	public UILabel BackButtonLabel;

	[SerializeField]
	private UILabelGradientOverwriter _backLabelGradientOverwriter;

	[SerializeField]
	private UILabelEffectOverwriter _backLabelEffectOverwriter;

	[SerializeField]
	private UILabel BackButtonTitleLabel;

	[SerializeField]
	private UISprite _backButtonTitleLabelBG;

	[SerializeField]
	public UILabel RupyLabel;

	[SerializeField]
	public UILabel CrystalLabel;

	[SerializeField]
	private Collider _titleSpriteCollider;

	private Vector3 _firstPositionBack;

	private Vector3 _firstPositionTitle;

	private Vector3 _firstPositionName;

	private bool _isWideMode;

	public UILabel rupyLabel => RupyLabel;

	public UILabel crystalLabel => CrystalLabel;

	public static TopBar Create(NguiObjs prefab, GameObject obj, string titleMsg, bool MoneyDraw = true, bool isWideMode = false)
	{
		if (GameMgr.GetIns() == null)
		{
			return null;
		}
		NguiObjs nguiObjs = UnityEngine.Object.Instantiate(prefab);
		TopBar component = nguiObjs.GetComponent<TopBar>();
		nguiObjs.transform.parent = obj.transform;
		nguiObjs.transform.localPosition = new Vector3(0f, 0f, 0f);
		nguiObjs.transform.localScale = new Vector3(1f, 1f, 1f);
		component._isWideMode = isWideMode;
		nguiObjs.objs[1].SetActive(titleMsg != null);
		component.SetTitleLabel(titleMsg);
		component.rupyLabel.text = PlayerStaticData.UserRupyCount.ToString();
		component.crystalLabel.text = PlayerStaticData.UserCrystalCount.ToString();
		component.BackButtonLabel.text = Data.SystemText.Get("Common_0137");
		component._titleSpriteCollider.enabled = false;
		component.BackButton.onClick.Clear();
		UIManager.SetObjectToGrey(component.BuyCrystalButton.gameObject, b: true);
		if (!MoneyDraw)
		{
			nguiObjs.objs[0].gameObject.SetActive(value: false);
		}
		return component;
	}

	public void SetBackButtonEnable(bool enable)
	{
		GameMgr.GetIns().GetInputMgr().isBackKeyEnable = enable;
		BackButton.enabled = enable;
		UIManager.SetObjectToGrey(BackButton.gameObject, !enable);
	}

	public void SetBackButtonEvent(GameObject obj, UIManager.ViewScene backScene, EventDelegate inEvent)
	{
		BackButton.onClick.Clear();
		if (inEvent != null)
		{
			BackButton.onClick.Add(inEvent);
			return;
		}
		BackButton.onClick.Add(new EventDelegate(delegate
		{
			UIBase uibase = obj.GetComponent<UIBase>();
			Action action = delegate
			{
				if (backScene == UIManager.ViewScene.None)
				{
					GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL_TRANS);
					uibase.PushTurnButton();
				}
				else
				{
					UIManager.ViewScene currentScene = UIManager.GetInstance().GetCurrentScene();
					if (UIManager.GetInstance().IsCurrentScene(UIManager.ViewScene.Room))
					{
						(UIManager.GetInstance().GetUIBase(UIManager.ViewScene.Room) as RoomRoot).CreateChangeSceneDialog(uibase.getBackScene(), UIManager.GetInstance().TopBarBackParameterDict[currentScene]);
					}
					else
					{
						GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL_TRANS);
						UIManager.GetInstance().ChangeViewScene(uibase.getBackScene(), UIManager.GetInstance().TopBarBackParameterDict[currentScene], UIManager.GetInstance().GetLastSceneChangeParam(uibase.getBackScene()));
					}
				}
			};
			if (uibase.IsGetOutOfScene(action))
			{
				action();
			}
		}));
	}

	private void Awake()
	{
		TitleObject.SetActive(value: true);
		NameWindowObject.SetActive(value: false);
		_firstPositionBack = BackButton.transform.localPosition;
		_firstPositionTitle = TitleObject.transform.localPosition;
		_firstPositionName = NameWindowObject.transform.localPosition;
	}

	private void OnDestroy()
	{
		UIManager.GetInstance().m_CrystalLabelList.Remove(CrystalLabel);
		UIManager.GetInstance().m_RupyLabelList.Remove(RupyLabel);
	}

	public void SetPanelDepth(int depth)
	{
		_panel.depth = depth;
	}

	public void SetSortingOrder(int sortingOrder)
	{
		_panel.sortingOrder = sortingOrder;
	}

	public void SetBackButtonActive(bool inActive)
	{
		BackButton.gameObject.SetActive(inActive);
		TitleObject.SetActive(inActive);
	}

	public void ChangeNameWindowMode()
	{
		NameWindowObject.SetActive(value: true);
		BackButton.gameObject.SetActive(value: false);
		TitleObject.SetActive(value: false);
	}

	public void SetBattlePoint(int inBattlePoint, bool inIsMaster)
	{
		BattlePointLabel.text = inBattlePoint.ToString();
		if (inIsMaster)
		{
			BattlePointTitleLabel.text = Data.SystemText.Get("Common_0151");
		}
		else
		{
			BattlePointTitleLabel.text = Data.SystemText.Get("Common_0150");
		}
	}

	public void SetActiveBattlePoint(bool isActive)
	{
		BattlePointLabel.gameObject.SetActive(isActive);
		BattlePointTitleLabel.gameObject.SetActive(isActive);
	}

	public void RestorePosition()
	{
		BackButton.transform.localPosition = _firstPositionBack;
		TitleObject.transform.localPosition = _firstPositionTitle;
		NameWindowObject.transform.localPosition = _firstPositionName;
	}

	public void SetTitleLabel(string text, bool isWideMode)
	{
		_isWideMode = isWideMode;
		SetTitleLabel(text);
	}

	public void SetTitleLabel(string text)
	{
		if (string.IsNullOrEmpty(text))
		{
			return;
		}
		int length = text.Length;
		int num = 16;
		int num2 = 15;
		if (CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Jpn.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Cht.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Chs.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Kor.ToString())
		{
			num = 9;
			num2 = 30;
		}
		int num3 = 294;
		int num4 = (_isWideMode ? 543 : 504);
		if (length > num)
		{
			int num5 = length - num;
			num3 += num5 * num2;
			if (num3 > num4)
			{
				num3 = num4;
			}
		}
		BackButtonTitleLabel.width = num3 + -30;
		_backButtonTitleLabelBG.width = num3;
		BackButtonTitleLabel.text = text;
	}

	public void SetTitleLabelWidth(int width)
	{
		BackButtonTitleLabel.width = width + -30;
		_backButtonTitleLabelBG.width = width;
	}

	public void OverwriteBackLabelColors(eColorCodeId gradientTopColorId, eColorCodeId gradientBottomColorId)
	{
		OverwriteBackLabelGradient(gradientTopColorId, gradientBottomColorId);
	}

	public void CancelOverwriteBackLabelColors()
	{
		CancelOverwriteBackLabelGradient();
	}

	private void OverwriteBackLabelGradient(eColorCodeId topColorId, eColorCodeId bottomColorId)
	{
		_backLabelGradientOverwriter.enabled = true;
		_backLabelGradientOverwriter.GradientTopColorId = topColorId;
		_backLabelGradientOverwriter.GradientBottomColorId = bottomColorId;
	}

	private void CancelOverwriteBackLabelGradient()
	{
		_backLabelGradientOverwriter.enabled = false;
	}

	public void OverwriteBackLabelEffect(UILabel.Effect style, eColorCodeId colorId, Vector2 distance)
	{
		_backLabelEffectOverwriter.enabled = true;
		_backLabelEffectOverwriter.EffectStyle = style;
		_backLabelEffectOverwriter.EffectColorId = colorId;
		_backLabelEffectOverwriter.EffectDistance = distance;
	}

	public void CancelOverwriteBackLabelEffect()
	{
		_backLabelEffectOverwriter.enabled = false;
	}

	public void SetTitleSpriteColliderEnabled(bool enabled)
	{
		_titleSpriteCollider.enabled = enabled;
	}
}
