context: using System;
  using System.Collections.Generic;
  using Cell.Core;
  using WCell.Constants;
  using WCell.Constants.Spells;
  using WCell.Core;
  using WCell.RealmServer.Entities;
  using WCell.RealmServer.GameObjects;
  using WCell.RealmServer.Handlers;
  using WCell.RealmServer.Items;
  using WCell.RealmServer.Looting;
  using WCell.RealmServer.NPCs;
  using WCell.RealmServer.Quests;
  using WCell.RealmServer.Spells;
  using WCell.Util.Commands;
  using WCell.Intercommunication.DataTypes;
  using WCell.RealmServer.Debugging;
  using WCell.RealmServer.Global;
  using WCell.Util.ObjectPools;
  using WCell.Util.Threading;
  using WCell.Constants.World;
  using WCell.RealmServer.Misc;
  using WCell.Constants.Updates;
  
  namespace WCell.RealmServer.Commands
  {
  	#region SendRaw
  	public class SendRawCommand : RealmServerCommand
  	{
  		protected SendRawCommand() { }
  
  		protected override void Initialize()
  		{
  			Init("SendRaw");
  			EnglishParamInfo = "<opcode> [<int value1> [<int value2> ...]]";
  			EnglishDescription = "Sends a raw packet to the client";
  		}
  
  		public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  		{
  			var opcode = trigger.Text.NextEnum(RealmServerOpCode.Unknown);
  			if (opcode == RealmServerOpCode.Unknown)
  			{
  				trigger.Reply("Invalid OpCode.");
  				return;
  			}
  
  			using (var packet = new RealmPacketOut(opcode))
  			{
  				while (trigger.Text.HasNext)
  				{
  					var val = trigger.Text.NextInt();
  					packet.Write(val);
  				}
  				((Character)trigger.Args.Target).Client.Send(packet);
  			}
  		}
  
  		public override ObjectTypeCustom TargetTypes
  		{
  			get { return ObjectTypeCustom.Player; }
  		}
  	}
  	#endregion
  
  	#region SendPacket
  	public class SendPacketCommand : RealmServerCommand
  	{
  		protected SendPacketCommand() { }
  
  		protected override void Initialize()
  		{
  			Init("SendPacket", "SendP");
  			EnglishParamInfo = "<packet> <args>";
  			EnglishDescription = "Sends the given packet with corresponding args to the client";
  		}
  
  		#region SendSpellDamageLog
  		public class SendSpellDamageLogCommand : SubCommand
  		{
  			protected SendSpellDamageLogCommand() { }
  
  			protected override void Initialize()
  			{
  				Init("SpellLog", "SLog");
  				EnglishParamInfo = "[<unkBool> [<flags> [<spell> [<damage> [<overkill> [<schools> [<absorbed> [<resisted> [<blocked>]]]]]]]]]";
  				EnglishDescription = "Sends a SpellMissLog packet to everyone in the area where you are the caster and everyone within 10y radius is the targets.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var unkBool = trigger.Text.NextBool();
  				var flags = trigger.Text.NextEnum(SpellLogFlags.SpellLogFlag_0x1);
  				var spell = trigger.Text.NextEnum(SpellId.ClassSkillFireballRank1);
  				var damage = trigger.Text.NextUInt(10);
  				var overkill = trigger.Text.NextUInt(0);
  				var schools = trigger.Text.NextEnum(DamageSchoolMask.Fire);
  				var absorbed = trigger.Text.NextUInt(0);
  				var resisted = trigger.Text.NextUInt(0);
  				var blocked = trigger.Text.NextUInt(0);
  
  				CombatLogHandler.SendMagicDamage(trigger.Args.Target, trigger.Args.User, spell, damage, overkill, schools, absorbed, resisted, blocked, unkBool, flags);
  			}
  		}
  		#endregion
  
  		#region SendBGError
  		public class SendBGErrorCommand : SubCommand
  		{
  			protected SendBGErrorCommand() { }
  
  			protected override void Initialize()
  			{
  				Init("BGError", "BGErr");
  				EnglishParamInfo = "<err> [<bg>]";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var err = trigger.Text.NextEnum(BattlegroundJoinError.None);
  
  				BattlegroundHandler.SendBattlegroundError(trigger.Args.Character, err);
  			}
  		}
  		#endregion
  
          #region SendTotemCreated
          public class SendTotemCreated : SubCommand
          {
              protected SendTotemCreated() { }
  
              protected override void Initialize()
              {
                  Init("TotemCreated", "TC");
                  EnglishParamInfo = "[<spellId>]";
              }
  
              public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
              {
                  var spellId = trigger.Text.NextEnum(SpellId.EarthElementalTotem);
                  var spell = SpellHandler.Get(spellId);
                  if(spell == null)
                      trigger.Reply("Invalid spell id {0} supplied", spellId);
                  if(!TotemHandler.SendTotemCreated(trigger.Args.Character, spell, EntityId.Zero))
                  {
                      trigger.Reply("Error sending packet");
                      return;
                  }
  
                  trigger.Reply("Sent");
              }
          }
          #endregion
  
          public override ObjectTypeCustom TargetTypes
  		{
  			get { return ObjectTypeCustom.Player; }
  		}
  	}
  	#endregion
  
  	#region SendGossipPOI
  	public class POICommand : RealmServerCommand
  	{
  		protected POICommand() { }
  
  		protected override void Initialize()
  		{
  			Init("POI");
  			EnglishParamInfo = "[-[d][f] <Data> <Flags>] <x> <y> [<name>]";
  			EnglishDescription = "Sends a Point of interest entry to the target (shows up on the minimap while not too close).";
  		}
  
  		public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  		{
  			var mod = trigger.Text.NextModifiers();
  			int Data;
  			GossipPOIFlags Flags;
  			if (mod.Contains("d"))
  			{
  				Data = trigger.Text.NextInt(0);
  			}
  			else
  			{
  				Data = 0;
  			}
  			if (mod.Contains("f"))
  			{
  				Flags = trigger.Text.NextEnum(GossipPOIFlags.None);
  			}
  			else
  			{
  				Flags = GossipPOIFlags.Six;
  			}
  			var X = trigger.Text.NextFloat();
  			var Y = trigger.Text.NextFloat();
              var Icon = 7;
              var Name = trigger.Text.Remainder;
  
  			if (Name.Length == 0)
  			{
  				Name = trigger.Args.User.Name;
  			}
  
  			GossipHandler.SendGossipPOI(trigger.Args.Target as Character, Flags, X, Y, Data, Icon, Name);
  		}
  
  		public override ObjectTypeCustom TargetTypes
  		{
  			get { return ObjectTypeCustom.Player; }
  		}
  	}
  	#endregion
  
  	#region ModAuras
  	public class ModAurasCommand : RealmServerCommand
  	{
  		protected ModAurasCommand() { }
  
  		protected override void Initialize()
  		{
  			base.Init("ModAuras", "MAura");
  			EnglishParamInfo = "<n> <subcommand> ...";
  			EnglishDescription = "Modifies the nth Aura of the target";
  		}
  
  		public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  		{
  			ProcessNth(trigger);
  		}
  
  		public class ModLevelCommand : SubCommand
  		{
  			protected ModLevelCommand() { }
  
  			protected override void Initialize()
  			{
  				base.Init("Level", "Lvl", "L");
  				EnglishParamInfo = "<AuraLevel>";
  				EnglishDescription = "Modifies the Level of the nth Aura.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var n = ((RealmServerNCmdArgs)trigger.Args).N - 1;
  				var aura = trigger.Args.Target.Auras.GetAt(n);
  
  				if (aura != null)
  				{
  					ModPropCommand.ModProp(aura, aura.GetType().GetProperty("Level"), trigger);
  				}
  				else
  				{
  					trigger.Reply("There aren't " + n + " Auras.");
  				}
  			}
  		}
  
  		public class ModFlagsCommand : SubCommand
  		{
  			protected ModFlagsCommand() { }
  
  			protected override void Initialize()
  			{
  				base.Init("Flags", "Fl", "F");
  				EnglishParamInfo = "<AuraFlags>";
  				EnglishDescription = "Modifies the Flags of the nth Aura.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var n = ((RealmServerNCmdArgs)trigger.Args).N - 1;
  				var aura = trigger.Args.Target.Auras.GetAt(n);
  
  				if (aura != null)
  				{
  					ModPropCommand.ModProp(aura, aura.GetType().GetProperty("Flags"), trigger);
  				}
  				else
  				{
  
gt: 					trigger.Reply("There aren't " + n + " Auras.");