#include "..\script_component.hpp"

/*
 * Function: forge_client_org_fnc_addFunds
 * Author: J. Schmidt
 * 
 * Description:
 * Adds funds to an organization's account
 *
 * Arguments:
 * 0: _uid - Player UID <STRING>
 * 1: _name - Organization name <STRING>
 * 2: _amount - Amount to add (can be negative for withdrawals) <NUMBER>
 *
 * Return Value:
 * New funds amount <NUMBER>
 */

params [["_uid", "", [""]], ["_name", "", [""]], ["_amount", 0, [0]]];

if (_uid == "" || _name == "") exitWith { TRACE_2("Invalid parameters for adding funds",_uid,_name); nil };

private _store = call FUNC(verifyOrgStore);
private _key = format ["%1_%2", _uid, _name];
private _org = _store call ["getByKey", [_key]];

private _timestamp = systemTimeUTC apply { if (_x < 10) then { "0" + str _x } else { str _x }};
private _dateTime = format ["%1-%2-%3_%4:%5:%6.%7", _timestamp#0, _timestamp#1, _timestamp#2, _timestamp#3, _timestamp#4, _timestamp#5, _timestamp#6];

if (isNil "_org") exitWith { TRACE_1("Organization not found",_key); nil };

private _funds = _org get "funds";
private _logs = _org get "logs";
private _owner = _org get "owner";
private _newFunds = _funds + _amount;

_org set ["funds", _newFunds];

_logs pushBack [_dateTime, "FUNDS_ADDED", _amount, _newFunds];
_org set ["logs", _logs];

_store call ["post", [_owner, _name]];

_newFunds