#include "..\script_component.hpp"

/*
 * Function: forge_client_db_fnc_loadGameState
 * Author: J. Schmidt
 * 
 * Description:
 * Loads game state from mission namespace and sets appropriate variables
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Success <BOOL>
 */

private _companyData = ["company", "CompanyState", nil] call FUNC(loadFromMission);
if (!isNil "_companyData") then {
    companyFunds = _companyData getOrDefault ["funds", 0];
    companyRating = _companyData getOrDefault ["rating", 0];
    companyGenerals = _companyData getOrDefault ["operations", []];
    companyGarageUnlocks = _companyData getOrDefault ["garage_unlocks", []];
};

private _playerUID = getPlayerUID player;
private _playerData = ["players", _playerUID, nil] call FUNC(loadFromMission);
if (!isNil "_playerData") then {
    private _armory_unlocks = _playerData getOrDefault ["armory_unlocks", [[],[],[],[]]];
    private _garage_unlocks = _playerData getOrDefault ["garage_unlocks", [[],[],[],[],[],[]]];
    private _locker = _playerData getOrDefault ["locker", []];
    private _garage = _playerData getOrDefault ["garage", []];
    private _cash = _playerData getOrDefault ["cash", 0];
    private _bank = _playerData getOrDefault ["bank", 0];
    private _number = _playerData getOrDefault ["number", "unknown"];
    private _email = _playerData getOrDefault ["email", "unknown@spearnet.mil"];
    private _paygrade = _playerData getOrDefault ["paygrade", "E1"];
    private _holster = _playerData getOrDefault ["holster", true];

    EGVAR(arsenal,armory_unlocks) = _armory_unlocks;
    EGVAR(arsenal,garage_unlocks) = _garage_unlocks;
    SETVAR(player,FORGE_Locker,_locker);
    SETVAR(player,FORGE_Garage,_garage);
    SETVAR(player,FORGE_Cash,_cash);
    SETVAR(player,FORGE_Bank,_bank);
    SETVAR(player,FORGE_Phone_Number,_number);
    SETVAR(player,FORGE_Email,_email);
    SETVAR(player,FORGE_Paygrade,_paygrade);
    SETVAR(player,FORGE_Holster_Weapon,_holster);

    if (isNull objectParent player) then {
        player setUnitLoadout (_playerData getOrDefault ["loadout", []]);
        if (_playerData getOrDefault ["currentWeapon", ""] != "") then {
            player selectWeapon (_playerData get "currentWeapon");
        };
        player setPosASL (_playerData getOrDefault ["position", getPosASL player]);
        player setDir (_playerData getOrDefault ["direction", 0]);
        
        if (_playerData getOrDefault ["stance", ""] != "") then {
            [player, _playerData get "stance"] call ace_common_fnc_setStance;
        };
    };
};

true