context: using WCell.RealmServer.Chat;
  using WCell.RealmServer.Help.Tickets;
  using WCell.RealmServer.Lang;
  using WCell.Util.Commands;
  using WCell.RealmServer.Entities;
  using WCell.Util.Graphics;
  
  namespace WCell.RealmServer.Commands
  {
  	public class TicketCommand : RealmServerCommand
  	{
  		protected override void Initialize()
  		{
  			Init("Ticket", "Tickets");
  			EnglishDescription = "Provides all commands necessary to work with Tickets.";
  		}
  
  		#region Select
  		public class SelectPlayerTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Select", "Sel");
  				EnglishParamInfo = "[<playername>]";
  				EnglishDescription = "Selects the Ticket of the targeted Player or the Player with the given name.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var name = trigger.Text.NextWord();
  				var chr = trigger.Args.GetCharArgumentOrTarget(trigger, name);
  				var handler = trigger.Args.TicketHandler;
  
  				if (chr != null && chr.IsInWorld)
  				{
  					var ticket = chr.Ticket;
  					var oldHandler = ticket.Handler;
  					if (oldHandler != null && oldHandler.Role > handler.Role)
  					{
  						trigger.Reply("Ticket is already being handled by: " + oldHandler.Name);
  					}
  					else
  					{
  						if (oldHandler != null)
  						{
  							trigger.Reply("Taking over Ticket from: " + oldHandler.Name);
  							oldHandler.SendMessage("The Ticket you were handling by " + ticket.Owner + " is now handled by: " + handler);
  						}
  						ticket.Handler = handler;
  					}
  				}
  				else
  				{
  					trigger.Reply("Selected player is offline or does not exist: " + name);
  				}
  			}
  		}
  		#endregion
  
  		#region SelectNext
  		public class SelectNextTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("SelectNext", "SelNext", "Next", "N");
  				EnglishDescription = "Selects the next unhandled ticket.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = TicketMgr.Instance.HandleNextUnhandledTicket(trigger.Args.TicketHandler);
  				if (ticket == null)
  				{
  					trigger.Reply("There are currently no unhandled Tickets.");
  				}
  				else
  				{
  					ticket.DisplayFormat(trigger, "Now Selected: ");
  				}
  			}
  		}
  		#endregion
  
  		#region Unselect
  		public class UnselectTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Unselect", "U");
  				EnglishDescription = "Unselects the current Ticket.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = trigger.Args.TicketHandler.HandlingTicket;
  				ticket.Handler = null;
  				trigger.Reply("Done.");
  			}
  
  			public override bool RequiresActiveTicket
  			{
  				get { return true; }
  			}
  		}
  		#endregion
  
  		#region Goto
  		public class GotoTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Goto", "Go", "Tele");
  				EnglishDescription = "Teleports directly to the ticket issuer or his/her ticket posting location if offline.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = trigger.Args.TicketHandler.HandlingTicket;
  				var owner = ticket.Owner;
  				if (owner == null)
  				{
  					trigger.Reply("The owner of this Ticket is offline.");
  					trigger.Args.Target.TeleportTo(ticket.Map, ticket.Position);
  				}
  				else
  				{
  					trigger.Args.Target.TeleportTo(owner);
  				}
  			}
  
  			public override bool RequiresActiveTicket
  			{
  				get
  				{
  					return true;
  				}
  			}
  
  			public override bool RequiresIngameTarget
  			{
  				get { return true; }
  			}
  		}
  		#endregion
  
  		#region Notify
  		public class NotifyTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Notify", "Msg", "M");
  				EnglishDescription = "Sends a notification to the person who issued the current ticket.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = trigger.Args.TicketHandler.HandlingTicket;
  				var owner = ticket.Owner;
  				if (owner == null)
  				{
  					trigger.Reply("The owner of this Ticket is offline.");
  				}
  				else
  				{
  					var text = trigger.Text.Remainder;
  					owner.ExecuteInContext(() => owner.Notify(text));
  					trigger.Reply(RealmLangKey.Done);
  				}
  			}
  
  			public override bool RequiresActiveTicket
  			{
  				get { return true; }
  			}
  
  			public override bool RequiresIngameTarget
  			{
  				get { return true; }
  			}
  		}
  		#endregion
  
  		#region List
  		public class ListTicketsCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("List", "L");
  				EnglishDescription = "Shows all currently active tickets.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var tickets = TicketMgr.Instance.GetAllTickets();
  				if (tickets.Length == 0)
  				{
  					trigger.Reply("There are no active tickets.");
  				}
  				else
  				{
  					foreach (var ticket in tickets)
  					{
  						trigger.Reply("{0} by {1}{2} (age: {3})", ticket.Type, ticket.OwnerName,
  									  ticket.Owner != null ? "" : ChatUtility.Colorize(" (Offline)", Color.Red, true), ticket.Age);
  					}
  				}
  			}
  
  			public override bool RequiresTicketHandler
  			{
  				get
  				{
  					return false;
  				}
  			}
  		}
  		#endregion
  
  		#region Current
  		public class CurrentTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Current", "Cur");
  				EnglishDescription = "Shows the Ticket you are currently handling.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = trigger.Args.TicketHandler.HandlingTicket;
  				ticket.DisplayFormat(trigger, "Now Selected: ");
  			}
  
  			public override bool RequiresActiveTicket
  			{
  				get
  				{
  					return true;
  				}
  			}
  		}
  		#endregion
  
  		#region Show
  		public class ShowTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Show", "S");
  				EnglishDescription = "Shows the Target's currently active Ticket.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var target = trigger.Args.Target as Character;
  				if (target == null)
  				{
  					trigger.Reply("Invalid selection.");
  					return;
  				}
  
  				var ticket = target.Ticket;
  				if (ticket != null)
  				{
  					ticket.DisplayFormat(trigger, "");
  				}
  				else
  				{
  					trigger.Reply("{0} has no active Ticket.", target.Name);
  				}
  			}
  
  			public override bool RequiresIngameTarget
  			{
  				get { return true; }
  			}
  		}
  		#endregion
  
  		#region Delete
  		public class DeleteTicketCommand : TicketSubCmd
  		{
  			protected override void Initialize()
  			{
  				Init("Delete", "Del", "D", "Remove", "R");
  				EnglishDescription = "Deletes the current Ticket.";
  			}
  
  			public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
  			{
  				var ticket = trigger.Args.TicketHandler.HandlingTicket;
  				trigger.Reply(ticket.OwnerName + "'s Ticket has been deleted.");
  				ticket.Delete();
  			}
  
  			public override bool RequiresActiveTicket
  			{
  
gt: 				get { return true; }