/// class PD.Web.UI.Controls.FilterSummaryGridWidget
/// using PD.Web.UI.Controls.GridWidget

PD.Web.UI.Controls.FilterSummaryGridWidget =
PD.Web.UI.Controls.GridWidget.extend({

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

    _summary: null,

    //===================================
    // EVENT HANDLERS
    //===================================
    
    onActivate: function () {

        var dataSource = this.get_grid().get_dataSource().get_instance();

        dataSource.bind("requestEnd", $.proxy(this._onRequestEnd, this));

    },
    
    _onRequestEnd: function (e) {

        var filterCount = 0;
        var $label = this.get_node().find(".-pd-filter-notification");

        $label.empty("");

        if (e.response == null || e.response.filtersSummary == null) {
            return;
        }

        this._summary = e.response.filtersSummary;

        filterCount = this._getFilterCount();

        if (filterCount > 0) {
            $label.append(filterCount + " filter(s)");
            $label.addClass("-pd-filter-notification-active");
        }
        else {
            $label.append("No filters");
            $label.removeClass("-pd-filter-notification-active");
        }

        this._initTooltip();

    },

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

    template: function () {
        return [
            "<div class='-pd-widget' style='width:100px;padding:4px;'>",
                "<div class='-pd-filter-notification'>No Filters</div>",
            "</div>"
        ];
    },

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

    _initTooltip: function () {
    
        var that = this,
            $filter = null;

        if (this._getFilterCount() === 0) {
            return;
        }

        $filter = this.get_node().find(".-pd-filter-notification");

        Tipped.create($filter[0], "<div style='height:200px;width:300px;'></div>", {
            skin: "white",
            onShow: function (content, element) {
                var $content = $(content).children("DIV:first");
                $content.empty();
                $content.append(that._getFilterSummaryText());
            }
        });
    
    },

    _getFilterCount: function () {

        var summary = this._summary;
        var result = 0;

        if (summary != null) {
           result = summary.inline.count + summary.design.count;
        }

        return result;

    },

    _getFilterSummaryText: function () {

        var summary = this._summary,
            result = "";

        if (summary.inline.count > 0) {
            result += "<div style='margin-bottom:5px;font-weight:bold;'>Column Filters</div>";
            result += "<div style='margin-bottom:5px;padding-left:10px;'>" + summary.inline.text + "</div>";
        }

        if (summary.design.count > 0) {
            result += "<div style='margin-bottom:5px;font-weight:bold;'>Design Filters</div>";
            result += "<div style='margin-bottom:5px;padding-left:10px;'>" + summary.design.text + "</div>";
        }

        result = "<div style='border-bottom: 1px solid black;font-weight:bold;margin-bottom:5px;font-size:14px;padding-bottom:3px;'>Filter Summary</div><div style='font-size:11px;'>" + result + "</div>";
        
        return result;

    }

});