package it.integry.integrywmsnative.ui.adapter;

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

import androidx.annotation.NonNull;

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

public class SimpleAutoCompleteDropdownAdapter extends ArrayAdapter {

    private List<Object> mObjects;

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource, @NonNull Object[] objects) {
        super(context, resource, objects);
        mObjects = Arrays.asList(objects);
    }

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull Object[] objects) {
        super(context, resource, textViewResourceId, objects);
        mObjects = Arrays.asList(objects);
    }

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
        mObjects = objects;
    }

    public SimpleAutoCompleteDropdownAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List objects) {
        super(context, resource, textViewResourceId, objects);
        mObjects = objects;
    }


    @NonNull
    @Override
    public Filter getFilter() {
        return new StringFilter(mObjects);
    }




    private class StringFilter<T> extends Filter {

        private ArrayList<T> sourceObjects;

        public StringFilter(List<T> objects) {
            if(objects == null) return;
            sourceObjects = new ArrayList<T>();
            synchronized (this) {
                sourceObjects.addAll(objects);
            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence chars) {
            String filterSeq = chars.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (filterSeq != null && filterSeq.length() > 0) {
                ArrayList<T> filter = new ArrayList<T>();

                for (T object : sourceObjects) {
                    // the filtering itself:
                    if (object.toString().toLowerCase().contains(filterSeq))
                        filter.add(object);
                }
                result.count = filter.size();
                result.values = filter;
            } else {
                // add all objects
                synchronized (this) {
                    result.values = sourceObjects;
                    result.count = sourceObjects.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            // NOTE: this function is *always* called from the UI thread.
            if(constraint != null && constraint.length() > 2) {
                ArrayList<T> filtered = (ArrayList<T>) results.values;
                notifyDataSetChanged();
                clear();
                for (int i = 0, l = filtered.size(); i < l; i++)
                    add(filtered.get(i));
                notifyDataSetInvalidated();
            }
        }
    }


}
