package it.integry.integrywmsnative.gest.picking_libero.core;

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

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.RecyclerView;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.ListaPickingLiberoListModelBinding;

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

    protected Context mContext;
    protected ObservableArrayList<MtbColr> mDataset;

    private IRecyclerItemClicked<MtbColr> mOnItemClickListener;

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


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

        public void bind(MtbColr mtbColr) {
            mViewDataBinding.setMtbColr(mtbColr);
            mViewDataBinding.executePendingBindings();
        }
    }



    public PickingLiberoListAdapter(Context context, ObservableArrayList<MtbColr> myDataset, IRecyclerItemClicked<MtbColr> onItemClickListener) {
        mContext = context;
        mDataset = myDataset;
        mOnItemClickListener = onItemClickListener;

        myDataset.addOnListChangedCallback(onListChangedCallback);
    }

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

        return new PickingLiberoListAdapter.ViewHolder(viewDataBinding);
    }

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

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

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

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



    private ObservableList.OnListChangedCallback onListChangedCallback = new ObservableList.OnListChangedCallback<ObservableList<MtbColr>>(){

        @Override
        public void onChanged(ObservableList<MtbColr> sender) {
            notifyDataSetChanged();
        }

        @Override
        public void onItemRangeChanged(ObservableList<MtbColr> sender, int positionStart, int itemCount) {
            notifyDataSetChanged();
        }

        @Override
        public void onItemRangeInserted(ObservableList<MtbColr> sender, int positionStart, int itemCount) {
            notifyDataSetChanged();
        }

        @Override
        public void onItemRangeMoved(ObservableList<MtbColr> sender, int fromPosition, int toPosition, int itemCount) {
            notifyDataSetChanged();
        }

        @Override
        public void onItemRangeRemoved(ObservableList<MtbColr> sender, int positionStart, int itemCount) {
            notifyDataSetChanged();
        }
    };

}
