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;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.dto.DialogAskClienteClienteDTO;

public class DialogAskCliente_Page1_Cliente_ArrayAdapter extends ArrayAdapter<DialogAskClienteClienteDTO> implements Filterable {

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

    private final ListFilter listFilter = new ListFilter();

    public DialogAskCliente_Page1_Cliente_ArrayAdapter(@NonNull Context context, @NonNull List<DialogAskClienteClienteDTO> 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_w_subtitle, parent, false);
        }

        if(position < mDataset.size()) {
            AppCompatTextView textView = listItem.findViewById(R.id.title);
            AppCompatTextView subTitle = listItem.findViewById(R.id.subtitle);

            textView.setText(mDataset.get(position).getRagSoc());
            subTitle.setText(mDataset.get(position).getCodAnag());

            return listItem;
        }

        return null;
    }


    @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<DialogAskClienteClienteDTO> matchValues = new ArrayList<>();

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

    }

}
