params ["_heli", ["_action", "deploy"]];

if (isNull _heli) exitWith {};

private _canFloat = getNumber (configFile >> "CfgVehicles" >> typeOf _heli >> "canFloat") > 0;
if (!_canFloat) exitWith {};

private _actionLower = toLower _action;
private _legacyDeploy = (count _this) < 2;
private _state = _heli getVariable ["braf_efs_state", "SAFE"];

if (_actionLower == "deploy") then {
    _actionLower = "deployfloats";
};

private _getConfiguredLeakiness = {
    params ["_vehicle"];

    private _leakiness = getNumber (configFile >> "CfgVehicles" >> typeOf _vehicle >> "waterLeakiness");
    if (_leakiness < 0) then {
        _leakiness = 0;
    };

    _leakiness
};

private _getEfsCenterASL = {
    params ["_vehicle"];

    private _center = _vehicle selectionPosition ["EFS_center", "Memory"];
    if (_center isEqualTo [0, 0, 0]) then {
        _center = _vehicle selectionPosition ["EFS", "Memory"];
    };

    _vehicle modelToWorldWorld _center
};

private _getWaterSurfaceASL = {
    params ["_vehicle"];

    private _vehicleASL = getPosASL _vehicle;
    private _vehicleASLW = getPosASLW _vehicle;

    (_vehicleASL select 2) - (_vehicleASLW select 2)
};

private _installEngineLock = {
    params ["_vehicle"];

    if ((_vehicle getVariable ["braf_efs_engine_lock_eh", -1]) < 0) then {
        private _engineLockEh = _vehicle addEventHandler ["Engine", {
            params ["_vehicle", "_engineState"];

            if (_engineState && {_vehicle getVariable ["braf_efs_water_landed", false]}) then {
                _vehicle engineOn false;
            };
        }];

        _vehicle setVariable ["braf_efs_engine_lock_eh", _engineLockEh];
    };
};

private _startWaterMonitor = {
    params ["_vehicle"];

    if (_vehicle getVariable ["braf_efs_water_monitor", false]) exitWith {};

    _vehicle setVariable ["braf_efs_water_monitor", true, true];

    [_vehicle, _getEfsCenterASL, _getWaterSurfaceASL] spawn {
        params ["_heli", "_getEfsCenterASL", "_getWaterSurfaceASL"];

        while {
            !isNull _heli
            && {_heli getVariable ["braf_efs_deployed", false]}
            && {(_heli getVariable ["braf_efs_state", "SAFE"]) in ["DEPLOYED", "WATER_LANDED"]}
        } do {
            private _currentState = _heli getVariable ["braf_efs_state", "SAFE"];

            if (_currentState == "DEPLOYED" && {surfaceIsWater (getPosWorld _heli)}) then {
                private _probeASL = [_heli] call _getEfsCenterASL;
                private _waterSurfaceASL = [_heli] call _getWaterSurfaceASL;
                private _probeDepth = (_probeASL select 2) - _waterSurfaceASL;

                if (_probeDepth <= 0.25) then {
                    _heli setVariable ["braf_efs_state", "WATER_LANDED", true];
                    _heli setVariable ["braf_efs_water_landed", true, true];
                    _heli engineOn false;
                };
            };

            if (_heli getVariable ["braf_efs_water_landed", false] && {isEngineOn _heli}) then {
                _heli engineOn false;
            };

            sleep 0.25;
        };

        if (!isNull _heli) then {
            _heli setVariable ["braf_efs_water_monitor", false, true];
        };
    };
};

switch (_actionLower) do {
    case "init": {
        _heli setVariable ["braf_efs_state", "SAFE", true];
        _heli setVariable ["braf_efs_deployed", false, true];
        _heli setVariable ["braf_efs_water_landed", false, true];
        _heli setVariable ["braf_efs_water_monitor", false, true];
        _heli setVariable ["braf_efs_water_leakiness", [_heli] call _getConfiguredLeakiness];
        _heli animateSource ["EFS_Hide", 0, true];
        _heli setWaterFillPercentage 1;
    };

    case "arm": {
        if (_state != "SAFE") exitWith {};

        _heli setVariable ["braf_efs_state", "ARMED", true];
        _heli setVariable ["braf_efs_deployed", false, true];
        _heli setVariable ["braf_efs_water_landed", false, true];
    };

    case "disarm": {
        if (_state != "ARMED") exitWith {};

        _heli setVariable ["braf_efs_state", "SAFE", true];
        _heli setVariable ["braf_efs_deployed", false, true];
        _heli setVariable ["braf_efs_water_landed", false, true];
        _heli setWaterFillPercentage 1;
    };

    case "deployfloats": {
        if (!_legacyDeploy && {_state != "ARMED"}) exitWith {};

        if (_state in ["DEPLOYED", "WATER_LANDED"]) exitWith {};

        _heli setVariable ["braf_efs_state", "DEPLOYED", true];
        _heli setVariable ["braf_efs_deployed", true, true];
        _heli setVariable ["braf_efs_water_landed", false, true];
        _heli say3D "EFS";
        _heli animateSource ["EFS_Cover_Hide", 1, true];
        _heli animateSource ["EFS_Hide", 1, true];
        _heli setWaterLeakiness 0;
        _heli setWaterFillPercentage 0;

        [_heli] call _installEngineLock;
        [_heli] call _startWaterMonitor;
    };

    default {};
};
