#include "..\script_component.hpp"

/*
 * Function: forge_client_db_fnc_savePlayerState
 * Author: J. Schmidt
 * 
 * Description:
 * Collects and saves player state data to profile namespace for persistence
 *
 * Arguments:
 * 0: _player - Player unit (Optional, defaults to local player) <OBJECT>
 *
 * Return Value:
 * Success <BOOL>
 */

params [["_player", player, [objNull]]];

if (isNil EGVAR(arsenal,armory_unlocks)) then { EGVAR(arsenal,armory_unlocks) = [[],[],[],[]] };
if (isNil EGVAR(arsenal,garage_unlocks)) then { EGVAR(arsenal,garage_unlocks) = [[],[],[],[],[],[]] };

private _playerData = createHashMapFromArray [
    ["key", getPlayerUID _player],
    ["armory_unlocks", EGVAR(arsenal,armory_unlocks)],
    ["garage_unlocks", EGVAR(arsenal,garage_unlocks)],
    ["locker", GETVAR(_player,FORGE_Locker,[])],
    ["garage", GETVAR(_player,FORGE_Garage,[])],
    ["cash", GETVAR(_player,FORGE_Cash,0)],
    ["bank", GETVAR(_player,FORGE_Bank,0)],
    ["number", GETVAR(_player,FORGE_Phone_Number,"unknown")],
    ["email", GETVAR(_player,FORGE_Email,"unknown@spearnet.mil")],
    ["paygrade", GETVAR(_player,FORGE_Paygrade,"E1")],
    ["reputation", rating _player],
    ["loadout", getUnitLoadout _player],
    ["holster", GETVAR(_player,FORGE_Holster_Weapon,true)],
    ["position", getPosASLVisual _player],
    ["direction", getDirVisual _player],
    ["lastSaved", systemTimeUTC]
];

if (isNull objectParent _player) then {
    _playerData set ["currentWeapon", currentMuzzle _player];
    _playerData set ["stance", stance _player];
};

["players", _playerData, getPlayerUID _player] call FUNC(saveToProfile);

TRACE_1("Player state saved to profile",getPlayerUID _player);

true