package it.integry.integrywmsnative.view.dialogs.choose_batch_lot.choose_art_from_lista_mtb_colr;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

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.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.DialogChooseArtFromListaArtItemModelBinding;

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

    protected Context mContext;

    private final List<MtbColr> mDataset;
    private final IRecyclerItemClicked<MtbColr> 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.setItem(mtbAart);
            mViewDataBinding.executePendingBindings();
        }
    }

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

    @Override
    public DialogChooseArtFromListaMtbColrAdapter.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(DialogChooseArtFromListaMtbColrAdapter.ViewHolder holder, int position) {
        MtbColr item = mDataset.get(position);
        holder.bind(item.getMtbAart());

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

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

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



}
