/// class PD.Web.UI.Controls.ToolbarSegment

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

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

    _originalContext: null,
    _context: null,
    _items: null,
    _options: null,
    _eventArgs: null,

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

    init: function (options) {
        this._options = PD.BCL.Util.ArgumentTransformer.toObject("context", options);
        this._originalContext = this._options.context;
        this._items = new PD.BCL.List();
    },

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

    get_context: function (context) {
        this._ensureContext();
        return this._context;
    },

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

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

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

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

    meetsLoadConditions: function (e) {
        return true;
    },

    refreshContext: function (toolbar) {

        var that = this;

        this._eventArgs = this._getEventArgs(toolbar);

        this.onRefresh(this._eventArgs);

        this._items.each(function (index, item) {
            item.refreshContext(that._eventArgs);
        });

    },

    click: function (toolbar, item) {

        this.onClick(this._eventArgs);

        item.onClick(this._eventArgs);

    },

    onRefresh: function (args) {
        //do nothing on purpose
    },

    onClick: function (args) {
        //do nothing on purpose
    },

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

    _ensureContext: function () {
        if (this._context === null) {
            this._context = this._transformContext(this._originalContext);
        }
    },

    _getEventArgs: function (toolbar) {

        this._context = this._transformContext(this._originalContext);

        return {
            toolbar: toolbar,
            context: this._context
        };

    },

    _transformContext: function (context) {

        var result = null;

        switch (true) {
            case PD.Type.isFunction(context):
                result = context(this);
                break;
            case PD.Type.isObject(context):
                result = context;
                break;
            default:
                result = {};
                break;
        }

        return result;

    }

});


PD.BCL.Util.ArgumentTransformer = {};
PD.BCL.Util.ArgumentTransformer.toObject = function (argumentAccessorName, argument) {

    var result = {};

    if (!PD.Type.isFunction(argument) && PD.Type.isObject(argument)) {

        result = argument;

        if (!result.hasOwnProperty(argumentAccessorName)) {
            result[argumentAccessorName] = null;
        }

    }
    else {
        result[argumentAccessorName] = argument;
    }

    return result;

}
