/// class PD.Manage.Modules.SystemSettingEditCommand

PD.Manage.Modules.SystemSettingEditCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function (options) {

        this.props("id", "system-setting/edit");
        this.props("enabled", true);
        this.props("label", "Edit...");
        this.props("icon", PD.Path.to16x16("pencil.png"));
        this.props("tooltip", "Edit the selected System Setting.");

        this.state("systemSettingId", null);
        this.state("systemSettingUi", null);

    },

    execute: function (e) {
        this._super(e);

        var data = this._transformSetting();

        if (data != null) {
            this._showEditorDialog(data);
        }
        else {
            this._showErrorDialog();
        }

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _showEditorDialog: function (data) {

        PDJS.Shared.Features.FeatureDialogForNhib.open({
            title: "Setting Editor",
            args: {
                targetId: data.id,
                targetType: data.model,
                arbiterType: data.arbiter,
                layoutType: data.layout || FeatureBlendJS.Views.FeatureBlendLayoutType.Simple
            },
            callbacks: {
                ok: (sender, e) => {
                    sender.destroyContext();
                    PD.context
                        .get_default()
                        .get_events()
                        .publish("system-setting/edit");
                }
            }
        }, {
            minHeight: "100px",
            height: parseInt(data.height),
            width: parseInt(data.width),
        });

    },

    _showErrorDialog: function () {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Error,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OK,
            title: "Error",
            message: "Unable to edit system setting."
        });

    },

    _transformSetting: function () {

        let result = {};
        let id = this.state("systemSettingId");
        let ui = this.state("systemSettingUi");

        try {
            ui = JSON.parse(ui);
        }
        catch (ex) {
            ui = {};
        }

        return {
            id: id,
            arbiter: ui.arbiter || "ProspectDirector.Web.Manage.Modules.Settings.SettingArbiter",
            model: ui.model,
            height: ui.height || "110px",
            width: ui.width || "350px",
            layout: ui.layout || FeatureBlendJS.Views.FeatureBlendLayoutType.Simple
        };

    }

});