/
etc/
lib/
src/Abilities/
src/Abilities/Skills/
src/Abilities/Spells/
src/Abilities/Spells/Enums/
src/Affects/
src/ArtheaConsole/
src/ArtheaConsole/Properties/
src/ArtheaGUI/Properties/
src/Clans/Enums/
src/Commands/Communication/
src/Commands/ItemCommands/
src/Connections/
src/Connections/Colors/
src/Connections/Enums/
src/Connections/Players/
src/Connections/Players/Enums/
src/Continents/
src/Continents/Areas/
src/Continents/Areas/Characters/
src/Continents/Areas/Characters/Enums/
src/Continents/Areas/Items/
src/Continents/Areas/Items/Enums/
src/Continents/Areas/Rooms/
src/Continents/Areas/Rooms/Enums/
src/Continents/Areas/Rooms/Exits/
src/Creation/
src/Creation/Attributes/
src/Creation/Interfaces/
src/Database/
src/Database/Interfaces/
src/Environment/
src/Properties/
src/Scripts/Enums/
src/Scripts/Interfaces/
#region Arthea License

/***********************************************************************
*  Arthea MUD by R. Jennings (2007)      http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses. *
***********************************************************************/

#endregion

using System;
using System.Collections.Generic;
using Arthea.Commands.Admin;
using Arthea.Commands.Communication;
using Arthea.Commands.Information;
using Arthea.Commands.ItemCommands;
using Arthea.Commands.PlayerCommands;
using Arthea.Connections.Players;
using Arthea.Continents.Areas.Rooms.Enums;
using Arthea.Environment;

namespace Arthea.Commands
{
    /// <summary>
    /// Maintains a list of commands.
    /// </summary>
    public class CommandList : List<Command>
    {
        #region [rgn] Constructors (1)

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandList"/> class.
        /// </summary>
        public CommandList()
        {
            Log.Info("Initializing commands..");
            foreach (string direction in Enum.GetNames(typeof (Direction)))
                Add(new MoveCommand(direction.ToLower()));
            Add(new QuitCommand());
            Add(new SayCommand());
            Add(new ShutdownCommand());
            Add(new WhoCommand());
            Add(new TitleCommand());
            Add(new SaveCommand());
            Add(LookCommand.Instance);
            Add(new HelpCommand());
            Add(new LockDownCommand());
            Add(new AutoLookCommand());
            Add(new ColorCommand());
            Add(new GetCommand());
            Add(new InventoryCommand());
            Add(new PromptCommand());
            Add(new DropCommand());
            Add(new EquipmentCommand());
            Add(new WearCommand());
            Add(new RemoveCommand());
            Add(new KillCommand());
            Add(new PeaceCommand());
            Add(new EditCommand());
            Add(new ExitsCommand());
            Add(new RebootCommand());
            Add(new GrantCommand());
            Add(new CastCommand());
            Add(new CreateCommand());
            Add(new CommandsCommand());
            Add(new AreasCommand());
            Add(new PasswordCommand());
            Add(new GotoCommand());
            Add(new ListCommand());
            Add(new DeleteCommand());
            Add(new AfkCommand());
            Add(new CompressCommand());
        }

        #endregion [rgn]

        #region [rgn] Methods (4)

        // [rgn] Public Methods (4)

        /// <summary>
        /// Finds the admin.
        /// </summary>
        /// <returns>an array of admin commands</returns>
        public Command[] FindAdmin()
        {
            List<Command> commands = new List<Command>();

            foreach (Command cmd in this)
            {
                if (cmd.Level == Levels.Admin)
                {
                    commands.Add(cmd);
                }
            }
            return commands.ToArray();
        }

        /// <summary>
        /// Finds the name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>a command</returns>
        public Command FindName(String name)
        {
            if (!name) return null;

            return Find(delegate(Command cmd) { return name == cmd.Name; });
        }

        /// <summary>
        /// Looks up a command in the list for a player
        /// </summary>
        /// <param name="name">the name of the command</param>
        /// <param name="player">the player invoking the lookup</param>
        /// <returns>a command</returns>
        public Command Lookup(String name, Player player)
        {
            foreach (Command cmd in this)
            {
                if (name.IsPrefixOf(cmd.Name)
                    || name.IsPrefixOf(cmd.Alias))
                {
                    if (cmd.Level == Levels.Admin && !player.Powers[cmd])
                    {
                        return new RestrictedCommand(cmd.Name);
                    }
                    else if (cmd.Level > player.Level)
                    {
                        return new RestrictedCommand(cmd.Name, cmd.Level);
                    }
                    else
                    {
                        return cmd;
                    }
                }
            }
            return new UnknownCommand(name);
        }

        /// <summary>
        /// Returns an array of commands for a player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <returns>an array of c ommands</returns>
        public Command[] ToArray(Player player)
        {
            List<Command> cmds = new List<Command>();

            foreach (Command cmd in this)
            {
                if ((cmd.Level == Levels.Admin && !player.Powers[cmd])
                    || cmd.Level > player.Level)
                {
                    continue;
                }

                cmds.Add(cmd);
            }

            return cmds.ToArray();
        }

        #endregion [rgn]
    }
}