package it.integry.integrywmsnative.view.dialogs.basket_lu.pages.page1;

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.MtbColt;
import it.integry.integrywmsnative.databinding.DialogBasketLuMtbColtModelBinding;

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

    protected Context mContext;

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

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


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

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

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

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

        return new ViewHolder(viewDataBinding);
    }

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

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

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

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


}
