#include "..\script_component.hpp"

/*
 * Function: forge_client_service_fnc_refuel
 * Author: Creedcoder, J. Schmidt
 *
 * Description:
 * Refuels the vehicle.
 *
 * Arguments:
 * 0: Vehicle <OBJECT> - The vehicle to refuel
 * 1: Vehicle Type <STRING> - The classname of vehicle to refuel
 *
 * Return Value:
 * None
 *
 * Example:
 * [vehicle, "B_APC_Tracked_01_CRV_F"] call forge_client_service_fnc_refuel;
 *
 * Public: No
 */

params ["_veh", "_vehType"];

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 _fnc_calculateFuelCost = {
    params ["_currentFuel", "_fuelPrice"];
    private _fuelNeeded = 1 - _currentFuel;
    private _cost = _fuelNeeded * _fuelPrice;
    [_fuelNeeded, _cost]
};

private _fnc_refuelVehicle = {
    params ["_vehicle", "_fuelNeeded", "_fuelPrice"];
    private _fuelAdded = 0;
    private _refuelRate = 0.01;
    private _refuelInterval = 0.1;

    while { fuel _vehicle < 0.99 && _fuelAdded < _fuelNeeded } do {
        _vehicle setFuel ((fuel _vehicle) + _refuelRate);
        _fuelAdded = _fuelAdded + _refuelRate;
        uiSleep _refuelInterval;
    };

    _fuelAdded
};

private _currentFuel = fuel _veh;
private _fuelPrice = "FUEL_COST" call BFUNC(getParamValue);
private _fuelDetails = [_currentFuel, _fuelPrice] call _fnc_calculateFuelCost;

_fuelDetails params ["_fuelNeeded", "_estimatedCost"];

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

_veh vehicleChat "Refueling...";

private _fuelAdded = [_veh, _fuelNeeded, _fuelPrice] call _fnc_refuelVehicle;
private _fuelCost = _fuelAdded * _fuelPrice;

_veh vehicleChat format ["%1 Refueled.", _vehType];

_fuelCost