using System;

namespace SharpQuake
{
	internal class LanConfigMenu : MenuBase
	{
		public bool JoiningGame
		{
			get
			{
				return MenuBase.MultiPlayerMenu.Cursor == 0;
			}
		}

		public bool StartingGame
		{
			get
			{
				return MenuBase.MultiPlayerMenu.Cursor == 1;
			}
		}

		public override void Show()
		{
			base.Show();
			if (this._Cursor == -1)
			{
				if (this.JoiningGame)
				{
					this._Cursor = 2;
				}
				else
				{
					this._Cursor = 1;
				}
			}
			if (this.StartingGame && this._Cursor == 2)
			{
				this._Cursor = 1;
			}
			this._Port = net.DefaultHostPort;
			this._PortName = this._Port.ToString();
			menu.ReturnOnError = false;
			menu.ReturnReason = string.Empty;
		}

		public override void KeyEvent(int key)
		{
			if (key != 13)
			{
				if (key != 27)
				{
					switch (key)
					{
					case 127:
						if (this._Cursor == 0 && !string.IsNullOrEmpty(this._PortName))
						{
							this._PortName = this._PortName.Substring(0, this._PortName.Length - 1);
						}
						if (this._Cursor == 2 && !string.IsNullOrEmpty(this._JoinName))
						{
							this._JoinName = this._JoinName.Substring(0, this._JoinName.Length - 1);
						}
						break;
					case 128:
						snd.LocalSound("misc/menu1.wav");
						this._Cursor--;
						if (this._Cursor < 0)
						{
							this._Cursor = 2;
						}
						break;
					case 129:
						snd.LocalSound("misc/menu1.wav");
						this._Cursor++;
						if (this._Cursor >= 3)
						{
							this._Cursor = 0;
						}
						break;
					default:
						if (key >= 32 && key <= 127)
						{
							if (this._Cursor == 2 && this._JoinName.Length < 21)
							{
								this._JoinName += ((char)key).ToString();
							}
							if (key >= 48 && key <= 57 && this._Cursor == 0 && this._PortName.Length < 5)
							{
								this._PortName += ((char)key).ToString();
							}
						}
						break;
					}
				}
				else
				{
					MenuBase.MultiPlayerMenu.Show();
				}
			}
			else if (this._Cursor != 0)
			{
				menu.EnterSound = true;
				net.HostPort = this._Port;
				if (this._Cursor == 1)
				{
					if (this.StartingGame)
					{
						MenuBase.GameOptionsMenu.Show();
					}
					else
					{
						MenuBase.SearchMenu.Show();
					}
				}
				else if (this._Cursor == 2)
				{
					menu.ReturnMenu = this;
					menu.ReturnOnError = true;
					MenuBase.Hide();
					Cbuf.AddText(string.Format("connect \"{0}\"\n", this._JoinName));
				}
			}
			if (this.StartingGame && this._Cursor == 2)
			{
				if (key == 128)
				{
					this._Cursor = 1;
				}
				else
				{
					this._Cursor = 0;
				}
			}
			int num = common.atoi(this._PortName);
			if (num > 65535)
			{
				num = this._Port;
			}
			else
			{
				this._Port = num;
			}
			this._PortName = this._Port.ToString();
		}

		public override void Draw()
		{
			menu.DrawTransPic(16, 4, Drawer.CachePic("gfx/qplaque.lmp"));
			glpic_t glpic_t = Drawer.CachePic("gfx/p_multi.lmp");
			int num = (320 - glpic_t.width) / 2;
			menu.DrawPic(num, 4, glpic_t);
			string str;
			if (this.StartingGame)
			{
				str = "New Game - TCP/IP";
			}
			else
			{
				str = "Join Game - TCP/IP";
			}
			menu.Print(num, 32, str);
			num += 8;
			menu.Print(num, 52, "Address:");
			menu.Print(num + 72, 52, net.MyTcpIpAddress);
			menu.Print(num, SharpQuake.LanConfigMenu._CursorTable[0], "Port");
			menu.DrawTextBox(num + 64, SharpQuake.LanConfigMenu._CursorTable[0] - 8, 6, 1);
			menu.Print(num + 72, SharpQuake.LanConfigMenu._CursorTable[0], this._PortName);
			if (this.JoiningGame)
			{
				menu.Print(num, SharpQuake.LanConfigMenu._CursorTable[1], "Search for local games...");
				menu.Print(num, 108, "Join game at:");
				menu.DrawTextBox(num + 8, SharpQuake.LanConfigMenu._CursorTable[2] - 8, 22, 1);
				menu.Print(num + 16, SharpQuake.LanConfigMenu._CursorTable[2], this._JoinName);
			}
			else
			{
				menu.DrawTextBox(num, SharpQuake.LanConfigMenu._CursorTable[1] - 8, 2, 1);
				menu.Print(num + 8, SharpQuake.LanConfigMenu._CursorTable[1], "OK");
			}
			menu.DrawCharacter(num - 8, SharpQuake.LanConfigMenu._CursorTable[this._Cursor], 12 + ((int)(host.RealTime * 4.0) & 1));
			if (this._Cursor == 0)
			{
				menu.DrawCharacter(num + 72 + 8 * this._PortName.Length, SharpQuake.LanConfigMenu._CursorTable[0], 10 + ((int)(host.RealTime * 4.0) & 1));
			}
			if (this._Cursor == 2)
			{
				menu.DrawCharacter(num + 16 + 8 * this._JoinName.Length, SharpQuake.LanConfigMenu._CursorTable[2], 10 + ((int)(host.RealTime * 4.0) & 1));
			}
			if (!string.IsNullOrEmpty(menu.ReturnReason))
			{
				menu.PrintWhite(num, 148, menu.ReturnReason);
			}
		}

		public LanConfigMenu()
		{
			this._Cursor = -1;
			this._JoinName = string.Empty;
		}

		private const int NUM_LANCONFIG_CMDS = 3;

		private static readonly int[] _CursorTable = new int[]
		{
			72,
			92,
			124
		};

		private int _Port;

		private string _PortName;

		private string _JoinName;
	}
}
