/// class PD.Manage.Modules.Extensions.SelectionListSelectorDialog
// using PD.Web.UI.Dialog

PD.Manage.Modules.Extensions.SelectionListSelectorDialog =
PD.Web.UI.Dialog.extend({
    
    //===================================
    // FIELDS
    //===================================
    
    _grid: null,
    _selection: null,

    //===================================
    // PROPERTIES
    //===================================

    get_okButton: function () {
        return this.get_node().find("BUTTON[data-id=button-ok]");
    },

    get_canelButton: function () {
        return this.get_node().find("BUTTON[data-id=button-cancel]");
    },

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

    getDefaultOptions: function () {

        return {
            config: {
                height: "400px",
                minHeight: "300px",
                width: "550px",
                minWidth: "400px",
                title: "Select Selection List"
            }
        };

    },

    template: function (options) {

        return [
            "<div style='positon:relative;'>",
                "<div data-id='grid' style='position:absolute;top:10px;left:10px;right:10px;bottom:47px;'></div>",
                "<div style='position:absolute;bottom:10px;left:10px;right:10px;text-align:right;'>",
                    "<button ",
                        "data-id='button-ok' ",
                        "class='pd-button' ",
                        "style='margin-right:5px;'>",
                        "OK",
                    "</button>",
                    "<button ",
                        "data-id='button-cancel' ",
                        "class='pd-button'>",
                        "Cancel",
                    "</button>",
                "</div>",
            "</div>"
        ];

    },

    onInit: function () {
        this._grid_init();
    },

    onActivate: function () {
        
        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));
        this.get_canelButton().on("click", $.proxy(this._cancelButton_onClick, this));

    },

    //===================================
    // PRIVATE EVENT HANDLERS
    //===================================
    
    _okButton_onClick: function () {
        this._return();
    },

    _cancelButton_onClick: function () {
        this.close();
    },
    
    _return: function () {

        var selection = this._selection;

        if (selection != null) {
            this.close({
                command: "ok",
                data: selection
            });
        }
        else {
            ProspectDirector.Web.UI.MessageDialog.warn({
                title: "No Selection",
                message: "No selection has been made. Please make a selection and try again."
            });
        }

    },

    //===================================
    // INITIALIZATION METHODS
    //===================================

    _grid_init: function () {

        var that = this;

        this._grid = new PD.Manage.Modules.SelectionListSelectorGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.get_options().data.viewStateId,
            change: function (e) {
                that._selection = e.selections.first();
            },
            dblClick: function (e) {
                that._selection = e.selections.first();
                that._return();
            }
        });

        this.get_children().add(this._grid);
        
    }
    
});

PD.Manage.Modules.Extensions.SelectionListSelectorDialog.open = function (options) {

    PD.ajax({
        url: PD.Path.resolve("~/Manage/SelectionList/Index"),
        success: function (e) {
            new PD.Manage.Modules.Extensions.SelectionListSelectorDialog(_.extend({
                data: {
                    viewStateId: e.viewStateId
                }
            }, options));
        }
    });
    
};