package cgeo.geocaching.filters.core;

import cgeo.geocaching.models.Geocache;
import cgeo.geocaching.storage.SqlBuilder;
import cgeo.geocaching.utils.expressions.ExpressionConfig;

import androidx.annotation.NonNull;

public class DifficultyAndTerrainGeocacheFilter extends BaseGeocacheFilter {

    public DifficultyGeocacheFilter difficultyGeocacheFilter;
    public TerrainGeocacheFilter terrainGeocacheFilter;

    private static final String CONFIG_DIFFICULTY = "difficulty";
    private static final String CONFIG_TERRAIN = "terrain";


    public DifficultyAndTerrainGeocacheFilter() {
        difficultyGeocacheFilter = GeocacheFilterType.DIFFICULTY.create();
        terrainGeocacheFilter = GeocacheFilterType.TERRAIN.create();
    }

    @Override
    public Boolean filter(final Geocache cache) {
        if (cache == null) {
            return null;
        }

        final Boolean diffRange = difficultyGeocacheFilter.filter(cache);
        final Boolean terrainRange = terrainGeocacheFilter.filter(cache);
        if (diffRange == null) {
            return terrainRange;
        }
        if (terrainRange == null) {
            return diffRange;
        }

        return diffRange && terrainRange;
    }

    @Override
    public boolean isFiltering() {
        return difficultyGeocacheFilter.isFiltering() || terrainGeocacheFilter.isFiltering();
    }

    @Override
    public void addToSql(final SqlBuilder sqlBuilder) {
        difficultyGeocacheFilter.addToSql(sqlBuilder);
        terrainGeocacheFilter.addToSql(sqlBuilder);
    }

    @Override
    public void setConfig(final ExpressionConfig config) {
        setToFilterConfig(difficultyGeocacheFilter, CONFIG_DIFFICULTY, config);
        setToFilterConfig(terrainGeocacheFilter, CONFIG_TERRAIN, config);
    }

    @Override
    public ExpressionConfig getConfig() {
        final ExpressionConfig config = new ExpressionConfig();
        addFromFilterConfig(difficultyGeocacheFilter, CONFIG_DIFFICULTY, config);
        addFromFilterConfig(terrainGeocacheFilter, CONFIG_TERRAIN, config);
        return config;
    }

    @Override
    protected String getUserDisplayableConfig() {
        return difficultyGeocacheFilter.getUserDisplayableConfig() + " / " + terrainGeocacheFilter.getUserDisplayableConfig();
    }

    private static void addFromFilterConfig(final NumberRangeGeocacheFilter<?> filter, final String key, @NonNull final ExpressionConfig config) {
        config.put(key, filter.getConfig().getDefaultList());
    }

    private static void setToFilterConfig(final NumberRangeGeocacheFilter<?> filter, final String key, @NonNull final ExpressionConfig config) {
        filter.setConfig(config.getSubConfig(key));
    }
}

@@ -440,32 +440,36 @@
 l String CONFIG_
+KEY_
 DIFFICULTY = %22di
@@ -467,25 +467,16 @@
 LTY = %22d
-ifficulty
 %22;%0A    p
@@ -505,24 +505,28 @@
 ring CONFIG_
+KEY_
 TERRAIN = %22t
@@ -525,22 +525,16 @@
 AIN = %22t
-errain
 %22;%0A%0A%0A   
@@ -1668,32 +1668,36 @@
 eFilter, CONFIG_
+KEY_
 DIFFICULTY, conf
@@ -1749,32 +1749,36 @@
 eFilter, CONFIG_
+KEY_
 TERRAIN, config)
@@ -1967,16 +1967,20 @@
  CONFIG_
+KEY_
 DIFFICUL
@@ -2050,16 +2050,20 @@
  CONFIG_
+KEY_
 TERRAIN,

use shorter key for D/T-config (fix #11362) (#11407)