/// class PD.Web.UI.Controls.GridMessagingModule
/// using PD.Web.UI.Controls.GridModule

PD.Web.UI.Controls.GridMessagingModule =
PD.Web.UI.Controls.GridModule.extend({

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

    m_callback: null,

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

    start: function () {
        this.addGridMethod("addMessage", this.addMessage);
        this.addGridMethod("clearMessage", this.clearMessage);
    },

    addMessage: function (text, callback) {

        let $message = this._getMessage(text);
        this._addMessageCallback($message, callback);
        this.get_kendo().content.append($message);
        
        setTimeout(_.bind(this._setContentHeight, this), 250);
        
    },

    clearMessage: function () {
        this.get_kendo().content.find("[data-role=message-container]").remove();
    },

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

    _addMessageCallback: function ($message, callback) {

        let messageSelector = "div[data-role=message]";

        if (this.m_callback != null) {
            this.get_kendo().content.off("click", messageSelector, this.m_callback);
        }

        if ($.isFunction(callback)) {

            this.m_callback = $.proxy(callback, this);

            $message.find("[data-role=message]").css({
                textDecoration: "underline",
                color: "#0000FF",
                cursor: "pointer"
            });

            this.get_kendo().content.on("click", messageSelector, this.m_callback);

        }

    },

    _getMessage: function (text) {
        return $(`<div data-role='message-container' class="-pd-grid-message">
                    <div data-role='message'>${text}</div>
                </div>`);
    },

    _setContentHeight: function () {

        if (this.get_kendo() == null) {
            return;
        }

        var $grid = this.get_kendo().element;
        var gridHeight = $grid.outerHeight();
        var headerHeight = $grid.find(".k-grid-toolbar").outerHeight();
        var columnHeaderHeight = $grid.find(".k-grid-header").outerHeight();
        var footerHeight = $grid.find(".k-grid-pager").outerHeight();
        var $content = $grid.find(".k-grid-content");
        var calculatedHeight = gridHeight - (headerHeight + footerHeight + columnHeaderHeight);

        $content.css({
            height: calculatedHeight != null ? (calculatedHeight - 2) + "px" : null
        });

    }

});