/// class PD.Commands.CommandCollection

PD.Commands.CommandCollection =
Class.extend({

    //===================================
    // FIELDS
    //===================================

    _collection: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {
        this._collection = {};
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    add: function (command) {
        if (this.commandIsValid(command)
            && !this.commandIsRegistered(command)) {
            this._collection[command.get_id()] = command;
        }
    },

    remove: function (command) {
        if (this.commandIsRegistered(command)) {
            this._collection[command]
        }
    },

    each: function (fn) {
        for (var key in this._collection) {
            if (fn != null) {
                fn(key, this._collection[key]);
            }
        }
    },

    getByPath: function (commandPath) {

        var result = null,
            pieces = PD.Commands.CommandUtil.splitPath(commandPath);

        if (pieces.commandId != - null) {
            result = this._collection[pieces.commandId];
        }

        return result;

    },

    getById: function (commandId) {
        return this._collection[commandId];
    },

    // TODO: turn this check into a static method on the command class?
    commandIsValid: function (command) {
        return command.get_id != null 
            && command.setState != null 
            && command.execute != null;
    },

    commandIsRegistered: function (command) {
        if (this.commandIsValid(command)) {
            return (command.get_id() in this._collection);
        }
        return false;
    }

});