package cgeo.geocaching.filter;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.IGeoData;
import cgeo.geocaching.R;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.geopoint.Geopoint;

import java.util.ArrayList;
import java.util.List;

class DistanceFilter extends AbstractFilter {
    private final IGeoData geo;
    private final int minDistance;
    private final int maxDistance;

    public DistanceFilter(String name, final int minDistance, final int maxDistance) {
        super(name);
        this.minDistance = minDistance;
        this.maxDistance = maxDistance;
        geo = cgeoapplication.getInstance().currentGeo();
    }

    @Override
    public boolean accepts(final Geocache cache) {
        final Geopoint currentPos = new Geopoint(geo.getLocation());
        final Geopoint coords = cache.getCoords();
        if (coords == null) {
            // If a cache has no coordinates, consider it to be out of range. It will
            // happen with archived cache.
            return false;
        }
        final float distance = currentPos.distanceTo(coords);
        return distance >= minDistance && distance <= maxDistance;
    }

    public static class Factory implements IFilterFactory {

        private static final int[] KILOMETERS = { 0, 2, 5, 10, 20, 50 };

        @Override
        public List<IFilter> getFilters() {
            final List<IFilter> filters = new ArrayList<IFilter>(KILOMETERS.length);
            for (int i = 0; i < KILOMETERS.length; i++) {
                final int minRange = KILOMETERS[i];
                final int maxRange;
                if (i < KILOMETERS.length - 1) {
                    maxRange = KILOMETERS[i + 1];
                }
                else {
                    maxRange = Integer.MAX_VALUE;
                }
                final String range = maxRange == Integer.MAX_VALUE ? "> " + String.valueOf(minRange) : String.valueOf(minRange) + " - " + String.valueOf(maxRange);
                final String name = cgeoapplication.getInstance().getResources().getQuantityString(R.plurals.tts_kilometers, maxRange, range);
                filters.add(new DistanceFilter(name, minRange, maxRange));
            }
            return filters;
        }

    }
}

@@ -1883,39 +1883,24 @@
  %22%3E %22 + 
-String.valueOf(
 minRange
 ) : Stri
@@ -1895,35 +1895,19 @@
 ange
-)
  : 
-String.valueOf(
 minRange
 ) + 
@@ -1902,17 +1902,16 @@
 minRange
-)
  + %22 - %22
@@ -1917,31 +1917,16 @@
 %22 + 
-String.valueOf(
 maxRange
 );%0A 
@@ -1917,25 +1917,24 @@
 %22 + maxRange
-)
 ;%0A          

remove useless calls to String.valueOf()