-- 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

-- 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 original = W900_TsarsLibDisabler.original_FindVehicleGas
        if not original then
            return false
        end

        local part = original(playerObj, playerVehicle)
        if not part 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")
