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

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

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

    _command: null,
    _items: null,

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

    init: function (options) {
        options = options || {};
        this._items = new PD.BCL.List();
        PD.Web.Structure.mixin(options, this);
        this.onPreInit(options);
        this.onInit(options);
        this._assignCommand(options);
    },

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

    get_items: function () {
        return this._items;
    },

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

    onPreInit: function (e) {
        this._setDefaultValues(e);
        this._assignCommand(e);
    },

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

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

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

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

    onClick: function (e) {

        switch (true) {
            case PD.Type.isFunction(this._command):
                this._command(e);
                break;
            case PD.Type.isObject(this._command):
                this._command.execute(e);
                break;
        }

    },

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

    getSettings: function () {

        var result = $.extend(this.data().toObject(), {
            text: this._applyTemplate(),
            encoded: false,
            attributes: {
                "data-id": this.data("id")
            }
        });
            
        return result;

    },

    open: function (e) {

        var args = {},
            open = this.data("open"),
            node = $(e.menu.element).find("[data-id=" + this.data("id") + "]").closest("LI");

        if (!PD.Type.isFunction(open)) {
            return;
        }

        args = new PD.Web.UI.Controls.ContextMenuOpenEventArgs({
            item: this,
            node: node
        });

        args = $.extend(args, e);

        open(args);
    },

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

    _applyTemplate: function () {

        var template = this.data("template"),
            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 (e) {

        var paramIsFunction = PD.Type.isFunction(e),
            paramIsObjectWithCommand = PD.Type.isObject(e) && PD.Type.isFunction(e.command),
            paramIsCommandObject = PD.Type.isObject(e) && PD.Type.isFunction(e.execute);

        switch (true) {
            case paramIsFunction || paramIsCommandObject:
                this._command = e;
                break;
            case paramIsObjectWithCommand:
                this._command = e.command;
                break;
        }

    }

});