ProspectDirector.Modules.Views.ViewGridToolbar = (function (options) {

    var PRIVATE = {

        //---------------------------
        //--- CONSTANTS
        //---------------------------

        SELECTOR_SEARCH: "[data-role=quick-search]",

        SELECTOR_SUMMARY: "[data-role=filter-summary]",

        //---------------------------
        //--- CONSTRUCTOR
        //---------------------------

        init: function () {

            options = $.extend(true, {
                parent: null,
                settings: {
                    search: true,
                    summary: true
                },
                configuration: null
            }, options);

            PRIVATE.init_toolBar();
            PRIVATE.init_dataSource();

        },

        //---------------------------
        //--- CONSTRUCTOR
        //---------------------------

        get_parent: function () {
            return options.parent;
        },

        get_search: function () {
            return $(PRIVATE.extractSelector(options.settings.search, PRIVATE.SELECTOR_SEARCH));
        },

        get_summary: function () {
            return $(PRIVATE.extractSelector(options.settings.summary, PRIVATE.SELECTOR_SUMMARY));
        },

        //---------------------------
        //--- EVENT HANDLERS
        //---------------------------

        dataSource_onRequestEnd: function (e) {

            if (e.response === undefined) {
                return;
            }

            if (PRIVATE.get_summary().length > 0) {
                PRIVATE.get_summary().html((e.response.filterSummary ? e.response.filterSummary : "None applied"));
            }

        },

        search_onExecute: function (e) {
            PRIVATE.get_parent().refresh();
        },

        //---------------------------
        //--- INITIALIZERS
        //---------------------------

        init_dataSource: function () {

            var grid = PRIVATE.get_parent().get_grid().data("kendoGrid");
            
            if (grid) {
                grid.dataSource.bind("requestEnd", $.proxy(PRIVATE.dataSource_onRequestEnd, PRIVATE));
            }

        },

        init_toolBar: function () {

            var grid = PRIVATE.get_parent().get_grid().data("kendoGrid");

            if (!grid) {
                return;
            }

            grid.setOptions({
                toolbar: $.proxy(PRIVATE.getToolBarTemplate, PRIVATE)
            });

            if (PRIVATE.display()) {
                grid._toolbar();
            }

            if (PRIVATE.get_search().length === 1 && PRIVATE.get_search().data("searchBox") === undefined) {

                PRIVATE.get_search().searchBox({
                    width: 150,
                    watermark: "Find in Results...",
                    search: $.proxy(PRIVATE.search_onExecute, PRIVATE)
                });

            }

        },

        //---------------------------
        //--- METHODS
        //---------------------------

        extractSelector: function (value, defaultSelector) {

            var type = typeof value,
                result = null;

            switch (true) {
                case type === "boolean" && value === true:
                    result = PRIVATE.get_parent().get_grid().find(defaultSelector);
                    break;
                case type === "string":
                    result = value;
                    break;
                case value instanceof jQuery:
                    result = value.selector;
                    break;
            }

            return result;

        },

        getToolBarTemplate: function () {

            var html = null;

            if (PRIVATE.display()) {

                html = "<table style=\"height:24px;border-spacing:0px;border-collapse:collapse;table-layout:fixed;\"><tr>";

                if (PRIVATE.displaySummary()) {
                    html += "<td style=\"padding:0px 0px 0px 5px;font-size:11px;vertical-align:middle;width:40px;border-width:0px;\"><b>Filter:</b></td>";
                    html += "<td style=\"padding:0px;font-size:11px;vertical-align:middle;border-width:0px;\"><i data-role=\"filter-summary\"></i></td>";
                }

                if (PRIVATE.displaySearch()) {
                    html += "<td style=\"padding:0px;width:200px;border:0px;text-align:right;vertical-align:middle;line-height:0px;padding:2px 1px 2px 0px;\"><input type=\"text\" style=\"font-size:12px;width:150px;\" data-role=\"quick-search\" /><br/></td>";
                }

                html += "</tr></table>";

            }

            return html;

        },

        display: function () {
            return PRIVATE.displaySummary() || PRIVATE.displaySearch();
        },

        displaySummary: function () {
            return typeof options.settings.summary === "boolean" && options.settings.summary === true;
        },

        displaySearch: function () {
            return typeof options.settings.search === "boolean" && options.settings.search === true && options.configuration.search === true;
        },

        getSearchTerm: function () {
            var box = PRIVATE.get_search().data("searchBox");
            if (box === undefined || box === null) {
                return "";
            }
            else {
                return box.value();
            }
        }

    },

    PUBLIC = {
        getSearchTerm: PRIVATE.getSearchTerm
    };

    PRIVATE.init();

    return PUBLIC;

});