package it.integry.integrywmsnative.gest.rettifica_giacenze.core.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.databinding.RettificaGiacenzeAutocompleteFornitoreLayoutBinding;
import it.integry.integrywmsnative.gest.rettifica_giacenze.dto.FornitoreDTO;

public class AutoCompleteFornitoreAdapter extends ArrayAdapter<FornitoreDTO> {

    private ArrayList<FornitoreDTO> items;
    private ArrayList<FornitoreDTO> itemsAll;
    private ArrayList<FornitoreDTO> suggestions;

    public AutoCompleteFornitoreAdapter(@NonNull Context context, @NonNull ArrayList<FornitoreDTO> items) {
        super(context, 0 , items);
        this.items = items;
        this.itemsAll = (ArrayList<FornitoreDTO>) items.clone();
        this.suggestions = new ArrayList<>();
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        RettificaGiacenzeAutocompleteFornitoreLayoutBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(getContext()),
                R.layout.rettifica_giacenze_autocomplete_fornitore_layout, parent, false);

        binding.setFornitore(items.get(position));

        return binding.getRoot();
    }



    private Filter fornitoreFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (FornitoreDTO fornitore : itemsAll) {
                    String tmpDescr = fornitore.getCodAlis() + " " + fornitore.getDescrizione();

                    if(tmpDescr.toLowerCase().contains(constraint.toString().toLowerCase())){
                        suggestions.add(fornitore);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }


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

            boolean error;

            do {
                try {
                    ArrayList<FornitoreDTO> filteredList = (ArrayList<FornitoreDTO>) results.values;
                    if (results.count > 0) {
                        clear();

                        for (FornitoreDTO c : filteredList) {
                            add(c);
                        }

                        notifyDataSetChanged();

                    }
                    error = false;
                } catch (ConcurrentModificationException ex) {
                    error = true;
                }
            } while (error);


        }


        @Override
        public CharSequence convertResultToString(Object resultValue) {
            return ((FornitoreDTO) resultValue).getCodAlis();
        }
    };
}
