/// class PD.Web.UI.Controls.ContextMenuCommandItem
/// using PD.Web.UI.Controls.ContextMenuItem

PD.Web.UI.Controls.ContextMenuCommandItem =
PD.Web.UI.Controls.ContextMenuItem.extend({

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

    _context: null,
    _contextId: null,
    _command: null,
    _commandId: null,
    _items: null,

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

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

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

    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
    //===================================

    onClick: function (e) {
        if (this._command != null) {
            this.get_commands().execute(this._commandId);
        }
    },

    open: function (e) {
        this._super(e);

        var that = this,
            menu = e.menu,
            $node = $(e.menu.element).find("[data-id=" + this.data("id") + "]").closest("LI"),
            command = null;

        this._ensureCommandInfo();

        command = this.get_command();

        if (command == null) {
            return;
        }

        var bindingFn = function (e) {

            var template = null,
                $template = null,
                $item = $node.find(".-pd-menu-item");

            $item.empty();

            template = that._applyTemplate({
                id: that.data("id"),
                text: e.label,
                imageUrl: e.icon
            });

            $template = $(template);

            if (e.visible) {
                $node.removeClass("hidden");
            }
            else {
                $node.addClass("hidden");
            }

            menu.enable($node, e.enabled);

            $item.html($template.html());

        };

        command.bind(bindingFn);
        command.update(bindingFn);

    },

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

    _applyTemplate: function (data) {

        var template = this.data("template"),
            data = data || this.data().toObject();

        if (!template) {
            template = new PD.Web.UI.Controls.ContextMenuItemTemplate();
        }

        return template.apply(data);

    },

    _setDefaultValues: function (e) {
        this.data().add("imageUrl", e.imageUrl || null);
        this.data().add("id", uuid.v4());
        this.data().add("enable", e.enable != null ? e.enable : true);
        this.data().add("open", e.open != null ? e.open : true);
        this.data().add("overflow", e.overflow || "auto");
        this.data().add("text", e.text || "");
        this.data().add("type", "button");
        this.data().add("cssClass", e.cssClass || "");
    },

    _assignCommand: function () {
    },

    _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
    }

});