#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Registers a defend task where players must hold a zone marked by a marker
 *
 * Arguments:
 * 0: ID of the task <STRING>
 * 1: Defense zone marker name <STRING>
 * 2: Time to defend in seconds <NUMBER>
 * 3: Amount of funds the company receives if the task is successful <NUMBER> (default: 0)
 * 4: Amount of rating the company and player lose if the task is failed <NUMBER> (default: 0)
 * 5: Amount of rating the company and player receive if the task is successful <NUMBER> (default: 0)
 * 6: Should the mission end (MissionSuccess) if the task is successful <BOOL> (default: false)
 * 7: Should the mission end (MissionFailed) if the task is failed <BOOL> (default: false)
 * 8: Enemy wave count <NUMBER> (default: 3)
 * 9: Time between waves in seconds <NUMBER> (default: 300)
 * 10: Minimum BLUFOR units required in zone <NUMBER> (default: 1)
 *
 * Return Value:
 * None
 *
 * Example:
 * ["defend_zone_1", "defend_marker", 900, 500000, -100, 400, false, false, 3, 300, 1] spawn forge_client_task_fnc_defend;
 *
 * Public: Yes
 */

params [["_taskID", "", [""]], ["_defenseZone", "", [""]], ["_defendTime", 600, [0]], ["_companyFunds", 0, [0]], ["_ratingFail", 0, [0]], ["_ratingSuccess", 0, [0]], ["_endSuccess", false, [false]], ["_endFail", false, [false]], ["_waveCount", 3, [0]], ["_waveCooldown", 300, [0]], ["_minBlufor", 1, [0]]];

if (_defenseZone == "" || !(markerShape _defenseZone in ["RECTANGLE", "ELLIPSE"])) exitWith { diag_log format ["ERROR: Invalid defense zone marker: %1", _defenseZone]; };

private _result = 0;
private _startTime = time;
private _nextWaveTime = _startTime;
private _currentWave = 0;
private _zoneEmptyCounter = 0;
private _warningIssued = false;

waitUntil {
    sleep 1;
    private _bluforInZone = count (allUnits select {
        _x isKindOf "CAManBase" && 
        {side _x == west} && 
        {alive _x} 
    } inAreaArray _defenseZone);

    private _timeElapsed = time - _startTime;

    if (_bluforInZone < _minBlufor) then {
        _zoneEmptyCounter = _zoneEmptyCounter + 1;

        if (_zoneEmptyCounter == 15 && !_warningIssued) then {
            ["Warning", ["Defense Zone Empty!", "Return to the defense zone immediately!"]] remoteExec ["BIS_fnc_showNotification", 0];
            _warningIssued = true;
        };
    } else {
        _zoneEmptyCounter = 0;
        _warningIssued = false;
    };

    if (_currentWave < _waveCount && time >= _nextWaveTime) then {
        [_defenseZone, _taskID, _currentWave] call FUNC(spawnEnemyWave);
        
        _currentWave = _currentWave + 1;
        _nextWaveTime = time + _waveCooldown;

        ["IncomingQRF", ["Enemy forces approaching!", format ["Wave %1 of %2", _currentWave, _waveCount]]] remoteExec ["BIS_fnc_showNotification", 0];
    };

    if (_zoneEmptyCounter >= 30) then { _result = 1; };
    
    (_result == 1) or ((_bluforInZone >= _minBlufor) && (_timeElapsed >= _defendTime) && (_currentWave >= _waveCount));
};

if (_result == 1) then {
    [_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);

} else {
    [_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);
};