using System;

namespace SharpQuake
{
	internal class SetupMenu : MenuBase
	{
		public override void Show()
		{
			this._MyName = client.Name;
			this._HostName = net.HostName;
			this._Top = (this._OldTop = (int)client.Color >> 4);
			this._Bottom = (this._OldBottom = ((int)client.Color & 15));
			base.Show();
		}

		public override void KeyEvent(int key)
		{
			if (key != 13)
			{
				if (key == 27)
				{
					MenuBase.MultiPlayerMenu.Show();
					goto IL_2D5;
				}
				switch (key)
				{
				case 127:
					if (this._Cursor == 0 && !string.IsNullOrEmpty(this._HostName))
					{
						this._HostName = this._HostName.Substring(0, this._HostName.Length - 1);
					}
					if (this._Cursor == 1 && !string.IsNullOrEmpty(this._MyName))
					{
						this._MyName = this._MyName.Substring(0, this._MyName.Length - 1);
						goto IL_2D5;
					}
					goto IL_2D5;
				case 128:
					snd.LocalSound("misc/menu1.wav");
					this._Cursor--;
					if (this._Cursor < 0)
					{
						this._Cursor = 4;
						goto IL_2D5;
					}
					goto IL_2D5;
				case 129:
					snd.LocalSound("misc/menu1.wav");
					this._Cursor++;
					if (this._Cursor >= 5)
					{
						this._Cursor = 0;
						goto IL_2D5;
					}
					goto IL_2D5;
				case 130:
					if (this._Cursor < 2)
					{
						return;
					}
					snd.LocalSound("misc/menu3.wav");
					if (this._Cursor == 2)
					{
						this._Top--;
					}
					if (this._Cursor == 3)
					{
						this._Bottom--;
						goto IL_2D5;
					}
					goto IL_2D5;
				case 131:
					if (this._Cursor < 2)
					{
						return;
					}
					break;
				default:
					if (key < 32 || key > 127)
					{
						goto IL_2D5;
					}
					if (this._Cursor == 0 && this._HostName.Length < 15)
					{
						this._HostName += ((char)key).ToString();
					}
					if (this._Cursor == 1 && this._MyName.Length < 15)
					{
						this._MyName += ((char)key).ToString();
						goto IL_2D5;
					}
					goto IL_2D5;
				}
			}
			else
			{
				if (this._Cursor == 0 || this._Cursor == 1)
				{
					return;
				}
				if (this._Cursor != 2 && this._Cursor != 3)
				{
					if (this._MyName != client.Name)
					{
						Cbuf.AddText(string.Format("name \"{0}\"\n", this._MyName));
					}
					if (net.HostName != this._HostName)
					{
						cvar.Set("hostname", this._HostName);
					}
					if (this._Top != this._OldTop || this._Bottom != this._OldBottom)
					{
						Cbuf.AddText(string.Format("color {0} {1}\n", this._Top, this._Bottom));
					}
					menu.EnterSound = true;
					MenuBase.MultiPlayerMenu.Show();
					goto IL_2D5;
				}
			}
			snd.LocalSound("misc/menu3.wav");
			if (this._Cursor == 2)
			{
				this._Top++;
			}
			if (this._Cursor == 3)
			{
				this._Bottom++;
			}
			IL_2D5:
			if (this._Top > 13)
			{
				this._Top = 0;
			}
			if (this._Top < 0)
			{
				this._Top = 13;
			}
			if (this._Bottom > 13)
			{
				this._Bottom = 0;
			}
			if (this._Bottom < 0)
			{
				this._Bottom = 13;
			}
		}

		public override void Draw()
		{
			menu.DrawTransPic(16, 4, Drawer.CachePic("gfx/qplaque.lmp"));
			glpic_t glpic_t = Drawer.CachePic("gfx/p_multi.lmp");
			menu.DrawPic((320 - glpic_t.width) / 2, 4, glpic_t);
			menu.Print(64, 40, "Hostname");
			menu.DrawTextBox(160, 32, 16, 1);
			menu.Print(168, 40, this._HostName);
			menu.Print(64, 56, "Your name");
			menu.DrawTextBox(160, 48, 16, 1);
			menu.Print(168, 56, this._MyName);
			menu.Print(64, 80, "Shirt color");
			menu.Print(64, 104, "Pants color");
			menu.DrawTextBox(64, 132, 14, 1);
			menu.Print(72, 140, "Accept Changes");
			glpic_t = Drawer.CachePic("gfx/bigbox.lmp");
			menu.DrawTransPic(160, 64, glpic_t);
			glpic_t = Drawer.CachePic("gfx/menuplyr.lmp");
			menu.BuildTranslationTable(this._Top * 16, this._Bottom * 16);
			menu.DrawTransPicTranslate(172, 72, glpic_t);
			menu.DrawCharacter(56, this._CursorTable[this._Cursor], 12 + ((int)(host.RealTime * 4.0) & 1));
			if (this._Cursor == 0)
			{
				menu.DrawCharacter(168 + 8 * this._HostName.Length, this._CursorTable[this._Cursor], 10 + ((int)(host.RealTime * 4.0) & 1));
			}
			if (this._Cursor == 1)
			{
				menu.DrawCharacter(168 + 8 * this._MyName.Length, this._CursorTable[this._Cursor], 10 + ((int)(host.RealTime * 4.0) & 1));
			}
		}

		private const int NUM_SETUP_CMDS = 5;

		private readonly int[] _CursorTable = new int[]
		{
			40,
			56,
			80,
			104,
			140
		};

		private string _HostName;

		private string _MyName;

		private int _OldTop;

		private int _OldBottom;

		private int _Top;

		private int _Bottom;
	}
}
