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 androidx.core.content.ContextCompat;

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.dto.DialogAskClienteDestinatarioDTO;

public class DialogAskCliente_Page2_ArrayAdapter extends ArrayAdapter<DialogAskClienteDestinatarioDTO> implements Filterable {

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

    private final ListFilter listFilter = new ListFilter();

    public DialogAskCliente_Page2_ArrayAdapter(@NonNull Context context, @NonNull List<DialogAskClienteDestinatarioDTO> 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.dialog_ask_cliente__dropdown_item_destinatario, parent, false);
        }

        if(position < mDataset.size()) {

            var item = mDataset.get(position);

            AppCompatTextView destinatario = listItem.findViewById(R.id.destinatario);
            destinatario.setText(UtilityString.capitalizeWords(item.getDestinatario() + (item.getCodVdes() != null ? ("(" + item.getCodVdes() + ")") : "")));
            if(item.getCodVdes() == null) destinatario.setTextColor(ContextCompat.getColor(mContext, R.color.gray_800));

            AppCompatTextView subtitle = listItem.findViewById(R.id.subtitle);

            StringBuilder subtitleStr = new StringBuilder();
            if(!UtilityString.isNullOrEmpty(mDataset.get(position).getCitta())) {
                subtitleStr.append(UtilityString.capitalizeWords(item.getCitta())).append(" - ");
            }

            if(!UtilityString.isNullOrEmpty(mDataset.get(position).getIndirizzo())) {
                subtitleStr.append(UtilityString.capitalizeWords(item.getIndirizzo()));
            }

            subtitle.setText(subtitleStr);


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

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

    }

}
