using System;
using UILib;
using UnityEngine;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUtliPoolLib;

namespace XMainClient
{
	internal class XBubbleComponent : XComponent
	{
		public override uint ID
		{
			get
			{
				return XBubbleComponent.uuID;
			}
		}

		private GameObject _billboard = null;

		private float _heroHeight = 0f;

		public static string BUBBLE_TEMPLATE = "UI/Billboard/Bubble";

		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Bubble");

		private IXUILabelSymbol _text;

		private IXUILabel _label;

		private IXUISprite _board;

		private uint _timer = 0u;

		private XTimerMgr.ElapsedEventHandler _hideBubbleCb = null;

		private Transform mCameraTrans;

		private float mSrcDistance;

		public XBubbleComponent()
		{
			this._hideBubbleCb = new XTimerMgr.ElapsedEventHandler(this.HideBubble);
		}

		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._billboard = XSingleton<XResourceLoaderMgr>.singleton.CreateFromPrefab(XBubbleComponent.BUBBLE_TEMPLATE, this._entity.EngineObject.Position, this._entity.EngineObject.Rotation, true, false);
			this._billboard.name = this._entity.ID.ToString();
			bool isNpc = this._entity.IsNpc;
			if (isNpc)
			{
				XSingleton<UiUtility>.singleton.AddChild(XSingleton<XGameUI>.singleton.NpcHpbarRoot.gameObject, this._billboard);
			}
			else
			{
				XSingleton<UiUtility>.singleton.AddChild(XSingleton<XGameUI>.singleton.HpbarRoot.gameObject, this._billboard);
			}
			float val = 0.9f;
			this._heroHeight = Math.Max(this._entity.Height, val);
			this._billboard.transform.localScale = new Vector3(0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f);
			this._text = (this._billboard.transform.Find("chattext/text/content").GetComponent("XUILabelSymbol") as IXUILabelSymbol);
			this._label = (this._billboard.transform.Find("chattext/text/content").GetComponent("XUILabel") as IXUILabel);
			this._board = (this._billboard.transform.Find("chattext/text/content/board").GetComponent("XUISprite") as IXUISprite);
			this.mCameraTrans = XSingleton<XScene>.singleton.GameCamera.CameraTrans;
			this.mSrcDistance = 8f;
		}

		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_Bubble, new XComponent.XEventHandler(this.ShowBubble));
		}

		public override void OnDetachFromHost()
		{
			bool flag = this._timer > 0u;
			if (flag)
			{
				XSingleton<XTimerMgr>.singleton.KillTimer(this._timer);
			}
			this.DestroyGameObjects();
			base.OnDetachFromHost();
		}

		protected void DestroyGameObjects()
		{
			XResourceLoaderMgr.SafeDestroy(ref this._billboard, true);
		}

		protected bool ShowBubble(XEventArgs e)
		{
			XBubbleEventArgs xbubbleEventArgs = e as XBubbleEventArgs;
			this._billboard.gameObject.SetActive(true);
			this._text.InputText = xbubbleEventArgs.bubbletext;
			DlgBase<XChatSmallView, XChatSmallBehaviour>.singleton.ShowCurrTempMsg(xbubbleEventArgs.bubbletext, xbubbleEventArgs.speaker);
			bool flag = this._timer > 0u;
			if (flag)
			{
				XSingleton<XTimerMgr>.singleton.KillTimer(this._timer);
			}
			this._timer = XSingleton<XTimerMgr>.singleton.SetTimer(xbubbleEventArgs.existtime, this._hideBubbleCb, null);
			return true;
		}

		protected void HideBubble(object o)
		{
			this._billboard.gameObject.SetActive(false);
			this._timer = 0u;
		}

		public override void PostUpdate(float fDeltaT)
		{
			bool flag = this._billboard == null;
			if (!flag)
			{
				XEntity xentity = this._host as XEntity;
				bool flag2 = xentity == null;
				if (!flag2)
				{
					bool flag3 = !XEntity.ValideEntity(xentity) && !xentity.IsAffiliate;
					if (flag3)
					{
						this._billboard.SetActive(false);
					}
					else
					{
						float num = 0.2f + this._entity.Height;
						bool isAffiliate = this._entity.IsAffiliate;
						if (isAffiliate)
						{
							num = 0.05f + this._entity.Height / 2f;
						}
						this._entity.EngineObject.SyncPos();
						Vector3 position;
						position= new Vector3(this._entity.EngineObject.Position.x, this._entity.EngineObject.Position.y + num, this._entity.EngineObject.Position.z);
						this._billboard.transform.position = position;
						this._billboard.transform.rotation = XSingleton<XScene>.singleton.GameCamera.Rotaton;
						bool flag4 = this.mCameraTrans != null;
						if (flag4)
						{
							float num2 = Vector3.Distance(this._entity.EngineObject.Position, this.mCameraTrans.position);
							float num3 = num2 / this.mSrcDistance;
							this._heroHeight = (this._entity.IsAffiliate ? 1.6f : 2f) * Mathf.Clamp(num3, 0.2f, 1.2f);
							this._billboard.transform.localScale = new Vector3(0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f, 0.01f * this._heroHeight / 1.5f);
						}
						bool flag5 = Time.frameCount % 15 == 0;
						if (flag5)
						{
							float billBoardDepth = Vector3.Distance(XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.position, this._billboard.transform.position);
							this.SetBillBoardDepth(billBoardDepth);
						}
					}
				}
			}
		}

		private void SetBillBoardDepth(float dis = 0f)
		{
			int num = -(int)(dis * 100f);
			bool flag = this._label != null && this._board != null;
			if (flag)
			{
				this._label.spriteDepth = num + 1;
				this._board.spriteDepth = num;
			}
		}
	}
}