/// class PD.Commands.CommandContextCollection

PD.Commands.CommandContextCollection =
Class.extend({

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

    _manager: null,
    _collection: null,
    _currentContextId: null,

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

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

    //===================================
    // PROPERTIES
    //===================================

    get_currentContextId: function () {
        return this._currentContextId;
    },

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

    current: function (contextId) {
        if (contextId !== undefined) {
            this.add(contextId);
            this._currentContextId = contextId;
        }
        else {
            if (this._isContextCached(this._currentContextId)) {
                return this._collection[this._currentContextId];
            }
        }
    },

    add: function (contextId) {
        if (!this._isContextCached(contextId)) {
            this._collection[contextId] = new PD.Commands.CommandContext({
                contextId: contextId,
                manager: this._manager
            });
        }
    },

    remove: function (contextId) {
        if (this._isContextCached(contextId)) {
            delete this._collection[contextId];
        }
    },

    getById: function (contextId) {

        var result = null;

        if (contextId != null) {
            result = this._collection[contextId];
        }

        return result;

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isContextCached: function (contextId) {
        return this._collection.hasOwnProperty(contextId);
    },

    _isValidContext: function (context) {
        return context != null
            && context.hasOwnProperty("id");
    },

    _mergeState: function (oldState, newState) {
        for (var key in newState) {
            oldState[key] = newState[key];
        }
        return oldState;
    }

});