#include "..\script_component.hpp"

/*
 * Function: forge_client_db_fnc_saveToMission
 * Author: J.Schmidt
 * Edit: 07.23.2024
 * Copyright © 2024 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]
 * Store mission and player data in Mission Namespace.
 *
 * Arguments:
 * N/A
 *
 * Return Value:
 * N/A
 *
 * Examples:
 * [] call forge_client_db_fnc_saveToMission;
 *
 * Public: Yes
 */

if (isNil "companyFunds") then { companyFunds = 0 };
if (isNil "companyRating") then { companyRating = 0 };
if (isNil "companyGenerals") then { companyGenerals = [] };
if (isNil "companyGarageUnlocks") then { companyGarageUnlocks = [] };
if (isNil EGVAR(arsenal,armory_unlocks)) then { EGVAR(arsenal,armory_unlocks) = [[],[],[],[]] };
if (isNil EGVAR(arsenal,garage_unlocks)) then { EGVAR(arsenal,garage_unlocks) = [[],[],[],[],[],[]] };

private _vdata = [];
private _pdata = [];
private _cdata = [
["key", "CompanyState"],
["funds", [companyFunds]],
["rating", [companyRating]],
["operations", [companyGenerals]],
["garage_unlocks", [companyGarageUnlocks]]
];

private _vehicles = nearestObjects [player, ["LandVehicle"], 50] apply {
    createHashMapFromArray [
        ["vehicle", _x],
        ["class", [typeOf _x]],
        ["position", [getPosATL _x]],
        ["direction", [getDir _x]],
        ["health", [damage _x]]
    ];
};

{
    if (alive _x) then {  
        _vdata pushBackUnique _x;
    };
} forEach _vehicles;

{
    private _data = [
    ["key", getPlayerUID player],
    // ["armory_unlocks", [forge_client_armory_arsenalUnlocks]],
    // ["garage_unlocks", [forge_client_armory_garageUnlocks]],
    ["armory_unlocks", [EGVAR(arsenal,armory_unlocks)]],
    ["garage_unlocks", [EGVAR(arsenal,garage_unlocks)]],
    // ["locker", [player getVariable ["Locker", []]]],
    // ["garage", [player getVariable ["Garage", []]]],
    ["locker", [GETVAR(player,FORGE_Locker,[])]],
    ["garage", [GETVAR(player,FORGE_Garage,[])]],
    // ["cash", [player getVariable ["FORGE_Cash", 0]]],
    // ["bank", [player getVariable ["FORGE_Bank", 0]]],
    ["cash", [GETVAR(player,FORGE_Cash,0)]],
    ["bank", [GETVAR(player,FORGE_Bank,0)]],
    // ["number", [player getVariable ["FORGE_Phone_Number", "unknown"]]],
    // ["email", [player getVariable ["FORGE_Email", "unknown@spearnet.mil"]]],
    ["number", [GETVAR(player,FORGE_Phone_Number,"unknown")]],
    ["email", [GETVAR(player,FORGE_Email,"unknown@spearnet.mil")]],
    // ["paygrade", [player getVariable ["Paygrade", "E1"]]],
    ["paygrade", [GETVAR(player,FORGE_Paygrade,"E1")]],
    ["reputation", [rating player]],
    ["loadout", [getUnitLoadout player]],
    // ["holster", [player getVariable ["FORGE_Holster_Weapon", true]]],
    ["holster", [GETVAR(player,FORGE_Holster_Weapon,true)]],
    ["position", [getPosASLVisual player]],
    ["direction", [getDirVisual player]]
    ];

    if (isNull objectParent player) then {
        _data pushBack "currentWeapon";
        _data pushBack [currentMuzzle player];
        _data pushBack "stance";
        _data pushBack [stance player];
    };

    _pdata pushBackUnique _data;
} forEach playableUnits;

private _cHashMap = createHashMapFromArray _cdata;
private _pHashMap = createHashMapFromArray _pdata;
private _vHashMap = createHashMapFromArray _vdata;

// missionProfileNamespace setVariable ["FORGE_MissionData", _cHashMap];
// missionProfileNamespace setVariable ["FORGE_PlayerData", _pHashMap];
// missionProfileNamespace setVariable ["FORGE_VehicleData", _vHashMap];
SETPVAR(missionNamespace,FORGE_MissionData,_cHashMap);
SETPVAR(missionNamespace,FORGE_PlayerData,_pHashMap);
SETPVAR(missionNamespace,FORGE_VehicleData,_vHashMap);