#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Registers a delivery task
 *
 * Arguments:
 * 0: ID of the task <STRING>
 * 1: Amount of damaged cargo to fail the task <NUMBER>
 * 2: Amount of cargo delivered to complete the task <NUMBER>
 * 3: Marker name for the delivery zone <STRING>
 * 4: Amount of funds the company receives if the task is successful <NUMBER> (default: 0)
 * 5: Amount of rating the company and player lose if the task is failed <NUMBER> (default: 0)
 * 6: Amount of rating the company and player receive if the task is successful <NUMBER> (default: 0)
 * 7: Should the mission end (MissionSuccess) if the task is successful <BOOL> (default: false)
 * 8: Should the mission end (MissionFailed) if the task is failed <BOOL> (default: false)
 * 9: Amount of time to complete delivery <NUMBER> (default: nil)
 *
 * Return Value:
 * None
 *
 * Example:
 * ["delivery_1", 1, 3, "delivery_zone", 250000, -75, 300, false, false] spawn forge_client_task_fnc_delivery;
 * ["delivery_1", 1, 3, "delivery_zone", 250000, -75, 300, false, false, 900] spawn forge_client_task_fnc_delivery;
 *
 * Public: Yes
 */

params [["_taskID", "", [""]], ["_limitFail", -1, [0]], ["_limitSuccess", -1, [0]], ["_deliveryZone", "", [""]], ["_companyFunds", 0, [0]], ["_ratingFail", 0, [0]], ["_ratingSuccess", 0, [0]], ["_endSuccess", false, [false]], ["_endFail", false, [false]], ["_time", nil, [0]]];

private _result = 0;

waitUntil {
    sleep 1;
    _cargo = GVAR(allCargo) select { (_x getVariable ["assignedTask", ""]) == _taskID };
    count _cargo > 0
};

private _cargo = GVAR(allCargo) select { (_x getVariable ["assignedTask", ""]) == _taskID };
private _startTime = if (!isNil "_time") then { floor(time) } else { nil };

waitUntil {
    sleep 1;

    private _cargoDelivered = ({ _x inArea _deliveryZone && (damage _x) < 0.7 } count _cargo);
    private _cargoDamaged = ({ damage _x >= 0.7 } count _cargo);

    if (!isNil "_time") then {
        private _timeExpired = (floor time - _startTime >= _time);

        if (_cargoDamaged >= _limitFail) then { _result = 1; };
        if (_cargoDelivered < _limitSuccess && _timeExpired) then { _result = 1; };

        (_result == 1) or ((_cargoDelivered >= _limitSuccess) && (_cargoDamaged < _limitFail))
    } else {
        if (_cargoDamaged >= _limitFail) then { _result = 1; };

        (_result == 1) or ((_cargoDelivered >= _limitSuccess) && (_cargoDamaged < _limitFail))
    };
};

if (_result == 1) then {
    { deleteVehicle _x } forEach _cargo;

    [_taskID, "FAILED"] call BFUNC(taskSetState);

    if (_endFail) then {
        ["MissionFail", false] remoteExecCall ["BIS_fnc_endMission", playerSide];
    };

    // ["deduct", _ratingFail] remoteExecCall ["forge_server_rating_fnc_handleRating", 2];
    [_ratingFail] call EFUNC(org,addReputation);
    [format ["Task failed: %1 reputation", _ratingFail], "warning", 5, "right"] call EFUNC(misc,notify);

    sleep 1;

    { [_x, _ratingFail] remoteExec ["addRating", -2] } forEach allPlayers;
} else {
    { deleteVehicle _x } forEach _cargo;

    [_taskID, "SUCCEEDED"] call BFUNC(taskSetState);

    if (_endSuccess) then {
        ["MissionSuccess", false] remoteExecCall ["BIS_fnc_endMission", playerSide];
    };

    // ["advance", _ratingSuccess] remoteExecCall ["forge_server_rating_fnc_handleRating", 2];
    [_ratingSuccess] call EFUNC(org,addReputation);
    [format ["Task succeeded: %1 reputation", _ratingSuccess], "success", 5, "right"] call EFUNC(misc,notify);

    sleep 1;

    { [_x, _ratingSuccess] remoteExec ["addRating", -2] } forEach allPlayers;

    // ["advance", _companyFunds] remoteExecCall ["forge_server_money_fnc_handleFunds", 2];
    [_companyFunds] call EFUNC(org,addFunds);
    [format ["Task succeeded: %1 funds", _companyFunds], "success", 5, "right"] call EFUNC(misc,notify);
};