package it.integry.integrywmsnative.gest.lista_bancali.ui;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedRecyclerView;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.databinding.ListaBancaliListModelBinding;

public class ListaColliMainListAdapter extends ExtendedRecyclerView<MtbColt, ListaColliMainListAdapter.ViewHolder> {


    private RunnableArgs<MtbColt> mOnItemClickListener;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        protected ListaBancaliListModelBinding mViewDataBinding;


        public ViewHolder(ListaBancaliListModelBinding v) {
            super(v.getRoot());
            mViewDataBinding = v;
        }

        public void bind(MtbColt mtbColt) {
            mViewDataBinding.setMtbColt(mtbColt);
            mViewDataBinding.executePendingBindings();
        }
    }

    public ListaColliMainListAdapter(ObservableArrayList<MtbColt> myDataset) {
        super(myDataset);
    }


    @Override
    public ListaColliMainListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        ListaBancaliListModelBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.lista_bancali_list_model, parent, false);

        return new ViewHolder(viewDataBinding);
    }


    @Override
    public void onBindViewHolder(ListaColliMainListAdapter.ViewHolder holder, int position) {
        MtbColt item = mDataset.get(position);
        holder.bind(item);

        holder.mViewDataBinding.getRoot().setOnClickListener(x -> {
            if(mOnItemClickListener != null) {
                mOnItemClickListener.run(item);
            }
        });
    }

    @Override
    public void onViewRecycled(ListaColliMainListAdapter.ViewHolder holder) {
        super.onViewRecycled(holder);
    }




    public ListaColliMainListAdapter setOnItemClickListener(RunnableArgs<MtbColt> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }


}
