using System;

namespace SharpQuake
{
	internal static class menu
	{
		public static void Init()
		{
			cmd.Add("togglemenu", new xcommand_t(menu.ToggleMenu_f));
			cmd.Add("menu_main", new xcommand_t(menu.Menu_Main_f));
			cmd.Add("menu_singleplayer", new xcommand_t(menu.Menu_SinglePlayer_f));
			cmd.Add("menu_load", new xcommand_t(menu.Menu_Load_f));
			cmd.Add("menu_save", new xcommand_t(menu.Menu_Save_f));
			cmd.Add("menu_multiplayer", new xcommand_t(menu.Menu_MultiPlayer_f));
			cmd.Add("menu_setup", new xcommand_t(menu.Menu_Setup_f));
			cmd.Add("menu_options", new xcommand_t(menu.Menu_Options_f));
			cmd.Add("menu_keys", new xcommand_t(menu.Menu_Keys_f));
			cmd.Add("menu_video", new xcommand_t(menu.Menu_Video_f));
			cmd.Add("help", new xcommand_t(menu.Menu_Help_f));
			cmd.Add("menu_quit", new xcommand_t(menu.Menu_Quit_f));
		}

		public static void KeyDown(int key)
		{
			if (MenuBase.CurrentMenu != null)
			{
				MenuBase.CurrentMenu.KeyEvent(key);
			}
		}

		public static void Draw()
		{
			if (MenuBase.CurrentMenu == null || Key.Destination != keydest_t.key_menu)
			{
				return;
			}
			if (!menu._RecursiveDraw)
			{
				Scr.CopyEverithing = true;
				if (Scr.ConCurrent > 0f)
				{
					Drawer.DrawConsoleBackground(Scr.vid.height);
					snd.ExtraUpdate();
				}
				else
				{
					Drawer.FadeScreen();
				}
				Scr.FullUpdate = 0;
			}
			else
			{
				menu._RecursiveDraw = false;
			}
			if (MenuBase.CurrentMenu != null)
			{
				MenuBase.CurrentMenu.Draw();
			}
			if (menu.EnterSound)
			{
				snd.LocalSound("misc/menu2.wav");
				menu.EnterSound = false;
			}
			snd.ExtraUpdate();
		}

		public static void ToggleMenu_f()
		{
			menu.EnterSound = true;
			if (Key.Destination == keydest_t.key_menu)
			{
				if (MenuBase.CurrentMenu != MenuBase.MainMenu)
				{
					MenuBase.MainMenu.Show();
					return;
				}
				MenuBase.Hide();
				return;
			}
			else
			{
				if (Key.Destination == keydest_t.key_console)
				{
					Con.ToggleConsole_f();
					return;
				}
				MenuBase.MainMenu.Show();
				return;
			}
		}

		public static void DrawPic(int x, int y, glpic_t pic)
		{
			Drawer.DrawPic(x + (Scr.vid.width - 320 >> 1), y, pic);
		}

		public static void DrawTransPic(int x, int y, glpic_t pic)
		{
			Drawer.DrawTransPic(x + (Scr.vid.width - 320 >> 1), y, pic);
		}

		public static void DrawTransPicTranslate(int x, int y, glpic_t pic)
		{
			Drawer.TransPicTranslate(x + (Scr.vid.width - 320 >> 1), y, pic, menu._TranslationTable);
		}

		public static void Print(int cx, int cy, string str)
		{
			for (int i = 0; i < str.Length; i++)
			{
				menu.DrawCharacter(cx, cy, (int)(str[i] + '\u0080'));
				cx += 8;
			}
		}

		public static void DrawCharacter(int cx, int line, int num)
		{
			Drawer.DrawCharacter(cx + (Scr.vid.width - 320 >> 1), line, num);
		}

		public static void PrintWhite(int cx, int cy, string str)
		{
			for (int i = 0; i < str.Length; i++)
			{
				menu.DrawCharacter(cx, cy, (int)str[i]);
				cx += 8;
			}
		}

		public static void DrawTextBox(int x, int y, int width, int lines)
		{
			int num = y;
			glpic_t pic = Drawer.CachePic("gfx/box_tl.lmp");
			menu.DrawTransPic(x, num, pic);
			pic = Drawer.CachePic("gfx/box_ml.lmp");
			for (int i = 0; i < lines; i++)
			{
				num += 8;
				menu.DrawTransPic(x, num, pic);
			}
			pic = Drawer.CachePic("gfx/box_bl.lmp");
			menu.DrawTransPic(x, num + 8, pic);
			int num2 = x + 8;
			while (width > 0)
			{
				num = y;
				pic = Drawer.CachePic("gfx/box_tm.lmp");
				menu.DrawTransPic(num2, num, pic);
				pic = Drawer.CachePic("gfx/box_mm.lmp");
				for (int j = 0; j < lines; j++)
				{
					num += 8;
					if (j == 1)
					{
						pic = Drawer.CachePic("gfx/box_mm2.lmp");
					}
					menu.DrawTransPic(num2, num, pic);
				}
				pic = Drawer.CachePic("gfx/box_bm.lmp");
				menu.DrawTransPic(num2, num + 8, pic);
				width -= 2;
				num2 += 16;
			}
			num = y;
			pic = Drawer.CachePic("gfx/box_tr.lmp");
			menu.DrawTransPic(num2, num, pic);
			pic = Drawer.CachePic("gfx/box_mr.lmp");
			for (int k = 0; k < lines; k++)
			{
				num += 8;
				menu.DrawTransPic(num2, num, pic);
			}
			pic = Drawer.CachePic("gfx/box_br.lmp");
			menu.DrawTransPic(num2, num + 8, pic);
		}

		public static void DrawSlider(int x, int y, float range)
		{
			if (range < 0f)
			{
				range = 0f;
			}
			if (range > 1f)
			{
				range = 1f;
			}
			menu.DrawCharacter(x - 8, y, 128);
			int i;
			for (i = 0; i < 10; i++)
			{
				menu.DrawCharacter(x + i * 8, y, 129);
			}
			menu.DrawCharacter(x + i * 8, y, 130);
			menu.DrawCharacter((int)((float)x + 72f * range), y, 131);
		}

		public static void DrawCheckbox(int x, int y, bool on)
		{
			if (on)
			{
				menu.Print(x, y, "on");
				return;
			}
			menu.Print(x, y, "off");
		}

		public static void BuildTranslationTable(int top, int bottom)
		{
			for (int i = 0; i < 256; i++)
			{
				menu._IdentityTable[i] = (byte)i;
			}
			menu._IdentityTable.CopyTo(menu._TranslationTable, 0);
			if (top < 128)
			{
				Array.Copy(menu._IdentityTable, top, menu._TranslationTable, 16, 16);
			}
			else
			{
				for (int j = 0; j < 16; j++)
				{
					menu._TranslationTable[16 + j] = menu._IdentityTable[top + 15 - j];
				}
			}
			if (bottom < 128)
			{
				Array.Copy(menu._IdentityTable, bottom, menu._TranslationTable, 96, 16);
				return;
			}
			for (int k = 0; k < 16; k++)
			{
				menu._TranslationTable[96 + k] = menu._IdentityTable[bottom + 15 - k];
			}
		}

		private static void Menu_Main_f()
		{
			MenuBase.MainMenu.Show();
		}

		private static void Menu_SinglePlayer_f()
		{
			MenuBase.SinglePlayerMenu.Show();
		}

		private static void Menu_Load_f()
		{
			MenuBase.LoadMenu.Show();
		}

		private static void Menu_Save_f()
		{
			MenuBase.SaveMenu.Show();
		}

		private static void Menu_MultiPlayer_f()
		{
			MenuBase.MultiPlayerMenu.Show();
		}

		private static void Menu_Setup_f()
		{
			MenuBase.SetupMenu.Show();
		}

		private static void Menu_Options_f()
		{
			MenuBase.OptionsMenu.Show();
		}

		private static void Menu_Keys_f()
		{
			MenuBase.KeysMenu.Show();
		}

		private static void Menu_Video_f()
		{
			MenuBase.VideoMenu.Show();
		}

		private static void Menu_Help_f()
		{
			MenuBase.HelpMenu.Show();
		}

		private static void Menu_Quit_f()
		{
			MenuBase.QuitMenu.Show();
		}

		public static bool EnterSound;

		public static bool ReturnOnError;

		public static string ReturnReason;

		public static MenuBase ReturnMenu;

		private const int SLIDER_RANGE = 10;

		private static bool _RecursiveDraw;

		private static byte[] _IdentityTable = new byte[256];

		private static byte[] _TranslationTable = new byte[256];
	}
}
