package com.boardgamegeek.filterer;
import android.content.Context;
import android.support.annotation.NonNull;
import com.boardgamegeek.R;
import com.boardgamegeek.provider.BggContract.Games;
import com.boardgamegeek.util.MathUtils;
import com.boardgamegeek.util.StringUtils;
public class SuggestedAgeFilterer extends CollectionFilterer {
public static final int MIN_RANGE = 1;
public static final int MAX_RANGE = 21;
private int min;
private int max;
private boolean includeUndefined;
public SuggestedAgeFilterer(Context context) {
super(context);
}
public SuggestedAgeFilterer(@NonNull Context context, int min, int max, boolean includeUndefined) {
super(context);
this.min = min;
this.max = max;
this.includeUndefined = includeUndefined;
}
@Override
public void setData(@NonNull String data) {
String[] d = data.split(DELIMITER);
min = d.length > 0 ? MathUtils.constrain(StringUtils.parseInt(d[0], MIN_RANGE), MIN_RANGE, MAX_RANGE) : MIN_RANGE;
max = d.length > 1 ? MathUtils.constrain(StringUtils.parseInt(d[1], MAX_RANGE), MIN_RANGE, MAX_RANGE) : MAX_RANGE;
includeUndefined = d.length > 2 && (d[2].equals("1"));
}
@Override
public int getTypeResourceId() {
return R.string.collection_filter_type_suggested_age;
}
@Override
public String getDisplayText() {
String text;
String minText = String.valueOf(min);
String maxText = String.valueOf(max);
if (max == MAX_RANGE) {
text = minText + "+";
} else if (min == max) {
text = maxText;
} else {
text = minText + "-" + maxText;
}
if (includeUndefined) {
text += " (+?)";
}
return context.getString(R.string.ages) + " " + text;
}
@Override
public String getSelection() {
String selection;
if (max == MAX_RANGE) {
selection = "(" + Games.MINIMUM_AGE + ">=?)";
} else {
selection = "(" + Games.MINIMUM_AGE + ">=? AND " + Games.MINIMUM_AGE + "<=?)";
}
if (includeUndefined) {
selection += " OR " + Games.MINIMUM_AGE + "=0 OR " + Games.MINIMUM_AGE + " IS NULL";
}
return selection;
}
@Override
public String[] getSelectionArgs() {
if (max == MAX_RANGE) {
return new String[] { String.valueOf(min) };
} else {
return new String[] { String.valueOf(min), String.valueOf(max) };
}
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
public boolean includeUndefined() {
return includeUndefined;
}
@NonNull
@Override
public String flatten() {
return String.valueOf(min) + DELIMITER + String.valueOf(max) + DELIMITER + (includeUndefined ? "1" : "0");
}
}