/// class PD.Shared.Dialogs.PermissionsEditorDialog
/// using PD.Web.UI.Dialog

PD.Shared.Dialogs.PermissionsEditorDialog = PD.Web.UI.Dialog.extend({

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

    _grid: null,
    _viewStateId: 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 {
            permissions: [],
            ace: { allow: 0, deny: 0 },
            config: {
                height: "300px",
                minHeight: "300px",
                width: "550px",
                minWidth: "400px",
                title: "Permissions Editor"
            }
        };

    },

    template: function (options) {

        return [
            "<div>",
                "<div data-id='controls' style='position:absolute;top:10px;bottom:44px;left:10px;right:10px;'>",
                "</div>",
                "<div style='position:absolute;bottom:10px;left:10px;right:10px;text-align:right;'>",
                    "<button ",
                        "data-id='button-ok' ",
                        "class='-pdjs-dialog-button' ",
                        "style='margin-right:5px;'>",
                        "OK",
                    "</button>",
                    "<button ",
                        "data-id='button-cancel' ",
                        "class='-pdjs-dialog-button'>",
                        "Cancel",
                    "</button>",
                "</div>",
            "</div>"
        ];

    },

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

    onActivate: function (options) {

        var that = this;
        
        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));

        this.get_canelButton().on("click", $.proxy(this._cancelButton_onClick, this));
        
    },

    onComplete: function () {

        this._permissionsEditor.load({
            permissions: this.get_options()["permissions"],
            ace: this.get_options()["ace"]
        });

    },

    //===================================
    // PRIVATE EVENT HANDLERS
    //===================================

    _init_permissionsEditor: function () {

        var that = this;

        this._permissionsEditor = new PD.Web.UI.Controls.Security.PermissionsEditor({
            insertAt: "DIV[data-id=controls]"
        });
        
        this.get_children().add(this._permissionsEditor);
        
    },

    _okButton_onClick: function () {
        var result = this._permissionsEditor.getAcePermissions();
        this.close({
            command: "ok",
            data: {
                ace: result
            }
        });
    },

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

PD.Shared.Dialogs.PermissionsEditorDialog.open = function (options) {
    return new PD.Shared.Dialogs.PermissionsEditorDialog(options);
};