package it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.dialog_ask_mag_prossimita;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.JtbFasi;

public class DialogAskMagazzinoProssimitaAdapter extends ArrayAdapter<JtbFasi> implements Filterable {

    private final Context mContext;
    private final List<JtbFasi> mDataset;
    private List<JtbFasi> mDatasetAllItems;

    private final ListFilter listFilter = new ListFilter();

    public DialogAskMagazzinoProssimitaAdapter(@NonNull Context context, @NonNull List<JtbFasi> list) {
        super(context, 0, list);
        mContext = context;
        mDataset = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(mContext).inflate(R.layout.dropdown_simple_item, parent, false);
        }

        if (position < mDataset.size()) {
            AppCompatTextView textView = listItem.findViewById(R.id.text);
            textView.setText(mDataset.get(position).toString());
            return listItem;
        }

        return null;
    }




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

    public class ListFilter extends Filter {
        private Object lock = new Object();

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            if (mDatasetAllItems == null) {
                synchronized (lock) {
                    mDatasetAllItems = new ArrayList<>(mDataset);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized (lock) {
                    results.values = mDatasetAllItems;
                    results.count = mDatasetAllItems.size();
                }
            } else {
                final String searchStrLowerCase = prefix.toString().toLowerCase();

                ArrayList<JtbFasi> matchValues = new ArrayList<>();

                for (JtbFasi dataItem : mDatasetAllItems) {
                    if (dataItem.getCodJfas().toLowerCase().startsWith(searchStrLowerCase) ||
                            dataItem.getDescrizione().toLowerCase().startsWith(searchStrLowerCase)) {
                        matchValues.add(dataItem);
                    }
                }

                results.values = matchValues;
                results.count = matchValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.values != null) {
                mDataset.clear();
                mDataset.addAll((ArrayList<JtbFasi>) results.values);
            } else {
                mDataset.clear();
            }
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

}
