/// 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) {

        ProspectDirector.Web.UI.FeatureSets.NHibTargetEditorDialog.open({
            config: {
                minHeight: "100px",
                height: data.height,
                width: data.width,
                title: "Setting Editor"
            },
            data: {
                layoutType: data.layout,
                targetArbiter: data.arbiter,
                targetType: data.model,
                targetId: data.id
            },
            callbacks: {
                ok: function (sender, e) {
                    PD.context.get_default().get_events().publish("system-setting/edit");
                }
            }
        });

    },

    _showErrorDialog: function () {

        ProspectDirector.Web.UI.MessageDialog.warn({
            message: "Unable to edit system setting."
        });

    },

    _transformSetting: function () {

        var result = null,
            id = this.state("systemSettingId"),
            ui = this.state("systemSettingUi");

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

        result = {
            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 || "ProspectDirector.Web.UI.FeatureSets.Layouts.SimpleLayout"
        };

        if (result.layout != null) {
            result.layout = PD.Type.getType(result.layout);
        }

        return result;

    }

});