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

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.MtbPartitaMag;
import it.integry.integrywmsnative.databinding.DialogChooseBatchLotItemModelBinding;

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

    protected Context mContext;

    private final List<MtbPartitaMag> mDataset;
    private final IRecyclerItemClicked<MtbPartitaMag> mOnItemClickListener;


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


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

        public void bind(MtbPartitaMag mtbPartitaMag) {
            mViewDataBinding.setMtbPartitaMag(mtbPartitaMag);
            mViewDataBinding.executePendingBindings();
        }
    }

    public DialogChooseBatchLotAdapter(List<MtbPartitaMag> myDataset, IRecyclerItemClicked<MtbPartitaMag> onItemClickListener) {
        mDataset = myDataset;
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public DialogChooseBatchLotAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        DialogChooseBatchLotItemModelBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.dialog_choose_batch_lot_item_model, parent, false);
        return new DialogChooseBatchLotAdapter.ViewHolder(viewDataBinding);
    }

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

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

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

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

}
