using System;

namespace SharpQuake
{
	internal class OptionsMenu : MenuBase
	{
		public override void Show()
		{
			if (this._Cursor > 12)
			{
				this._Cursor = 0;
			}
			if (this._Cursor == 12 && MenuBase.VideoMenu == null)
			{
				this._Cursor = 0;
			}
			base.Show();
		}

		public override void KeyEvent(int key)
		{
			if (key != 13)
			{
				if (key != 27)
				{
					switch (key)
					{
					case 128:
						snd.LocalSound("misc/menu1.wav");
						this._Cursor--;
						if (this._Cursor < 0)
						{
							this._Cursor = 12;
						}
						break;
					case 129:
						snd.LocalSound("misc/menu1.wav");
						this._Cursor++;
						if (this._Cursor >= 13)
						{
							this._Cursor = 0;
						}
						break;
					case 130:
						this.AdjustSliders(-1);
						break;
					case 131:
						this.AdjustSliders(1);
						break;
					}
				}
				else
				{
					MenuBase.MainMenu.Show();
				}
				if (this._Cursor == 12)
				{
					if (key == 128)
					{
						this._Cursor = 11;
						return;
					}
					this._Cursor = 0;
				}
				return;
			}
			menu.EnterSound = true;
			int cursor = this._Cursor;
			switch (cursor)
			{
			case 0:
				MenuBase.KeysMenu.Show();
				return;
			case 1:
				MenuBase.Hide();
				Con.ToggleConsole_f();
				return;
			case 2:
				Cbuf.AddText("exec default.cfg\n");
				return;
			default:
				if (cursor != 12)
				{
					this.AdjustSliders(1);
					return;
				}
				MenuBase.VideoMenu.Show();
				return;
			}
		}

		public override void Draw()
		{
			menu.DrawTransPic(16, 4, Drawer.CachePic("gfx/qplaque.lmp"));
			glpic_t glpic_t = Drawer.CachePic("gfx/p_option.lmp");
			menu.DrawPic((320 - glpic_t.width) / 2, 4, glpic_t);
			menu.Print(16, 32, "    Customize controls");
			menu.Print(16, 40, "         Go to console");
			menu.Print(16, 48, "     Reset to defaults");
			menu.Print(16, 56, "           Screen size");
			float range = (Scr.ViewSize.Value - 30f) / 90f;
			menu.DrawSlider(220, 56, range);
			menu.Print(16, 64, "            Brightness");
			range = (1f - view.Gamma) / 0.5f;
			menu.DrawSlider(220, 64, range);
			menu.Print(16, 72, "           Mouse Speed");
			range = (client.Sensitivity - 1f) / 10f;
			menu.DrawSlider(220, 72, range);
			menu.Print(16, 80, "       CD Music Volume");
			range = snd.BgmVolume;
			menu.DrawSlider(220, 80, range);
			menu.Print(16, 88, "          Sound Volume");
			range = snd.Volume;
			menu.DrawSlider(220, 88, range);
			menu.Print(16, 96, "            Always Run");
			menu.DrawCheckbox(220, 96, client.ForwardSpeed > 200f);
			menu.Print(16, 104, "          Invert Mouse");
			menu.DrawCheckbox(220, 104, client.MPitch < 0f);
			menu.Print(16, 112, "            Lookspring");
			menu.DrawCheckbox(220, 112, client.LookSpring);
			menu.Print(16, 120, "            Lookstrafe");
			menu.DrawCheckbox(220, 120, client.LookStrafe);
			menu.DrawCharacter(200, 32 + this._Cursor * 8, 12 + ((int)(host.RealTime * 4.0) & 1));
		}

		private void AdjustSliders(int dir)
		{
			snd.LocalSound("misc/menu3.wav");
			switch (this._Cursor)
			{
			case 3:
			{
				float num = Scr.ViewSize.Value + (float)(dir * 10);
				if (num < 30f)
				{
					num = 30f;
				}
				if (num > 120f)
				{
					num = 120f;
				}
				cvar.Set("viewsize", num);
				return;
			}
			case 4:
			{
				float num = view.Gamma - (float)dir * 0.05f;
				if ((double)num < 0.5)
				{
					num = 0.5f;
				}
				if (num > 1f)
				{
					num = 1f;
				}
				cvar.Set("gamma", num);
				return;
			}
			case 5:
			{
				float num = client.Sensitivity + (float)dir * 0.5f;
				if (num < 1f)
				{
					num = 1f;
				}
				if (num > 11f)
				{
					num = 11f;
				}
				cvar.Set("sensitivity", num);
				return;
			}
			case 6:
			{
				float num = snd.BgmVolume + (float)dir * 0.1f;
				if (num < 0f)
				{
					num = 0f;
				}
				if (num > 1f)
				{
					num = 1f;
				}
				cvar.Set("bgmvolume", num);
				return;
			}
			case 7:
			{
				float num = snd.Volume + (float)dir * 0.1f;
				if (num < 0f)
				{
					num = 0f;
				}
				if (num > 1f)
				{
					num = 1f;
				}
				cvar.Set("volume", num);
				return;
			}
			case 8:
				if (client.ForwardSpeed > 200f)
				{
					cvar.Set("cl_forwardspeed", 200f);
					cvar.Set("cl_backspeed", 200f);
					return;
				}
				cvar.Set("cl_forwardspeed", 400f);
				cvar.Set("cl_backspeed", 400f);
				return;
			case 9:
				cvar.Set("m_pitch", -client.MPitch);
				return;
			case 10:
				cvar.Set("lookspring", (!client.LookSpring) ? 1f : 0f);
				return;
			case 11:
				cvar.Set("lookstrafe", (!client.LookStrafe) ? 1f : 0f);
				return;
			default:
				return;
			}
		}

		private const int OPTIONS_ITEMS = 13;
	}
}
