using System;
using System.Collections.Generic;
using UILib;
using UnityEngine;
using XMainClient.UI.UICommon;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient.UI
{
	internal class DemoUI : DlgBase<DemoUI, DemoUIBehaviour>
	{
		public override string fileName
		{
			get
			{
				return "DebugDlg";
			}
		}

		public override int layer
		{
			get
			{
				return 1;
			}
		}

		public override bool autoload
		{
			get
			{
				return true;
			}
		}

		public override bool isMainUI
		{
			get
			{
				return true;
			}
		}

		public static uint MAX_MESSAGE_COUNT = 20u;

		private Queue<string> messages = new Queue<string>();

		private LinkedList<string> inputs = new LinkedList<string>();

		private LinkedListNode<string> lastInput = null;

		private float lastMessageY = 0f;

		private object locker = new object();

		private IPlatform _platform = null;

		protected override void Init()
		{
			this._platform = XSingleton<XUpdater.XUpdater>.singleton.XPlatform;
		}

		public override void Reset()
		{
		}

		public override void RegisterEvent()
		{
			base.uiBehaviour.m_Button.RegisterClickEventHandler(new ButtonClickEventHandler(this.OnButtonClick));
			base.uiBehaviour.m_PreviousButton.RegisterClickEventHandler(new ButtonClickEventHandler(this._onPreviousBtnClick));
			base.uiBehaviour.m_NextButton.RegisterClickEventHandler(new ButtonClickEventHandler(this._onNextBtnClick));
			base.uiBehaviour.m_Close.RegisterClickEventHandler(new ButtonClickEventHandler(this._onCloseBtnClick));
			base.uiBehaviour.m_Input.RegisterKeyTriggeredEventHandler(new InputKeyTriggeredEventHandler(this._onKeyTriggered));
			base.uiBehaviour.m_Input.RegisterSubmitEventHandler(new InputSubmitEventHandler(this.OnSubmit));
			base.uiBehaviour.m_Open.RegisterClickEventHandler(new ButtonClickEventHandler(this.OnFpsClick));
		}

		protected override void OnShow()
		{
			this._refreshMessages();
			base.uiBehaviour.m_ScrollView.ResetPosition();
			base.uiBehaviour.m_Input.selected(true);
			base.uiBehaviour.m_Bg.SetActive(true);
		}

		public override void OnUpdate()
		{
			this.UpdateFPS();
		}

		private bool _onPreviousBtnClick(IXUIButton go)
		{
			this._onKeyTriggered(base.uiBehaviour.m_Input, (KeyCode)273);
			return true;
		}

		private bool _onNextBtnClick(IXUIButton go)
		{
			this._onKeyTriggered(base.uiBehaviour.m_Input, (KeyCode)274);
			return true;
		}

		private bool _onCloseBtnClick(IXUIButton go)
		{
			base.uiBehaviour.m_Bg.SetActive(false);
			return true;
		}

		private void _onKeyTriggered(IXUIInput input, KeyCode key)
		{
			bool flag = key == (KeyCode)273;
			if (flag)
			{
				bool flag2 = this.inputs.Count == 0;
				if (flag2)
				{
					return;
				}
				bool flag3 = this.lastInput != null;
				if (flag3)
				{
					bool flag4 = this.lastInput.Previous != null;
					if (flag4)
					{
						this.lastInput = this.lastInput.Previous;
					}
				}
				else
				{
					this.lastInput = this.inputs.Last;
				}
			}
			else
			{
				bool flag5 = key == (KeyCode)274;
				if (!flag5)
				{
					return;
				}
				bool flag6 = this.inputs.Count == 0;
				if (flag6)
				{
					return;
				}
				bool flag7 = this.lastInput != null;
				if (flag7)
				{
					bool flag8 = this.lastInput.Next != null;
					if (flag8)
					{
						this.lastInput = this.lastInput.Next;
					}
				}
				else
				{
					this.lastInput = this.inputs.First;
				}
			}
			bool flag9 = this.lastInput != null;
			if (flag9)
			{
				base.uiBehaviour.m_Input.SetText(this.lastInput.Value);
			}
		}

		private void UpdateLastLabelPos(IXUILabel label)
		{
			this.lastMessageY -= (float)label.spriteHeight;
		}

		private float GetLastLabelPos(IXUILabel label)
		{
			return this.lastMessageY - (float)(label.spriteHeight / 2);
		}

		private void _refreshMessages()
		{
			Vector3 tplPos = base.uiBehaviour.m_MessagePool.TplPos;
			this.lastMessageY = tplPos.y;
			base.uiBehaviour.m_MessagePool.ReturnAll(false);
			base.uiBehaviour.m_ActiveMessages.Clear();
			foreach (string text in this.messages)
			{
				GameObject gameObject = base.uiBehaviour.m_MessagePool.FetchGameObject(false);
				IXUILabel ixuilabel = gameObject.GetComponent("XUILabel") as IXUILabel;
				ixuilabel.SetText(text);
				base.uiBehaviour.m_ActiveMessages.Add(ixuilabel);
				gameObject.transform.localPosition = new Vector3(tplPos.x, this.GetLastLabelPos(ixuilabel), 0f);
				this.UpdateLastLabelPos(ixuilabel);
			}
		}

		public void AddMessage(string message)
		{
			bool flag = !base.IsVisible();
			if (!flag)
			{
				object obj = this.locker;
				lock (obj)
				{
					bool flag2 = (long)this.messages.Count > (long)((ulong)DemoUI.MAX_MESSAGE_COUNT);
					if (flag2)
					{
						this.messages.Dequeue();
					}
					this.messages.Enqueue(message);
					bool flag3 = this.messages.Count != base.uiBehaviour.m_ActiveMessages.Count;
					if (flag3)
					{
						bool flag4 = this.messages.Count - base.uiBehaviour.m_ActiveMessages.Count == 1;
						if (flag4)
						{
							GameObject gameObject = base.uiBehaviour.m_MessagePool.FetchGameObject(false);
							IXUILabel ixuilabel = gameObject.GetComponent("XUILabel") as IXUILabel;
							ixuilabel.SetText(message);
							base.uiBehaviour.m_ActiveMessages.Add(ixuilabel);
							Vector3 tplPos = base.uiBehaviour.m_MessagePool.TplPos;
							gameObject.transform.localPosition = new Vector3(tplPos.x, this.GetLastLabelPos(ixuilabel), 0f);
							this.UpdateLastLabelPos(ixuilabel);
						}
						else
						{
							this._refreshMessages();
						}
					}
					else
					{
						int num = -1;
						Vector3 tplPos2 = base.uiBehaviour.m_MessagePool.TplPos;
						this.lastMessageY = tplPos2.y;
						foreach (string text in this.messages)
						{
							IXUILabel ixuilabel2 = base.uiBehaviour.m_ActiveMessages[++num];
							ixuilabel2.SetText(text);
							ixuilabel2.gameObject.transform.localPosition = new Vector3(tplPos2.x, this.GetLastLabelPos(ixuilabel2), 0f);
							this.UpdateLastLabelPos(ixuilabel2);
						}
					}
					base.uiBehaviour.m_ScrollView.ResetPosition();
				}
			}
		}

		private void _addInput(string s)
		{
			bool flag = (long)this.inputs.Count > (long)((ulong)DemoUI.MAX_MESSAGE_COUNT);
			if (flag)
			{
				this.inputs.RemoveFirst();
			}
			this.inputs.AddLast(s);
			this.lastInput = null;
		}

		private void OnSubmit(IXUIInput go)
		{
			this.Submit();
		}

		private bool OnButtonClick(IXUIButton go)
		{
			this.Submit();
			return true;
		}

		private void Submit()
		{
			string text = base.uiBehaviour.m_Input.GetText();
			this._addInput(text);
			string[] separator = new string[]
			{
				"\n"
			};
			string[] array = text.Split(separator, StringSplitOptions.None);
			for (int i = 0; i < array.Length; i++)
			{
				this.AddMessage("> " + array[i]);
				bool flag = !XSingleton<XCommand>.singleton.ProcessCommand(array[i]);
				if (flag)
				{
					this.AddMessage(string.Format("[ff0000]Invalid Command: {0}[-]", array[i]));
				}
			}
			base.uiBehaviour.m_Input.SetText("");
			base.uiBehaviour.m_Input.selected(true);
		}

		private bool OnFpsClick(IXUIButton go)
		{
			bool activeInHierarchy = base.uiBehaviour.m_Bg.activeInHierarchy;
			if (activeInHierarchy)
			{
				base.uiBehaviour.m_Bg.SetActive(false);
			}
			else
			{
				base.uiBehaviour.m_Bg.SetActive(true);
			}
			return true;
		}

		public void UpdateFPS()
		{
			bool flag = !XSingleton<XTimerMgr>.singleton.NeedFixedUpdate;
			if (!flag)
			{
				bool flag2 = !this._platform.IsPublish();
				if (flag2)
				{
					bool showBuildLog = XSingleton<XGame>.singleton.ShowBuildLog;
					if (showBuildLog)
					{
						string syncModeString = XSingleton<XGame>.singleton.GetSyncModeString();
						string text = "Debug Q";
						base.uiBehaviour.m_fps.SetText(string.Concat(new object[]
						{
							text,
							" Build:",
							XLinkTimeStamp.BuildDateTime.ToString(),
							"\nFps: ",
							XSingleton<XGame>.singleton.Fps.ToString("F1"),
							" Avg Fps: ",
							XSingleton<XGame>.singleton.FpsAvg.ToString("F1"),
							"\n",
							syncModeString,
							XSingleton<XClientNetwork>.singleton.ServerIP,
							"\nSend:",
							XSingleton<XClientNetwork>.singleton.SendBytes,
							" Recv:",
							XSingleton<XClientNetwork>.singleton.RecvBytes,
							" delay:",
							XSingleton<XServerTimeMgr>.singleton.GetDelay()
						}));
					}
					else
					{
						base.uiBehaviour.m_fps.SetText("");
					}
				}
			}
		}

		public void Toggle()
		{
			bool flag = !base.IsVisible();
			if (flag)
			{
				this.SetVisible(true, true);
			}
			else
			{
				bool activeInHierarchy = base.uiBehaviour.m_Bg.activeInHierarchy;
				if (activeInHierarchy)
				{
					base.uiBehaviour.m_Bg.SetActive(false);
					base.uiBehaviour.m_Input.selected(false);
				}
				else
				{
					base.uiBehaviour.m_Bg.SetActive(true);
					base.uiBehaviour.m_Input.selected(true);
				}
			}
		}

		public bool IsMainUIVisible()
		{
			return base.IsVisible() && base.uiBehaviour.m_Bg.activeInHierarchy;
		}
	}
}