package it.integry.integrywmsnative.view.dialogs.choose_art_from_lista_arts;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.databinding.DialogChooseArtFromListaArtItemModelBinding;

public class DialogChooseArtFromListaArtsAdapter extends RecyclerView.Adapter<DialogChooseArtFromListaArtsAdapter.ViewHolder>  {

    protected Context mContext;

    private List<MtbAart> mDataset;
    private IRecyclerItemClicked<MtbAart> mOnItemClickListener;

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


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

        public void bind(MtbAart mtbAart) {
            mViewDataBinding.setMtbAart(mtbAart);
            mViewDataBinding.executePendingBindings();
        }
    }

    public DialogChooseArtFromListaArtsAdapter(Context context, List<MtbAart> myDataset, IRecyclerItemClicked<MtbAart> onItemClickListener) {
        mContext = context;
        mDataset = myDataset;
        mOnItemClickListener = onItemClickListener;
    }

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

        return new ViewHolder(viewDataBinding);
    }

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

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

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

    @Override
    public int getItemCount() {
        return mDataset.size();
    }



}
