W900CisternParts = W900CisternParts or {}

function W900CisternParts.hasPartContainer(part)
    return part and part.isContainer and part:isContainer() and part.getItemContainer and part:getItemContainer() ~= nil
end

function W900CisternParts.getGasTankPart(vehicle)
    return vehicle and vehicle:getPartById("2000TankG") or nil
end

function W900CisternParts.getWaterTankPart(vehicle)
    return vehicle and vehicle:getPartById("2000TankW") or nil
end

function W900CisternParts.isSplitCisternVehicle(vehicle)
    return W900CisternParts.getGasTankPart(vehicle) or W900CisternParts.getWaterTankPart(vehicle)
end

function W900CisternParts.isInstalledPart(part)
    return part and part:isContainer() and (part:getInventoryItem() or W900CisternParts.hasPartContainer(part))
end

function W900CisternParts.isValidGasSourcePart(part)
    if not W900CisternParts.isInstalledPart(part) then
        return false
    end

    local vehicle = part:getVehicle()
    local waterPart = W900CisternParts.getWaterTankPart(vehicle)

    return part:getId() == "2000TankG"
        and part:getContainerContentType() == "Gasoline"
        and (not waterPart or not W900CisternParts.isInstalledPart(waterPart))
end

function W900CisternParts.isHydrantFillTarget(part)
    return W900CisternParts.isInstalledPart(part)
        and part:getId() == "2000TankW"
        and part:getContainerContentType() == "Water"
end

function W900CisternParts.isValidVehicleGasDestination(part)
    if not part then
        return false
    end

    return part:getId() == "GasTank"
        and part:isContainer()
        and part:getContainerContentType() == "Gasoline"
        and not W900CisternParts.isSplitCisternVehicle(part:getVehicle())
end

function W900CisternParts.isTowVehicleForSplitCistern(vehicle)
    local trailer = vehicle and vehicle:getVehicleTowing() or nil
    return W900CisternParts.isSplitCisternVehicle(trailer)
end

return W900CisternParts