context: using System;
  using System.Collections;
  using System.Collections.Generic;
  using Server.Commands;
  using Server.Gumps;
  using Server.Network;
  using System.Linq;
  using System.IO;
  using Server.Items;
  using Server.Mobiles;
  
  namespace Server.Commands 
  {
  	public class CreateWorld
  	{
  		public enum GumpType
  		{
  			Create,
  			Delete,
  			Recreate,
  		}
  
          public enum Category
          {
              All,
              Decoration,
              Spawn,
              System,
              Dungeon,
              RevampedDungeon,
              Expansion
          }
  
  		public class CommandEntry
  		{
              public string Name { get; set; }
              public string CreateCommand { get; set; }
              public string DeleteCommand { get; set; }
              public int CheckID { get; set; }
              public int Delay { get; set; }
  
              public Category Category { get; set; }
              public Expansion RequiredExpansion { get; set; }
  
  			public CommandEntry(string n, string c, string d, Category cat, Expansion expansion, int i, int delay = 0)
  			{
  				Name = n;
  				CreateCommand = c;
  				DeleteCommand = d;
                  Category = cat;
                  RequiredExpansion = expansion;
  				CheckID = i;
                  Delay = delay;
  			}
  		}
  
  		public static List<CommandEntry> Commands = new List<CommandEntry>(new CommandEntry[] 
          {
  			new CommandEntry("Moongates",           "Moongen",			"MoonGenDelete",		Category.Decoration,      Expansion.None,   101),
  			new CommandEntry("Doors",               "DoorGen",			"DoorGenDelete",		Category.Decoration,      Expansion.None,   102),
  			new CommandEntry("Signs",               "SignGen",			"SignGenDelete",		Category.Decoration,      Expansion.None,   103),
  			new CommandEntry("Teleporters",         "TelGen",			"TelGenDelete",			Category.Decoration,      Expansion.None,   104),
  			new CommandEntry("Doom Lamp",           "GenLeverPuzzle",   "LampPuzzleDelete",		Category.System,          Expansion.AOS,    105),
  			new CommandEntry("Doom Gauntlet",       "GenGauntlet",      "DeleteGauntlet",		Category.Dungeon,         Expansion.AOS,    106),
              new CommandEntry("Khaldun",             "GenKhaldun",       "DeleteKhaldun",        Category.Dungeon,         Expansion.None,   107),
              new CommandEntry("Stealables",          "GenStealArties",   "RemoveStealArties",	Category.Spawn,           Expansion.AOS,    108),
  			new CommandEntry("Solen Hives",         "SHTelGen",         "SHTelGenDelete",		Category.Dungeon,         Expansion.LBR,    109),
  			new CommandEntry("Malas Secrets",       "SecretLocGen",     "SecretLocDelete",		Category.System,          Expansion.AOS,    110),
  			new CommandEntry("Factions",            "GenerateFactions",	"DeleteFactions",		Category.System,          Expansion.None,   111),
  			new CommandEntry("Decorations",         "Decorate",         "DecorateDelete",		Category.Decoration,      Expansion.None,   113),
  			new CommandEntry("ML Decorations",      "DecorateML",		"DecorateMLDelete",		Category.Decoration,      Expansion.ML,     114),
  			new CommandEntry("SA Decorations",      "DecorateSA",		"DecorateSADelete",		Category.Decoration,      Expansion.SA,     115),
  			new CommandEntry("Spawners",		    "XmlLoad Spawns",	"WipeAllXmlSpawners",	Category.Spawn,           Expansion.None,   116),
              new CommandEntry("New Despise",         "SetupDespise",     "DeleteDespise",        Category.RevampedDungeon, Expansion.SA,     117),
              new CommandEntry("New Covetous",        "SetupNewCovetous", "DeleteCovetous",       Category.RevampedDungeon, Expansion.SA,     118),
              new CommandEntry("New Shame",           "GenerateNewShame", "DeleteShame",          Category.RevampedDungeon, Expansion.SA,     119),
              new CommandEntry("New Magincia",        "GenNewMagincia",   "DeleteNewMagincia",    Category.Decoration,      Expansion.None,   120),
              new CommandEntry("High Seas",           "DecorateHS",       "DeleteHS",             Category.Expansion,       Expansion.HS,     121),
              new CommandEntry("City Loyalty",        "SetupCityLoyaltySystem","DeleteCityLoyaltySystem",Category.System,     Expansion.SA,   122),
              new CommandEntry("Castle Blackthorn",   "GenBlackthorn",                null,       Category.RevampedDungeon, Expansion.SA,     123),
              new CommandEntry("TOL Decorations",     "DecorateTOL",                  null,       Category.Decoration,      Expansion.TOL,    124),
              new CommandEntry("New Wrong",           "GenWrongRevamp",               null,       Category.RevampedDungeon, Expansion.SA,     125),
              new CommandEntry("Kotl City",           "GenerateTreasuresOfKotlCity",  null,       Category.System,          Expansion.TOL,    126),
              new CommandEntry("Fillable Containers", "CheckFillables",               null,       Category.Spawn,           Expansion.None,   127, 5),
              new CommandEntry("Champ Spawns",        "GenChampSpawns",   "DelChampSpawns",       Category.Spawn,           Expansion.UOR,    128),
  		});
  
          public static bool WorldCreating { get; set; }
  
          public CreateWorld()
          {
          }
  
          public static void Initialize() 
          { 
              CommandSystem.Register("Createworld", AccessLevel.Administrator, new CommandEventHandler(Create_OnCommand));
  			CommandSystem.Register("DeleteWorld", AccessLevel.Administrator, new CommandEventHandler(Delete_OnCommand));
  			CommandSystem.Register("RecreateWorld", AccessLevel.Administrator, new CommandEventHandler(Recreate_OnCommand));
  		}
  
  		[Usage("CreateWorld [nogump]")]
  		[Description("Generates the world with a menu. If nogump argument is given, no gump will be displayed, all options will be assumed true, and the action will proceed immediately.")]
  		private static void Create_OnCommand(CommandEventArgs e)
  		{
  			if (String.IsNullOrEmpty(e.ArgString))
  			{
                  if (e.Mobile is PlayerMobile)
                      BaseGump.SendGump(new NewCreateWorldGump((PlayerMobile)e.Mobile, GumpType.Create));
                  else
  				    e.Mobile.SendGump(new CreateWorldGump(e, GumpType.Create));
  			}
  			else if (e.ArgString.ToLower().Equals("nogump"))
  			{
  				DoAllCommands(GumpType.Create, e.Mobile);
  			}
  			else
  			{
  				if (e.Mobile != null)
  					e.Mobile.SendMessage("Usage: CreateWorld [nogump]");
  			}
  		}
  
  		[Usage("DeleteWorld [nogump]")]
  		[Description("Undoes world generation with a menu. If nogump argument is given, no gump will be displayed, all options will be assumed true, and the action will proceed immediately.")]
  		private static void Delete_OnCommand(CommandEventArgs e)
  		{
  			if (String.IsNullOrEmpty(e.ArgString))
  			{
                  if (e.Mobile is PlayerMobile)
                      BaseGump.SendGump(new NewCreateWorldGump((PlayerMobile)e.Mobile, GumpType.Delete));
                  else
  				    e.Mobile.SendGump(new CreateWorldGump(e, GumpType.Delete));
  			}
  			else if (e.ArgString.ToLower().Equals("nogump"))
  			{
  				DoAllCommands(GumpType.Delete, e.Mobile);
  			}
  			else
  			{
  				if (e.Mobile != null)
  					e.Mobile.SendMessage("Usage: DeleteWorld [nogump]");
  			}
  		}
  
  		[Usage("RecreateWorld [nogump]")]
  		[Description("Re-generates the world with a menu. If nogump argument is given, no gump will be displayed, all options will be assumed true, and the action will proceed immediately.")]
  		private static void Recreate_OnCommand(CommandEventArgs e)
  		{
  			if (String.IsNullOrEmpty(e.ArgString))
  			{
  				e.Mobile.SendGump(new CreateWorldGump(e, GumpType.Recreate));
  			}
  			else if (e.ArgString.ToLower().Equals("nogump"))
  			{
  				DoAllCommands(GumpType.Recreate, e.Mobile);
  			}
  			else
  			{
  				if (e.Mobile != null)
  					e.Mobile.SendMessage("Usage: RecreateWorld [nogump]");
  			}
  
  		}
  
  		public static void DoAllCommands(GumpType type, Mobile from)
  		{
  			List<int> ids = new List<int>();
  			foreach (CommandEntry entry in Commands)
  			{
  				ids.Add(entry.CheckID);
  			}
  			DoCommands(ids.ToArray(), type, from);
  		}
  
  		public static void DoCommands(int[] selections, GumpType type, Mobile from)
  		{
  			World.Broadcast(0x35, false, "The world is generating. This may take some time...");
  			string prefix = Server.Commands.CommandSystem.Prefix;
  
              string error = null;
              WorldCreating = true;
  
  			foreach (int sel in selections)
  			{
  				foreach (CreateWorld.CommandEntry entry in CreateWorld.Commands)
  				{
  					if (entry.CheckID == sel)
  					{
  						switch (type)
  						{
  							case CreateWorld.GumpType.Create:
  								from.Say("Generating " + entry.Name);
  
                                  if (CanGenerate(entry, ref error))
                                  {
                                      if (entry.Delay > 0)
                                      {
                                          DoDelayedCommand(from, TimeSpan.FromMinutes(entry.Delay), prefix + entry.CreateCommand);
                                      }
                                      else
                                      {
                                          CommandSystem.Handle(from, prefix + entry.CreateCommand);
                                      }
  
                                      if (CreateWorldData.CreateTable.ContainsKey(sel))
                                          CreateWorldData.CreateTable[sel] = true;
                                  }
  
  								break;
  							case CreateWorld.GumpType.Delete:
  								if (!String.IsNullOrEmpty(entry.DeleteCommand))
  								{
  									from.Say("Deleting " + entry.Name);
  
gt: 									CommandSystem.Handle(from, prefix + entry.DeleteCommand);