#include "..\script_component.hpp"

if ( !hasInterface ) exitWith {};

["ace_killed", {
	params ["_unit", "_causeOfDeath", "_killer", "_instigator"];

	if (not (local _unit)) exitWith {};

	private _causeOfDeath = _unit getVariable ["ace_medical_causeOfDeath", "#scripted"];

	if (_causeOfDeath != "respawn_button") exitWith {};
	private _timeWentUnconscious = _unit getVariable [QGVARMAIN(lastTimeKnockedOut), -1];
	private _durationSpentUnconscious = -1;
	if (_timeWentUnconscious isEqualTo -1) exitWith {};
	
	_durationSpentUnconscious = diag_tickTime - _timeWentUnconscious;

	[
		LEVEL_INFO,
		QUOTE(COMPONENT),
		"RESPAWNED WHILE UNCONSCIOUS",
		[_unit, [
			["durationSpentUnconscious", _durationSpentUnconscious]
		]] call EFUNC(common,addPlayerInfoToArray)
	] remoteExec [QEFUNC(common,log), 2];
	//	format["%1 was unconscious then clicked the respawn button", name _unit] remoteExec["systemChat", 0];
}] call CBA_fnc_addEventHandler;


["ace_medical_knockOut", { // local event for module & epi event
	// systemChat format["ace_medical_knockOut: %1", _this];
	private _unit = _this;
	if (not (local _unit)) exitWith {};
	_this setVariable [QGVARMAIN(lastTimeKnockedOut), diag_tickTime];
}] call CBA_fnc_addEventHandler;

["ace_medical_WakeUp", { // local event for module & epi event
	// systemChat format["ace_medical_WakeUp: %1", _this];
	private _unit = _this;
	if (not (local _unit)) exitWith {};
	_this setVariable [QGVARMAIN(lastTimeKnockedOut), nil];
}] call CBA_fnc_addEventHandler;

["ace_unconscious", { // used when applying damage
	params ["_unit", "_isUnconscious"];
	if (not (local _unit)) exitWith {};

	if (_isUnconscious && isNil {_unit getVariable QGVARMAIN(lastTimeKnockedOut)}) then {
		// systemChat format["%1 is unconscious", _unit];
		_unit setVariable [QGVARMAIN(lastTimeKnockedOut), diag_tickTime];
	} else {
		// systemChat format["%1 is conscious", _unit];
		_unit setVariable [QGVARMAIN(lastTimeKnockedOut), nil];
	};
}] call CBA_fnc_addEventHandler;