
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Settings grid functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


function ProcessSettings(SettingsText, SettingsValue, SettingsType) {

    //console.log("ProcessSettings: " + SettingsText + ", " + SettingsValue + ", " + SettingsType);

    // Get the current SettingsJsonString from the selected pane
    // Merge the current settings with the new value
    // Then refresh the pane with the final settings results
    var CurrentSettingsJsonString = GetSelectedContentPaneSettingsJsonString();
    var StContentType = GetSelectedContentType();

    if (!bSmallSizeApp) {
        var SelectedContentPane = GetSelectedContentPane();
        if (!SelectedContentPane) {
            ToastError("Settings", "Open a new page to change its settings");
            return;
        }
    }

    // Apply the settings to the selected content pane
    AjaxApplySettings(StContentType, CurrentSettingsJsonString, SettingsText, SettingsValue, SettingsType);
}



/**
 * Update the selected content with this new settings value
 * Call Ajax to merge the new settgings value with the current value
 * Ajax will return the new settings value
 * The new settings string can then be saved to a cookie for the selected content
 * @param {any} StContentType
 * @param {any} CurrentSettingsJsonString
 * @param {any} SettingsText
 * @param {any} SettingsValue
 * @param {any} SettingsType
 */
function AjaxApplySettings(StContentType, CurrentSettingsJsonString, SettingsText, SettingsValue, SettingsType) {

    if (!StContentType)
        return;

    var params = {
        "ContentType": StContentType,
        "CurrentSettingsJsonString": CurrentSettingsJsonString,
        "SettingsText": SettingsText,
        "SettingsValue": SettingsValue,
        "SettingsType": SettingsType
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxGetSettingsJsonString",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {
                if (response.HasSettings) {
                    //console.log("GetSettingsJsonString: " + response.SettingsJsonString);

                    // Set the new settings into the selected content
                    SetSelectedContentPaneSettingsJsonString(response.SettingsJsonString);

                    // Refresh the selected content to show the new settings
                    RefreshSelectedContent();
                }
                else {
                    // "This content has no settings. Select an icon from Home (All Views)."
                    $.toaster({ settings: { timeout: 10000 } });
                    $.toaster(response.SettingsMessage, 'Settings', 'danger');
                    $.toaster.reset();
                }
            }
            else {
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText;
            ToastError("AjaxApplySettings", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}

