local function getClosestVehicle(player)
    local vehicle = player:getVehicle();
    if vehicle == nil then
        vehicle = ISVehicleMenu.getVehicleToInteractWith(player);
    end
    return vehicle;
end

local PartList = {"GunTurret", "AmmoCan", "SignalSystem", "Senser", "Silencer", "GunTurretLaser"}
local function UnInstallTurret(player, vehicle)
    local modData = vehicle:getModData()
    if not modData.User then
        player:Say(getText("IGUI_WQ_NoAccess"))
        return
    end
    if not modData.User[player:getUsername()] then
        player:Say(getText("IGUI_WQ_NoAccess"))
        return
    end
    for i = 1, #PartList do
        local part = vehicle:getPartById(PartList[i])
        if part and part:getInventoryItem() then
            player:Say(getText("IGUI_ShouldUninstall"))
            return
        end
    end
    ISTimedActionQueue.add(ISFUninstallTurretAction:new(player, vehicle))
end

local function CheckVechile(player, context, worldobjects, test)
    player = getSpecificPlayer(player);
    local vehicle = getClosestVehicle(player);
    if vehicle and string.find(vehicle:getScriptName(), "AutoTurret") then
        context:addOption(getText("ContextMenu_UninstallTurret"), player, UnInstallTurret, vehicle);
    end
end

Events.OnFillWorldObjectContextMenu.Add(CheckVechile)
