/// class PD.Manage.Modules.SystemSettingsService

PD.Manage.Modules.SystemSettingsService = Class.extend({

    //===================================
    // FIELDS
    //===================================
    
    _setting: null,
    _viewGrid: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (viewGrid, setting) {
        this._setting = setting;
        this._viewGrid = viewGrid;
    },

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

    edit: function () {

        var that = this,
            settingId = this._setting.id,
            config = this._getSettingEditorConfig(this._setting.UiSettings);

        if (config == null) {
            ProspectDirector.Web.UI.MessageDialog.warn({
                message: "Unable to edit system setting."
            });
            return null;
        }

        ProspectDirector.Web.UI.FeatureSets.NHibTargetEditorDialog.open({
            config: {
                minHeight: "100px",
                height: config.height,
                width: config.width,
                title: "Setting Editor"
            },
            data: {
                layoutType: config.layout,
                targetArbiter: config.arbiter,
                targetType: config.model,
                targetId: settingId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._viewGrid.refresh();
                }
            }
        });

    },

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

    _getSettingEditorConfig: function (value) {

        var config = this._parseSettingEditorString(value),
            result = null;

        if (config == null) {
            return null;
        }

        result = {
            arbiter: config.arbiter || "ProspectDirector.Web.Manage.Modules.Settings.SettingArbiter",
            model: config.model,
            height: config.height || "110px",
            width: config.width || "350px",
            layout: config.layout || "ProspectDirector.Web.UI.FeatureSets.Layouts.SimpleLayout"
        };

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

        return result;

    },

    _parseSettingEditorString: function (value) {

        var result = null;

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

    }

});