-- W900 Cistern - Disable TsarsLib Integration
-- We want to use vanilla-style real-time fuel transfer instead of TsarsLib's end-of-action transfer

require "W900_CisternParts"

if not W900_TsarsLibDisabler then W900_TsarsLibDisabler = {} end

local function forEachLoadedVehicle(cell, visitor)
    if type(visitor) ~= "function" or not cell or not cell.getVehicles then
        return
    end

    local vehicles = cell:getVehicles()
    if not vehicles then
        return
    end

    if type(vehicles.size) == "function" and type(vehicles.get) == "function" then
        for index = 0, vehicles:size() - 1 do
            visitor(vehicles:get(index), index)
        end
        return
    end

    if type(vehicles.iterator) == "function" then
        local ok, iterator = pcall(function() return vehicles:iterator() end)
        if ok and iterator then
            local index = 0
            while iterator:hasNext() do
                visitor(iterator:next(), index)
                index = index + 1
            end
            return
        end
    end

    local count = nil
    if type(vehicles.size) == "function" then
        local ok, sizeValue = pcall(function() return vehicles:size() end)
        if ok then
            count = sizeValue
        end
    elseif type(vehicles) == "table" then
        count = #vehicles
    end

    if count then
        for index = 0, count - 1 do
            local ok, vehicle = pcall(function() return vehicles[index] end)
            if not ok or vehicle == nil then
                ok, vehicle = pcall(function() return vehicles[index + 1] end)
            end
            if ok and vehicle then
                visitor(vehicle, index)
            end
        end
        return
    end

    if type(vehicles) == "table" then
        for index, vehicle in ipairs(vehicles) do
            visitor(vehicle, index - 1)
        end
    end
end

local function findVehicleGasCompat(playerObj, playerVehicle)
    if not playerObj or not playerObj.getCell then
        return false
    end

    local radius = 5
    local result = false

    forEachLoadedVehicle(playerObj:getCell(), function(vehicle)
        if result or not vehicle or vehicle == playerVehicle then
            return
        end

        for index = 0, vehicle:getPartCount() - 1 do
            local part = vehicle:getPartByIndex(index)
            if part
                and part.isContainer and part:isContainer()
                and part.getContainerContentType and part:getContainerContentType() == "Gasoline Storage"
                and part.getContainerContentAmount and part:getContainerContentAmount() > 0 then
                local deltaX = math.abs(vehicle:getX() - playerObj:getX())
                local deltaY = math.abs(vehicle:getY() - playerObj:getY())
                if deltaX < radius and deltaY < radius then
                    result = part
                    return
                end
            end
        end
    end)

    return result or false
end

-- Wait for TsarsLib to load, then override its function
local function disableTsarsLibForW900Cistern()
    -- Check if TsarsLib's LiqudTanker is loaded
    if not LiqudTanker then 
        ----print(("W900: TsarsLib LiqudTanker not found, skipping override")
        return 
    end
    
    ----print(("W900: Disabling TsarsLib for W900 Cistern tanks")
    
    -- Store the original TsarsLib function
    if not W900_TsarsLibDisabler.original_getNearbyFuelPumpForTank then
        W900_TsarsLibDisabler.original_getNearbyFuelPumpForTank = LiqudTanker.getNearbyFuelPumpForTank
    end
    
    -- Override to exclude our 2000TankG from TsarsLib processing
    function LiqudTanker.getNearbyFuelPumpForTank(vehicle)
        -- Check if this vehicle has our custom W900 2000TankG
        local gas_part = W900CisternParts.getGasTankPart(vehicle)
        local water_part = W900CisternParts.getWaterTankPart(vehicle)
        if gas_part or water_part then
            -- Return nil to prevent TsarsLib from handling our tank
            -- Our own W900_CisternRefuel will handle it instead
            return nil
        end
        
        -- For other vehicles, use original TsarsLib function
        return W900_TsarsLibDisabler.original_getNearbyFuelPumpForTank(vehicle)
    end
    
    -- Also override the FindVehicleGas to exclude our tank from vehicle-to-vehicle transfers
    if not W900_TsarsLibDisabler.original_FindVehicleGas then
        W900_TsarsLibDisabler.original_FindVehicleGas = FindVehicleGas
    end
    
    function FindVehicleGas(playerObj, playerVehicle)
        local part = findVehicleGasCompat(playerObj, playerVehicle)
        if not part then
            return part
        end

        if not part.getVehicle or not part.getId then
            return part
        end

        local vehicle = part:getVehicle()
        if not vehicle then
            return part
        end

        if part:getId() == "2000TankG" or part:getId() == "2000TankW" or W900CisternParts.isTowVehicleForSplitCistern(vehicle) then
            return false
        end

        return part
    end
    
    ----print(("W900: TsarsLib override complete - W900 Cistern will use vanilla-style refueling")
end

-- Try to override immediately
disableTsarsLibForW900Cistern()

-- Also try after a delay in case TsarsLib loads later
Events.OnGameStart.Add(function()
    disableTsarsLibForW900Cistern()
end)

----print(("W900: TsarsLib disabler loaded")
