context: using System;
  using System.IO;
  using System.Collections;
  using System.Collections.Generic;
  using Server;
  using Server.Items;
  using Server.Engines.Quests.Haven;
  using Server.Engines.Quests.Necro;
  
  namespace Server.Commands
  {
  	public class DecorateMag
  	{
  		public static void Initialize()
  		{
  			CommandSystem.Register( "DecorateMag", AccessLevel.Administrator, new CommandEventHandler( DecorateMag_OnCommand ) );
  		}
  
  		[Usage( "DecorateMag" )]
  		[Description( "Generates world decoration." )]
  		private static void DecorateMag_OnCommand( CommandEventArgs e )
  		{
  			m_Mobile = e.Mobile;
  			m_Count = 0;
  
  			m_Mobile.SendMessage( "Generating world decoration, please wait." );
  
  			Generate( "Data/Decoration/RuinedMaginciaTram", Map.Trammel );
  			Generate( "Data/Decoration/RuinedMaginciaFel", Map.Felucca );
  
  
  			m_Mobile.SendMessage( "World generating complete. {0} items were generated.", m_Count );
  		}
  
  		public static void Generate( string folder, params Map[] maps )
  		{
  			if ( !Directory.Exists( folder ) )
  				return;
  
  			string[] files = Directory.GetFiles( folder, "*.cfg" );
  
  			for ( int i = 0; i < files.Length; ++i )
  			{
  				ArrayList list = DecorationListMag.ReadAll( files[i] );
  
  				for ( int j = 0; j < list.Count; ++j )
  					m_Count += ((DecorationListMag)list[j]).Generate( maps );
  			}
  		}
  
  		private static Mobile m_Mobile;
  		private static int m_Count;
  	}
  
  	public class DecorationListMag
  	{
  		private Type m_Type;
  		private int m_ItemID;
  		private string[] m_Params;
  		private ArrayList m_Entries;
  
  		public DecorationListMag()
  		{
  		}
  
  		private static Type typeofStatic = typeof( Static );
  		private static Type typeofLocalizedStatic = typeof( LocalizedStatic );
  		private static Type typeofBaseDoor = typeof( BaseDoor );
  		private static Type typeofAnkhWest = typeof( AnkhWest );
  		private static Type typeofAnkhNorth = typeof( AnkhNorth );
  		private static Type typeofBeverage = typeof( BaseBeverage );
  		private static Type typeofLocalizedSign = typeof( LocalizedSign );
  		private static Type typeofMarkContainer = typeof( MarkContainer );
  		private static Type typeofWarningItem = typeof( WarningItem );
  		private static Type typeofHintItem = typeof( HintItem );
  		private static Type typeofCannon = typeof( Cannon );
  		private static Type typeofSerpentPillar = typeof( SerpentPillar );
  
  		public Item Construct()
  		{
  			Item item;
  
  			try
  			{
  				if ( m_Type == typeofStatic )
  				{
  					item = new Static( m_ItemID );
  				}
  				else if ( m_Type == typeofLocalizedStatic )
  				{
  					int labelNumber = 0;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "LabelNumber" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  							{
  								labelNumber = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  								break;
  							}
  						}
  					}
  
  					item = new LocalizedStatic( m_ItemID, labelNumber );
  				}
  				else if ( m_Type == typeofLocalizedSign )
  				{
  					int labelNumber = 0;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "LabelNumber" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  							{
  								labelNumber = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  								break;
  							}
  						}
  					}
  
  					item = new LocalizedSign( m_ItemID, labelNumber );
  				}
  				else if ( m_Type == typeofAnkhWest || m_Type == typeofAnkhNorth )
  				{
  					bool bloodied = false;
  
  					for ( int i = 0; !bloodied && i < m_Params.Length; ++i )
  						bloodied = ( m_Params[i] == "Bloodied" );
  
  					if ( m_Type == typeofAnkhWest )
  						item = new AnkhWest( bloodied );
  					else
  						item = new AnkhNorth( bloodied );
  				}
  				else if ( m_Type == typeofMarkContainer )
  				{
  					bool bone = false;
  					bool locked = false;
  					Map map = Map.Malas;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i] == "Bone" )
  						{
  							bone = true;
  						}
  						else if ( m_Params[i] == "Locked" )
  						{
  							locked = true;
  						}
  						else if ( m_Params[i].StartsWith( "TargetMap" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								map = Map.Parse( m_Params[i].Substring( ++indexOf ) );
  						}
  					}
  
  					MarkContainer mc = new MarkContainer( bone, locked );
  
  					mc.TargetMap = map;
  					mc.Description = "strange location";
  
  					item = mc;
  				}
  				else if ( m_Type == typeofHintItem )
  				{
  					int range = 0;
  					int messageNumber = 0;
  					string messageString = null;
  					int hintNumber = 0;
  					string hintString = null;
  					TimeSpan resetDelay = TimeSpan.Zero;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "Range" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								range = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "WarningString" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								messageString = m_Params[i].Substring( ++indexOf );
  						}
  						else if ( m_Params[i].StartsWith( "WarningNumber" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								messageNumber = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "HintString" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								hintString = m_Params[i].Substring( ++indexOf );
  						}
  						else if ( m_Params[i].StartsWith( "HintNumber" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								hintNumber = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "ResetDelay" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								resetDelay = TimeSpan.Parse( m_Params[i].Substring( ++indexOf ) );
  						}
  					}
  
  					HintItem hi = new HintItem( m_ItemID, range, messageNumber, hintNumber );
  
  					hi.WarningString = messageString;
  					hi.HintString = hintString;
  					hi.ResetDelay = resetDelay;
  
  					item = hi;
  				}
  				else if ( m_Type == typeofWarningItem )
  				{
  					int range = 0;
  					int messageNumber = 0;
  					string messageString = null;
  					TimeSpan resetDelay = TimeSpan.Zero;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "Range" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								range = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "WarningString" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								messageString = m_Params[i].Substring( ++indexOf );
  						}
  						else if ( m_Params[i].StartsWith( "WarningNumber" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								messageNumber = Utility.ToInt32( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "ResetDelay" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								resetDelay = TimeSpan.Parse( m_Params[i].Substring( ++indexOf ) );
  						}
  					}
  
  					WarningItem wi = new WarningItem( m_ItemID, range, messageNumber );
  
  					wi.WarningString = messageString;
  					wi.ResetDelay = resetDelay;
  
  					item = wi;
  				}
  				else if ( m_Type == typeofCannon )
  				{
  					CannonDirection direction = CannonDirection.North;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "CannonDirection" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								direction = (CannonDirection)Enum.Parse( typeof( CannonDirection ), m_Params[i].Substring( ++indexOf ), true );
  						}
  					}
  
  					item = new Cannon( direction );
  				}
  				else if ( m_Type == typeofSerpentPillar )
  				{
  					string word = null;
  					Rectangle2D destination = new Rectangle2D();
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "Word" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								word = m_Params[i].Substring( ++indexOf );
  						}
  						else if ( m_Params[i].StartsWith( "DestStart" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								destination.Start = Point2D.Parse( m_Params[i].Substring( ++indexOf ) );
  						}
  						else if ( m_Params[i].StartsWith( "DestEnd" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  								destination.End = Point2D.Parse( m_Params[i].Substring( ++indexOf ) );
  						}
  					}
  
  					item = new SerpentPillar( word, destination );
  				}
  				else if ( m_Type.IsSubclassOf( typeofBeverage ) )
  				{
  					BeverageType content = BeverageType.Liquor;
  					bool fill = false;
  
  					for ( int i = 0; !fill && i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "Content" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  							{
  								content = (BeverageType)Enum.Parse( typeof( BeverageType ), m_Params[i].Substring( ++indexOf ), true );
  								fill = true;
  							}
  						}
  					}
  
  					if ( fill )
  						item = (Item)Activator.CreateInstance( m_Type, new object[]{ content } );
  					else
  						item = (Item)Activator.CreateInstance( m_Type );
  				}
  				else if ( m_Type.IsSubclassOf( typeofBaseDoor ) )
  				{
  					DoorFacing facing = DoorFacing.WestCW;
  
  					for ( int i = 0; i < m_Params.Length; ++i )
  					{
  						if ( m_Params[i].StartsWith( "Facing" ) )
  						{
  							int indexOf = m_Params[i].IndexOf( '=' );
  
  							if ( indexOf >= 0 )
  							{
  								facing = (DoorFacing)Enum.Parse( typeof( DoorFacing ), m_Params[i].Substring( ++indexOf ), true );
  								break;
  							}
  						}
  					}
  
  					item = (Item)Activator.CreateInstance( m_Type, new object[]{ facing } );
  				}
  				else
  				{
  
gt: 					item = (Item)Activator.CreateInstance( m_Type );