package cgeo.geocaching.filter;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;

import android.content.res.Resources;

import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;

class AttributeFilter extends AbstractFilter {

    private final String attribute;

    public AttributeFilter(final String name, final String attribute) {
        super(name);
        this.attribute = attribute;
    }

    private static String getName(final String attribute, final Resources res, final String packageName) {
        // dynamically search for a translation of the attribute
        final int id = res.getIdentifier(attribute, "string", packageName);
        return id > 0 ? res.getString(id) : attribute;
    }

    @Override
    public boolean accepts(final Geocache cache) {
        Geocache fullCache = DataStore.loadCache(cache.getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES));
        if (fullCache == null) {
            fullCache = cache;
        }
        return fullCache.getAttributes().contains(attribute);
    }

    public static class Factory implements IFilterFactory {

        @Override
        public List<IFilter> getFilters() {
            final String packageName = CgeoApplication.getInstance().getBaseContext().getPackageName();
            final Resources res = CgeoApplication.getInstance().getResources();

            final List<IFilter> filters = new LinkedList<IFilter>();
            for (final String id: res.getStringArray(R.array.attribute_ids)) {
                filters.add(new AttributeFilter(getName("attribute_" + id, res, packageName), id));
            }
            return filters;
        }

    }
}

@@ -70,42 +70,8 @@
 on;%0A
-import cgeo.geocaching.DataStore;%0A
 impo
@@ -128,64 +128,8 @@
 g.R;
-%0Aimport cgeo.geocaching.enumerations.LoadFlags.LoadFlag;
 %0A%0Aim
@@ -169,34 +169,8 @@
 s;%0A%0A
-import java.util.EnumSet;%0A
 impo
@@ -818,190 +818,8 @@
 ) %7B%0A
-        Geocache fullCache = DataStore.loadCache(cache.getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES));%0A        if (fullCache == null) %7B%0A            fullCache = cache;%0A        %7D%0A
     
@@ -833,13 +833,9 @@
 urn 
-fullC
+c
 ache

work on #3780: do not reload caches to get their attributes