/// class PD.Web.UI.Controls.Toolbar
/// using PD.Web.UI.Controls.Control

PD.Web.UI.Controls.Toolbar =
PD.Web.UI.Controls.Control.extend({

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

    _items: null,
    _segments: null,
    _kendo: null,

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

    onInit: function (e) {
        this._items = new PD.BCL.List();
        this._segments = new PD.BCL.List();
    },

    _onFrameExit: function (e) {
        this.onRegisterSegments(e);
        this.onRegisterItems(e);
    },

    onRegisterSegments: function (e) {

        var that = this,
            segments = this._getOptionSegments();

        segments.each(function (index, segment) {
            if (segment.meetsLoadConditions(e)) {
                that._segments.add(segment);
            };
        });

    },

    onRegisterItems: function (e) {

        var that = this;

        this._segments.each(function (index, segment) {

            segment.onRegisterItems(new PD.Web.UI.Controls.ToolbarOnRegisterItemsEventArgs({
                toolbar: this,
                items: that._items,
                segment: segment
            }));

        });

    },

    template: function (e) {
        var cssClass = this._getOptionValue("cssClass", "-jadejs-toolbar -jadejs-primary-toolbar");
        return "<div class='-pd-toolbar " + cssClass + "'></div>";
    },

    onActivate: function (e) {
        
        var items = this._items.transform(function (item) {
                return item.getSettings();
            });

        this._kendo = this.get_node().kendoToolBar({
            items: items.toArray(),
            click: $.proxy(this.onToggleOrClick, this),
            toggle: $.proxy(this.onToggleOrClick, this)
        }).data("kendoToolBar");

        this.get_node().css({
            backgroundColor: this._getOptionValue("backgroundColor", null)
        });

        this._items.each(function (index, item) {
            if (item.onActivate != null) {
                item.onActivate();
            }
        });

    },

    _onActivateExit: function () {
        this.refreshContext();
    },
    
    onToggleOrClick: function (e) {

        var that = this,
            item = null,
            uid = $(e.target).data("id");

        e = $.extend({
            uid: uid,
            context: this._context
        }, e);

        this._iterateItems(this._items, function (i) {
            if (i.data("id") === uid) {
                item = i;
                return false;
            }
        });

        if (item) {

            this._segments.each(function (index, segment) {
                segment.click(that._kendo, item);
            });

        }

    },

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

    remove: function () {
        if (this.get_node() && this.get_node().data("kendoToolBar")) {
            this.get_node().data("kendoToolBar").destroy();
        }
        this.get_node().remove();
    },

    refreshContext: function () {

        var that = this;

        this._segments.each(function (index, segment) {
            segment.refreshContext(that._kendo);
        });

    },

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

    _getOptionValue: function (name, defaultValue) {

        var result = defaultValue;

        if (this.get_options()) {
            if (this.get_options()[name] != undefined) {
                result = this.get_options()[name];
            }
        }

        return result;

    },

    _getOptionSegments: function () {
        
        var result = null,
            options = this.get_options(); 
        
        if (options) {
        
            if (PD.Type.isArray(options)) {
                result = options;
            }
            else if (PD.Type.isArray(options.segments)) {
                result = options.segments;
            }

        } 
        
        return result ? new PD.BCL.List(result) : new PD.BCL.List();

    },

    _iterateItems: function (items, fn) {

        var that = this;

        items.each(function (index, item) {

            var abort = false;

            if (PD.Type.isFunction(fn)) {
                abort = fn(item) === false;
            }

            if (!abort
                && PD.Type.isFunction(item.get_buttons)
                && item.item.get_buttons().count() > 0) {
                that._iterateItems(item.item.get_buttons(), fn);
            }

            return !abort;

        });

    },

});