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

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.ListaContenutoBancaleListModelBinding;

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


    protected Context mContext;
    protected ObservableArrayList<MtbColr> mDataset;

    private IRecyclerItemClicked<MtbColr> mOnItemClickListener;

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


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

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


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

        myDataset.addOnListChangedCallback(onListChangedCallback);
    }

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

        return new ViewHolder(viewDataBinding);
    }

    @Override
    public void onBindViewHolder(ContenutoBancaleListAdapter.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(ContenutoBancaleListAdapter.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();
        }
    };


}
