#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Refuels the vehicle
 *
 * Arguments:
 * 0: Vehicle <OBJECT> - The vehicle to refuel
 *
 * Return Value:
 * None
 *
 * Example:
 * [vehicle] call forge_client_service_fnc_fuel;
 *
 * Public: No
 */

params ["_veh"];

private _store = missionNamespace getVariable ["FORGE_ORG_STORE_REG", createHashMap];
private _org = _store call ["getOrg", []];

if (_org isEqualTo nil) exitWith {
    ["You are not in an organization!", "warning", 3] call EFUNC(misc,notify);
};

private _orgFunds = _org get "funds";

private _fuelNeeded = 1 - (fuel _veh);
private _fuelCost = "FUEL_COST" call BFUNC(getParamValue);
private _estimatedCost = _fuelNeeded * _fuelCost;

private _fuelAdded = 0;
private _refuelRate = 0.01;
private _refuelInterval = 0.1;

if (_orgFunds < _estimatedCost) exitWith {
    ["Insufficient organization funds for refueling.", "warning", 3] call EFUNC(misc,notify);
};

["Refueling Vehicle...", "grey", 3] call EFUNC(misc,notify);

while { fuel _veh < 0.99 && _orgFunds >= (_fuelAdded * _fuelCost) } do {
    _veh setFuel ((fuel _veh) + _refuelRate);
    _fuelAdded = _fuelAdded + _refuelRate;
    uiSleep _refuelInterval;
};

private _totalCost = _fuelAdded * _fuelCost;
private _formattedTotalCost = (_totalCost) call EFUNC(misc,formatNumber);
private _formattedFuelAdded = (_fuelAdded) toFixed 2;

_store call ["updateFunds", -_totalCost];

[format ["VEHICLE REFUELED:
    <br/>Fuel Added: %1
    <br/>Total Cost: $%2
    <br/>Billed to SOF PMC Group.", _formattedFuelAdded, _formattedTotalCost], "blue-grey", 5] call EFUNC(misc,notify);