/// class PD.Web.UI.Controls.CommandItem

PD.Web.UI.Controls.CommandItem =
PD.Web.UI.Controls.Item.extend({

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

    _uid: null,
    _context: null,
    _contextId: null,
    _command: null,
    _commandId: null,
    _commandInfo: null,

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

    init: function (options) {
        this._super(options);
        this._contextId = options.contextId;
        this._commandId = options.commandId;
    },

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

    get_uid: function () {
        return this._uid;
    },

    get_context: function () {
        
        var result = PD.context.getContextById(this._contextId);

        if (result == null) {
            result = PD.context.get_default();
        }

        return result;

    },

    get_commands: function () {
        return this.get_context().get_commands();
    },

    get_command: function () {
        return this._command;
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    onActivate: function (e) {
        this.setupCommand();
    },

    onCommandChange: function (e) {
        // do nothing on purpose
    },

    //===================================
    // PROTECTED METHODS
    //===================================

    setupCommand: function () {

        var that = this,
            node = null,
            toolbar = null,
            commandEventHandlerFn = null;

        this._ensureCommandInfo();

        if (!this._isCommandResolved()) {
            return;
        }

        node = this.getNode();

        toolbar = this.getNode().closest(".k-toolbar").data("kendoToolBar");

        commandEventHandlerFn = function (e) {
            e.toolbar = toolbar;
            e.node = node;
            e.command = that.get_command();
            e.context = that.get_context();
            that.onCommandChange(e);
        };

        this.get_command().bind(commandEventHandlerFn);
        this.get_command().update(commandEventHandlerFn);

    },

    executeCommand: function (args) {

        var info = this._commandInfo;

        if (info != null && info.command) {
            info.group.execute(this._commandId, args);
        }

    },

    _setDefaultValues: function (e) {
        //overridable
    },

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

    _ensureCommandInfo: function () {

        if (this._commandInfo != null
                || this._commandId == null) {
            return;
        }

        this._commandInfo = this.get_commands().resolve(this._commandId);

        if (this._isCommandResolved()) {
            this._command = this._commandInfo.command;
        }

    },

    _isCommandResolved: function () {
        return this._commandInfo != null
            this._commandInfo.command != null
    }

});