#include "script_component.hpp"

/*
 * Function: xpdb_db_fnc_saveToSlot
 * Author: NikolaiF90, J.Schmidt
 * Edit: 07.27.2024
 * Copyright © 2024 NikolaiF90, J.Schmidt, All rights reserved
 *
 * Do not edit without permission!
 *
 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivative 4.0 International License.
 * To view a copy of this license, vist https://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to Creative Commons,
 * PO Box 1866, Mountain View, CA 94042
 *
 * [Description]
 * Save or update data for specified slot.
 *
 * Arguments:
 * N/A
 *
 * Return Value:
 * N/A
 *
 * Examples:
 * [] call xpdb_db_fnc_saveToSlot
 *
 * Public: Yes
 */

EGVAR(db,selectedList) = [EGVAR(db,listBox)] call DFUNC(getSelectedList);

[EGVAR(db,debug), "xpdb_db_fnc_saveToSlot", "Generating save...", false] call EFUNC(utils,debug);

private _saveDate = [] call EFUNC(generate,dateData);
private _saveName = format ["'%1' saved on, '%2'", Scenario_Name, _saveDate];
private _saveSlot = 0;

if (EGVAR(db,selectedList) == 0) then {
    [EGVAR(db,debug), "xpdb_db_fnc_saveToSlot", "Creating a new save slot...", false] call EFUNC(utils,debug);
    EGVAR(db,slots) pushBack _saveName;
    _saveSlot = (count EGVAR(db,slots)) -1;
} else {
    [EGVAR(db,debug), "xpdb_db_fnc_saveToSlot", format ["Saving to slot '%1'", EGVAR(db,selectedList)], false] call EFUNC(utils,debug);
    EGVAR(db,slots) set [EGVAR(db,selectedList), _saveName];
    _saveSlot = EGVAR(db,selectedList);
};

[EGVAR(db,debug), "xpdb_db_fnc_saveToSlot", "Now saving...", false] call EFUNC(utils,debug);
[_saveSlot] call EFUNC(save,game);

profileNamespace setVariable [QEGVAR(db,pListKey), EGVAR(db,slots)];

[EGVAR(db,debug), "xpdb_db_fnc_saveToSlot", "Progress saved.", false] call EFUNC(utils,debug);
[EGVAR(db,listBox)] call DFUNC(updatePersistentList);