/// class ProspectDirector.Modules.Reports.ReportSelectorDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Reports.ReportSelectorDialog = (function (options) {

    var

        PRIVATE = {

            init: function () {

                options = $.extend({
                    selector: null,
                    data: null
                }, options);

                PRIVATE.init_dialog();
                PRIVATE.init_grid();

            },

            //---------------------------
            //--- PROPERTIES
            //---------------------------

            get_dialog: function () {
                return $(options.selector);
            },

            get_okButton: function () {
                return PRIVATE.get_dialog().find("INPUT[type=button][data-command=ok]");
            },

            get_cancelButton: function () {
                return PRIVATE.get_dialog().find("INPUT[type=button][data-command=cancel]");
            },

            get_grid: function () {
                return PRIVATE.get_dialog().find("[data-role=grid]");
            },

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

            cancelButton_onClick: function () {
                this.get_dialog().data("dialog").hide();
            },

            okButton_onClick: function () {

                var selection = PRIVATE.getSelectedReport();

                if (selection === null) {
                    alert("No report has been selected.");
                    return;
                }

                ProspectDirector.Types.Core.Notifications.action("Generating report... please wait.");

                PRIVATE.get_okButton().disable();

                PRIVATE.requestReport(selection.id, options.data.entityId, selection.isHtml);

                PRIVATE.get_dialog().data("dialog").hide();

            },

            grid_onDblClick: function (e) {

                var row = $(e.currentTarget),
                    grid = PRIVATE.get_grid().data("kendoGrid"),
                    item = grid.dataSource.getByUid(row.data().uid);

                PRIVATE.requestReport(item.id, options.data.entityId, item.isHtml);

                PRIVATE.get_dialog().data("dialog").hide();


            },

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

            init_dialog: function () {

                PRIVATE.get_cancelButton().on("click", $.proxy(PRIVATE.cancelButton_onClick, PRIVATE));
                PRIVATE.get_okButton().on("click", $.proxy(PRIVATE.okButton_onClick, PRIVATE));

            },

            init_grid: function () {

                var that = this;

                PRIVATE.get_grid().kendoGrid({
                    dataSource: options.data.reportTypes,
                    selectable: "multiple row",
                    columns: [
                        { field: "id", title: "Available Reports:", template: PRIVATE.rowTemplate }
                    ],
                    dataBound: function (e) {

                        var grid = that.get_grid().data("kendoGrid");

                        if (grid.dataSource.data().length === 0) {
                            ProspectDirector.Types.KendoExtensions.Grid.addMessage(that.get_grid(), "There are no published reports.");
                        }
                        else {
                            ProspectDirector.Types.KendoExtensions.Grid.removeMessage(that.get_grid());
                        }

                    }
                });

                PRIVATE.get_grid().on("dblclick", "TR[data-uid]", $.proxy(PRIVATE.grid_onDblClick, PRIVATE));

                PRIVATE.get_grid().disableTextSelection();

            },

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

            rowTemplate: function (e) {

                return "<table>" +
                        "<tr>" +
                        "<td style=\"vertical-align:top;width:40px;\"><img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/32x32/" + e.icon) + "\" /></td>" +
                        "<td style=\"vertical-align:top;\"><b>" + e.name + "</b><br/>" + (e.description ? e.description : "<i style=\"color:#AAAAAA;\">No Description Provided</i>") + "</td>" +
                        "</tr>" +
                        "</table>";

            },

            requestReport: function (reportId, entityId, isHtml) {

                if (isHtml) {
                    window.open(ProspectDirector.RUNTIME.resolveUrl("~/Shared/Report/Export?id=" + reportId + "&entityId=" + entityId));
                }
                else {

                    ProspectDirector.RUNTIME.iframeRequest("~/Shared/Report/Export", {
                        id: reportId,
                        entityId: entityId
                    });

                }

            },

            getSelectedReportId: function () {

                var result = null,
                    grid = PRIVATE.get_grid().data("kendoGrid"),
                    item = grid.select();

                if (item.length > 0) {
                    result = grid.dataSource.getByUid(item.data("uid")).id;
                }

                return result;

            },

            getSelectedReport: function () {

                var result = null,
                    grid = PRIVATE.get_grid().data("kendoGrid"),
                    item = grid.select();

                if (item.length > 0) {
                    result = grid.dataSource.getByUid(item.data("uid"));
                }

                return result;

            }

        },

        PUBLIC = {

        };

    PRIVATE.init()

    return PUBLIC

});


ProspectDirector.Modules.Reports.ReportSelectorDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("ReportSelectorDialog",
                "Select Report",
                ProspectDirector.RUNTIME.resolveUrl("~/report/ReportSelectorDialog"));

    }

}));