package it.integry.integrywmsnative.view.dialogs.ask_cliente.viewmodel;

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;

public class DialogAskCliente_Page1_Commessa_ArrayAdapter extends ArrayAdapter<String> implements Filterable {

    private Context mContext;
    private List<String> mDataset = new ArrayList<>();
    private List<String> mDatasetAllItems;

    private final DialogAskCliente_Page1_Commessa_ArrayAdapter.ListFilter listFilter = new DialogAskCliente_Page1_Commessa_ArrayAdapter.ListFilter();

    public DialogAskCliente_Page1_Commessa_ArrayAdapter(@NonNull Context context) {
        super(context, 0);
        mContext = context;


    }

    @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 < getCount()) {
            AppCompatTextView textView = listItem.findViewById(R.id.text);
            textView.setText(getItem(position));
            return listItem;
        }
        return convertView;
    }


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

    public class ListFilter extends Filter {
        private final 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<String> matchValues = new ArrayList<>();

                for (String dataItem : mDatasetAllItems) {
                    if (dataItem.toLowerCase().contains(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<String>) results.values);
            } else {
                mDataset.clear();
            }
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

}
