/// class ProspectDirector.Modules.EntityTypes.FeatureAclCompact
/// using ProspectDirector.Types.Core.Feature

ProspectDirector.Modules.EntityTypes.FeatureAclCompact = ProspectDirector.Types.Core.Feature.extend({

    init: function (options) {

        this._super(options);

        this.get_aclData().val(JSON.stringify(options.data.aceData));

        this.get_buttonEdit().on("click", $.proxy(this.buttonEdit_onClick, this));
        this.get_buttonEditCreatorPermissions().on("click", $.proxy(this.buttonEditCreatorPermissions_onClick, this));
        this.get_isCreatorAceAdded().on("click", $.proxy(this.isCreatorAceAdded_onClick, this));

        this.isCreatorAceAdded_onClick();

    },

    get_isCreatorAceAdded: function () {
        return this.get_container().find("[data-id=isCreatorAceAdded]");
    },

    get_buttonEdit: function () {
        return this.get_container().find("INPUT[data-command=edit]");
    },

    get_buttonEditCreatorPermissions: function () {
        return this.get_container().find("INPUT[data-command=edit-creator-permissions]");
    },

    get_creatorPermissionsAllowed: function () {
        return this.get_container().find("INPUT[data-id=creator-permissions-allowed]");
    },

    get_creatorPermissionsDenied: function () {
        return this.get_container().find("INPUT[data-id=creator-permissions-denied]");
    },

    get_aclData: function () {
        return this.get_container().find("[data-id=acl-data]");
    },

    get_permissionsList: function () {
        return JSON.parse(this.options.data.permissionsList);
    },

    buttonEdit_onClick: function () {

        ProspectDirector.Modules.EntityTypes.AclDialog.show({
            config: {
                content: {
                    type: "POST"
                }
            },
            params: {
                id: this.options.data.entityTypeId,
                aces: this.get_aclData().val()
            },
            callbacks: {
                ok: $.proxy(this.aclDialog_onOk, this)
            }
        });

    },

    buttonEditCreatorPermissions_onClick: function () {

        var that = this;

        PD.Shared.Dialogs.PermissionsEditorDialog.open({
            ace: {
                allow: this.get_creatorPermissionsAllowed().val(),
                deny: this.get_creatorPermissionsDenied().val()
            },
            permissions: this.get_permissionsList(),
            config: {
                title: "Creator Permissions"
            },
            callbacks: {
                ok: function (sender, e) {
                    that.get_creatorPermissionsAllowed().val(e.ace.allow);
                    that.get_creatorPermissionsDenied().val(e.ace.deny);
                }
            }
        });

    },

    isCreatorAceAdded_onClick: function () {

        this.creatorAceToggle(this.get_isCreatorAceAdded().is(":checked"));

    },

    creatorAceToggle: function (enabled) {

        this.get_container().find("[data-role=creatorAceControl]").each(function (idx, el) {
            $(el).parent().enabled(enabled);
            $(el).enabled(enabled);
        });

    },

    aclDialog_onOk: function (sender, e) {
        this.get_aclData().val(JSON.stringify(e));
        this.get_aclData().change();
    }

});