package ch.spherIC.recurvebowsight.utilities;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * Almost unbelievably, if we want to create a Material Spinner,
 * we're forced to subclass ArrayAdapter.  That statement alone
 * is maddening.  The issue is that there's no such thing as a Material
 * Spinner. Instead, there's an Exposed Dropdown Menu, which is really
 * an AutoCompleteTextView wrapped in a TextInputLayout, which replaces a
 * Spinner. The reason we have to subclass ArrayAdapter is because we need
 * the AutoCompleteTextView to act like a proper Spinner.  Thus we have to
 * override the AutoCompleteTextView's Filter so that it NEVER performs
 * filtering of the dropdown menu items.
 */
public class NoFilterArrayAdapter<T extends Object> extends ArrayAdapter<T> {

    private final List<T> objects;
    private final Filter filter_that_does_nothing = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            results.values = NoFilterArrayAdapter.this.objects;
            results.count = NoFilterArrayAdapter.this.objects.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            notifyDataSetChanged();
        }
    };

    /**
     * Constructor
     *
     * @param context  the context
     * @param resource the resource
     * @param objects  the list of objects
     */
    public NoFilterArrayAdapter(@NonNull Context context, int resource, @NonNull List<T> objects) {
        super(context, resource, objects);
        this.objects = objects;
    }

    /**
     * Constructor
     *
     * @param context  the context
     * @param resource the resource
     * @param objects  the list of objects
     */
    public NoFilterArrayAdapter(@NonNull Context context, int resource, @NonNull T[] objects) {
        super(context, resource, objects);
        this.objects = Arrays.asList(objects);
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return filter_that_does_nothing;
    }
}
