#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Handles servicing of vehicles including rearmament, refueling, and repairs.
 * Calculates and deducts service costs from the player's organization funds.
 *
 * Arguments:
 * 0: Vehicle <OBJECT> - The vehicle to service
 * 1: Vehicle Kind <STRING> - The type of vehicle to check ("LAND", "AIR", etc.)
 *
 * Return Value:
 * None
 *
 * Example:
 * [vehicle, "LAND"] call forge_client_service_fnc_vehicle;
 *
 * Notes:
 * - Vehicle must be stationary (speed < 1)
 * - Player must be the driver of the vehicle
 * - Costs are automatically calculated and deducted
 *
 * Trigger Example:
 * _trg = createTrigger ["EmptyDetector", getPos player];
 * _trg setTriggerArea [5, 5, 0, false];
 * _trg setTriggerActivation ["ANYPLAYER", "PRESENT", true];
 * _trg setTriggerStatements [
 *     "call { {_x iskindof 'LAND' && speed _x < 1} count thisList > 0; };",
 *     "call { _handle = [(thisList select 0), 'LAND'] spawn forge_client_service_fnc_vehicle; };",
 *     ""
 * ];
 *
 * Public: No
 */

params ["_veh", "_vehKind"];

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 _veh = _this select 0;
private _vehType = getText (configFile >> "CfgVehicles" >> typeOf _veh >> "DisplayName");

private _rearmCost = 0;
private _refuelCost = 0;
private _repairCost = 0;
private _totalCost = 0;

if ((_veh isKindOf _vehKind) && (driver _veh == player)) exitWith {

	_veh vehicleChat format ["Servicing %1... Please Stand By...", _vehType];

	uiSleep 3;

	_rearmCost = [_veh, _vehType] call FUNC(rearm);
	_refuelCost = [_veh, _vehType] call FUNC(refuel);
	_repairCost = [_veh, _vehType] call FUNC(repair);
	_totalCost = (_rearmCost + _repairCost + _refuelCost);

	private _formattedRearmCost = (_rearmCost) call EFUNC(misc,formatNumber);
	private _formattedRefuelCost = (_refuelCost) call EFUNC(misc,formatNumber);
	private _formattedRepairCost = (_RepairCost) call EFUNC(misc,formatNumber);
	private _formattedTotalCost = (_totalCost) call EFUNC(misc,formatNumber);

	_store call ["updateFunds", -_totalCost];

	[format ["SERVICE COST:
		<br/>Rearmament: $%1
		<br/>Repairs: $%2
		<br/>Refueling: $%3
		<br/>Total: $%4
		<br/>Billed to SOF PMC Group.", 
		_formattedRearmCost, _formattedRepairCost, _formattedRefuelCost, _formattedTotalCost], "blue-grey", 8] call EFUNC(misc,notify);

	_veh vehicleChat format ["Service Completed for %1", _vehType];
};