using System;

namespace SharpQuake
{
	internal class GameOptionsMenu : MenuBase
	{
		public override void Show()
		{
			base.Show();
			if (this._MaxPlayers == 0)
			{
				this._MaxPlayers = server.svs.maxclients;
			}
			if (this._MaxPlayers < 2)
			{
				this._MaxPlayers = server.svs.maxclientslimit;
			}
		}

		public override void KeyEvent(int key)
		{
			if (key != 13)
			{
				if (key == 27)
				{
					MenuBase.LanConfigMenu.Show();
					return;
				}
				switch (key)
				{
				case 128:
					snd.LocalSound("misc/menu1.wav");
					this._Cursor--;
					if (this._Cursor < 0)
					{
						this._Cursor = 8;
						return;
					}
					break;
				case 129:
					snd.LocalSound("misc/menu1.wav");
					this._Cursor++;
					if (this._Cursor >= 9)
					{
						this._Cursor = 0;
						return;
					}
					break;
				case 130:
					if (this._Cursor != 0)
					{
						snd.LocalSound("misc/menu3.wav");
						this.Change(-1);
						return;
					}
					break;
				case 131:
					if (this._Cursor != 0)
					{
						snd.LocalSound("misc/menu3.wav");
						this.Change(1);
						return;
					}
					break;
				default:
					return;
				}
			}
			else
			{
				snd.LocalSound("misc/menu2.wav");
				if (this._Cursor == 0)
				{
					if (server.IsActive)
					{
						Cbuf.AddText("disconnect\n");
					}
					Cbuf.AddText("listen 0\n");
					Cbuf.AddText(string.Format("maxplayers {0}\n", this._MaxPlayers));
					Scr.BeginLoadingPlaque();
					if (common.GameKind == GameKind.Hipnotic)
					{
						Cbuf.AddText(string.Format("map {0}\n", SharpQuake.GameOptionsMenu.HipnoticLevels[SharpQuake.GameOptionsMenu.HipnoticEpisodes[this._StartEpisode].firstLevel + this._StartLevel].name));
						return;
					}
					if (common.GameKind == GameKind.Rogue)
					{
						Cbuf.AddText(string.Format("map {0}\n", SharpQuake.GameOptionsMenu.RogueLevels[SharpQuake.GameOptionsMenu.RogueEpisodes[this._StartEpisode].firstLevel + this._StartLevel].name));
						return;
					}
					Cbuf.AddText(string.Format("map {0}\n", SharpQuake.GameOptionsMenu.Levels[SharpQuake.GameOptionsMenu.Episodes[this._StartEpisode].firstLevel + this._StartLevel].name));
					return;
				}
				else
				{
					this.Change(1);
				}
			}
		}

		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.DrawTextBox(152, 32, 10, 1);
			menu.Print(160, 40, "begin game");
			menu.Print(0, 56, "      Max players");
			menu.Print(160, 56, this._MaxPlayers.ToString());
			menu.Print(0, 64, "        Game Type");
			if (host.IsCoop)
			{
				menu.Print(160, 64, "Cooperative");
			}
			else
			{
				menu.Print(160, 64, "Deathmatch");
			}
			menu.Print(0, 72, "        Teamplay");
			if (common.GameKind == GameKind.Rogue)
			{
				string str;
				switch ((int)host.TeamPlay)
				{
				case 1:
					str = "No Friendly Fire";
					break;
				case 2:
					str = "Friendly Fire";
					break;
				case 3:
					str = "Tag";
					break;
				case 4:
					str = "Capture the Flag";
					break;
				case 5:
					str = "One Flag CTF";
					break;
				case 6:
					str = "Three Team CTF";
					break;
				default:
					str = "Off";
					break;
				}
				menu.Print(160, 72, str);
			}
			else
			{
				int num = (int)host.TeamPlay;
				string str2;
				if (num != 1)
				{
					if (num != 2)
					{
						str2 = "Off";
					}
					else
					{
						str2 = "Friendly Fire";
					}
				}
				else
				{
					str2 = "No Friendly Fire";
				}
				menu.Print(160, 72, str2);
			}
			menu.Print(0, 80, "            Skill");
			if (host.Skill == 0f)
			{
				menu.Print(160, 80, "Easy difficulty");
			}
			else if (host.Skill == 1f)
			{
				menu.Print(160, 80, "Normal difficulty");
			}
			else if (host.Skill == 2f)
			{
				menu.Print(160, 80, "Hard difficulty");
			}
			else
			{
				menu.Print(160, 80, "Nightmare difficulty");
			}
			menu.Print(0, 88, "       Frag Limit");
			if (host.FragLimit == 0f)
			{
				menu.Print(160, 88, "none");
			}
			else
			{
				menu.Print(160, 88, string.Format("{0} frags", (int)host.FragLimit));
			}
			menu.Print(0, 96, "       Time Limit");
			if (host.TimeLimit == 0f)
			{
				menu.Print(160, 96, "none");
			}
			else
			{
				menu.Print(160, 96, string.Format("{0} minutes", (int)host.TimeLimit));
			}
			menu.Print(0, 112, "         Episode");
			if (common.GameKind == GameKind.Hipnotic)
			{
				menu.Print(160, 112, SharpQuake.GameOptionsMenu.HipnoticEpisodes[this._StartEpisode].description);
			}
			else if (common.GameKind == GameKind.Rogue)
			{
				menu.Print(160, 112, SharpQuake.GameOptionsMenu.RogueEpisodes[this._StartEpisode].description);
			}
			else
			{
				menu.Print(160, 112, SharpQuake.GameOptionsMenu.Episodes[this._StartEpisode].description);
			}
			menu.Print(0, 120, "           Level");
			if (common.GameKind == GameKind.Hipnotic)
			{
				menu.Print(160, 120, SharpQuake.GameOptionsMenu.HipnoticLevels[SharpQuake.GameOptionsMenu.HipnoticEpisodes[this._StartEpisode].firstLevel + this._StartLevel].description);
				menu.Print(160, 128, SharpQuake.GameOptionsMenu.HipnoticLevels[SharpQuake.GameOptionsMenu.HipnoticEpisodes[this._StartEpisode].firstLevel + this._StartLevel].name);
			}
			else if (common.GameKind == GameKind.Rogue)
			{
				menu.Print(160, 120, SharpQuake.GameOptionsMenu.RogueLevels[SharpQuake.GameOptionsMenu.RogueEpisodes[this._StartEpisode].firstLevel + this._StartLevel].description);
				menu.Print(160, 128, SharpQuake.GameOptionsMenu.RogueLevels[SharpQuake.GameOptionsMenu.RogueEpisodes[this._StartEpisode].firstLevel + this._StartLevel].name);
			}
			else
			{
				menu.Print(160, 120, SharpQuake.GameOptionsMenu.Levels[SharpQuake.GameOptionsMenu.Episodes[this._StartEpisode].firstLevel + this._StartLevel].description);
				menu.Print(160, 128, SharpQuake.GameOptionsMenu.Levels[SharpQuake.GameOptionsMenu.Episodes[this._StartEpisode].firstLevel + this._StartLevel].name);
			}
			menu.DrawCharacter(144, SharpQuake.GameOptionsMenu._CursorTable[this._Cursor], 12 + ((int)(host.RealTime * 4.0) & 1));
			if (this._ServerInfoMessage)
			{
				if (host.RealTime - this._ServerInfoMessageTime < 5.0)
				{
					int num2 = 56;
					menu.DrawTextBox(num2, 138, 24, 4);
					int cx = num2 + 8;
					menu.Print(cx, 146, "  More than 4 players   ");
					menu.Print(cx, 154, " requires using command ");
					menu.Print(cx, 162, "line parameters; please ");
					menu.Print(cx, 170, "   see techinfo.txt.    ");
					return;
				}
				this._ServerInfoMessage = false;
			}
		}

		private void Change(int dir)
		{
			switch (this._Cursor)
			{
			case 1:
				this._MaxPlayers += dir;
				if (this._MaxPlayers > server.svs.maxclientslimit)
				{
					this._MaxPlayers = server.svs.maxclientslimit;
					this._ServerInfoMessage = true;
					this._ServerInfoMessageTime = host.RealTime;
				}
				if (this._MaxPlayers < 2)
				{
					this._MaxPlayers = 2;
					return;
				}
				break;
			case 2:
				cvar.Set("coop", (float)(host.IsCoop ? 0 : 1));
				return;
			case 3:
			{
				int num;
				if (common.GameKind == GameKind.Rogue)
				{
					num = 6;
				}
				else
				{
					num = 2;
				}
				float num2 = host.TeamPlay + (float)dir;
				if (num2 > (float)num)
				{
					num2 = 0f;
				}
				else if (num2 < 0f)
				{
					num2 = (float)num;
				}
				cvar.Set("teamplay", num2);
				return;
			}
			case 4:
			{
				float num3 = host.Skill + (float)dir;
				if (num3 > 3f)
				{
					num3 = 0f;
				}
				if (num3 < 0f)
				{
					num3 = 3f;
				}
				cvar.Set("skill", num3);
				return;
			}
			case 5:
			{
				float num4 = host.FragLimit + (float)(dir * 10);
				if (num4 > 100f)
				{
					num4 = 0f;
				}
				if (num4 < 0f)
				{
					num4 = 100f;
				}
				cvar.Set("fraglimit", num4);
				return;
			}
			case 6:
			{
				float num5 = host.TimeLimit + (float)(dir * 5);
				if (num5 > 60f)
				{
					num5 = 0f;
				}
				if (num5 < 0f)
				{
					num5 = 60f;
				}
				cvar.Set("timelimit", num5);
				return;
			}
			case 7:
			{
				this._StartEpisode += dir;
				int num;
				if (common.GameKind == GameKind.Hipnotic)
				{
					num = 6;
				}
				else if (common.GameKind == GameKind.Rogue)
				{
					num = 4;
				}
				else if (common.IsRegistered)
				{
					num = 7;
				}
				else
				{
					num = 2;
				}
				if (this._StartEpisode < 0)
				{
					this._StartEpisode = num - 1;
				}
				if (this._StartEpisode >= num)
				{
					this._StartEpisode = 0;
				}
				this._StartLevel = 0;
				return;
			}
			case 8:
			{
				this._StartLevel += dir;
				int num;
				if (common.GameKind == GameKind.Hipnotic)
				{
					num = SharpQuake.GameOptionsMenu.HipnoticEpisodes[this._StartEpisode].levels;
				}
				else if (common.GameKind == GameKind.Rogue)
				{
					num = SharpQuake.GameOptionsMenu.RogueEpisodes[this._StartEpisode].levels;
				}
				else
				{
					num = SharpQuake.GameOptionsMenu.Episodes[this._StartEpisode].levels;
				}
				if (this._StartLevel < 0)
				{
					this._StartLevel = num - 1;
				}
				if (this._StartLevel >= num)
				{
					this._StartLevel = 0;
				}
				break;
			}
			default:
				return;
			}
		}

		private const int NUM_GAMEOPTIONS = 9;

		private static readonly GameOptionsMenu.level_t[] Levels = new GameOptionsMenu.level_t[]
		{
			new GameOptionsMenu.level_t("start", "Entrance"),
			new GameOptionsMenu.level_t("e1m1", "Slipgate Complex"),
			new GameOptionsMenu.level_t("e1m2", "Castle of the Damned"),
			new GameOptionsMenu.level_t("e1m3", "The Necropolis"),
			new GameOptionsMenu.level_t("e1m4", "The Grisly Grotto"),
			new GameOptionsMenu.level_t("e1m5", "Gloom Keep"),
			new GameOptionsMenu.level_t("e1m6", "The Door To Chthon"),
			new GameOptionsMenu.level_t("e1m7", "The House of Chthon"),
			new GameOptionsMenu.level_t("e1m8", "Ziggurat Vertigo"),
			new GameOptionsMenu.level_t("e2m1", "The Installation"),
			new GameOptionsMenu.level_t("e2m2", "Ogre Citadel"),
			new GameOptionsMenu.level_t("e2m3", "Crypt of Decay"),
			new GameOptionsMenu.level_t("e2m4", "The Ebon Fortress"),
			new GameOptionsMenu.level_t("e2m5", "The Wizard's Manse"),
			new GameOptionsMenu.level_t("e2m6", "The Dismal Oubliette"),
			new GameOptionsMenu.level_t("e2m7", "Underearth"),
			new GameOptionsMenu.level_t("e3m1", "Termination Central"),
			new GameOptionsMenu.level_t("e3m2", "The Vaults of Zin"),
			new GameOptionsMenu.level_t("e3m3", "The Tomb of Terror"),
			new GameOptionsMenu.level_t("e3m4", "Satan's Dark Delight"),
			new GameOptionsMenu.level_t("e3m5", "Wind Tunnels"),
			new GameOptionsMenu.level_t("e3m6", "Chambers of Torment"),
			new GameOptionsMenu.level_t("e3m7", "The Haunted Halls"),
			new GameOptionsMenu.level_t("e4m1", "The Sewage System"),
			new GameOptionsMenu.level_t("e4m2", "The Tower of Despair"),
			new GameOptionsMenu.level_t("e4m3", "The Elder God Shrine"),
			new GameOptionsMenu.level_t("e4m4", "The Palace of Hate"),
			new GameOptionsMenu.level_t("e4m5", "Hell's Atrium"),
			new GameOptionsMenu.level_t("e4m6", "The Pain Maze"),
			new GameOptionsMenu.level_t("e4m7", "Azure Agony"),
			new GameOptionsMenu.level_t("e4m8", "The Nameless City"),
			new GameOptionsMenu.level_t("end", "Shub-Niggurath's Pit"),
			new GameOptionsMenu.level_t("dm1", "Place of Two Deaths"),
			new GameOptionsMenu.level_t("dm2", "Claustrophobopolis"),
			new GameOptionsMenu.level_t("dm3", "The Abandoned Base"),
			new GameOptionsMenu.level_t("dm4", "The Bad Place"),
			new GameOptionsMenu.level_t("dm5", "The Cistern"),
			new GameOptionsMenu.level_t("dm6", "The Dark Zone")
		};

		private static readonly GameOptionsMenu.level_t[] HipnoticLevels = new GameOptionsMenu.level_t[]
		{
			new GameOptionsMenu.level_t("start", "Command HQ"),
			new GameOptionsMenu.level_t("hip1m1", "The Pumping Station"),
			new GameOptionsMenu.level_t("hip1m2", "Storage Facility"),
			new GameOptionsMenu.level_t("hip1m3", "The Lost Mine"),
			new GameOptionsMenu.level_t("hip1m4", "Research Facility"),
			new GameOptionsMenu.level_t("hip1m5", "Military Complex"),
			new GameOptionsMenu.level_t("hip2m1", "Ancient Realms"),
			new GameOptionsMenu.level_t("hip2m2", "The Black Cathedral"),
			new GameOptionsMenu.level_t("hip2m3", "The Catacombs"),
			new GameOptionsMenu.level_t("hip2m4", "The Crypt"),
			new GameOptionsMenu.level_t("hip2m5", "Mortum's Keep"),
			new GameOptionsMenu.level_t("hip2m6", "The Gremlin's Domain"),
			new GameOptionsMenu.level_t("hip3m1", "Tur Torment"),
			new GameOptionsMenu.level_t("hip3m2", "Pandemonium"),
			new GameOptionsMenu.level_t("hip3m3", "Limbo"),
			new GameOptionsMenu.level_t("hip3m4", "The Gauntlet"),
			new GameOptionsMenu.level_t("hipend", "Armagon's Lair"),
			new GameOptionsMenu.level_t("hipdm1", "The Edge of Oblivion")
		};

		private static readonly GameOptionsMenu.level_t[] RogueLevels = new GameOptionsMenu.level_t[]
		{
			new GameOptionsMenu.level_t("start", "Split Decision"),
			new GameOptionsMenu.level_t("r1m1", "Deviant's Domain"),
			new GameOptionsMenu.level_t("r1m2", "Dread Portal"),
			new GameOptionsMenu.level_t("r1m3", "Judgement Call"),
			new GameOptionsMenu.level_t("r1m4", "Cave of Death"),
			new GameOptionsMenu.level_t("r1m5", "Towers of Wrath"),
			new GameOptionsMenu.level_t("r1m6", "Temple of Pain"),
			new GameOptionsMenu.level_t("r1m7", "Tomb of the Overlord"),
			new GameOptionsMenu.level_t("r2m1", "Tempus Fugit"),
			new GameOptionsMenu.level_t("r2m2", "Elemental Fury I"),
			new GameOptionsMenu.level_t("r2m3", "Elemental Fury II"),
			new GameOptionsMenu.level_t("r2m4", "Curse of Osiris"),
			new GameOptionsMenu.level_t("r2m5", "Wizard's Keep"),
			new GameOptionsMenu.level_t("r2m6", "Blood Sacrifice"),
			new GameOptionsMenu.level_t("r2m7", "Last Bastion"),
			new GameOptionsMenu.level_t("r2m8", "Source of Evil"),
			new GameOptionsMenu.level_t("ctf1", "Division of Change")
		};

		private static readonly GameOptionsMenu.episode_t[] Episodes = new GameOptionsMenu.episode_t[]
		{
			new GameOptionsMenu.episode_t("Welcome to Quake", 0, 1),
			new GameOptionsMenu.episode_t("Doomed Dimension", 1, 8),
			new GameOptionsMenu.episode_t("Realm of Black Magic", 9, 7),
			new GameOptionsMenu.episode_t("Netherworld", 16, 7),
			new GameOptionsMenu.episode_t("The Elder World", 23, 8),
			new GameOptionsMenu.episode_t("Final Level", 31, 1),
			new GameOptionsMenu.episode_t("Deathmatch Arena", 32, 6)
		};

		private static readonly GameOptionsMenu.episode_t[] HipnoticEpisodes = new GameOptionsMenu.episode_t[]
		{
			new GameOptionsMenu.episode_t("Scourge of Armagon", 0, 1),
			new GameOptionsMenu.episode_t("Fortress of the Dead", 1, 5),
			new GameOptionsMenu.episode_t("Dominion of Darkness", 6, 6),
			new GameOptionsMenu.episode_t("The Rift", 12, 4),
			new GameOptionsMenu.episode_t("Final Level", 16, 1),
			new GameOptionsMenu.episode_t("Deathmatch Arena", 17, 1)
		};

		private static readonly GameOptionsMenu.episode_t[] RogueEpisodes = new GameOptionsMenu.episode_t[]
		{
			new GameOptionsMenu.episode_t("Introduction", 0, 1),
			new GameOptionsMenu.episode_t("Hell's Fortress", 1, 7),
			new GameOptionsMenu.episode_t("Corridors of Time", 8, 8),
			new GameOptionsMenu.episode_t("Deathmatch Arena", 16, 1)
		};

		private static readonly int[] _CursorTable = new int[]
		{
			40,
			56,
			64,
			72,
			80,
			88,
			96,
			112,
			120
		};

		private int _StartEpisode;

		private int _StartLevel;

		private int _MaxPlayers;

		private bool _ServerInfoMessage;

		private double _ServerInfoMessageTime;

		private class level_t
		{
			public level_t(string name, string desc)
			{
				this.name = name;
				this.description = desc;
			}

			public string name;

			public string description;
		}

		private class episode_t
		{
			public episode_t(string desc, int firstLevel, int levels)
			{
				this.description = desc;
				this.firstLevel = firstLevel;
				this.levels = levels;
			}

			public string description;

			public int firstLevel;

			public int levels;
		}
	}
}
