package it.integry.integrywmsnative.gest.rettifica_giacenze.core.adapter;

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.View;
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.ListaRettificaGiacenzeModelBinding;

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

    protected Context mContext;
    protected ObservableArrayList<MtbColr> mDataset;
    private View mEmptyView;

    private IRecyclerItemClicked<MtbColr> mOnItemClickListener;

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


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

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

    public RettificaGiacenzeMainListAdapter(Context context, ObservableArrayList<MtbColr> myDataset, IRecyclerItemClicked<MtbColr> onItemClickListener, View emptView) {
        mContext = context;
        mDataset = myDataset;
        mEmptyView = emptView;
        mOnItemClickListener = onItemClickListener;

        myDataset.addOnListChangedCallback(onListChangedCallback);

        checkIfEmpty();
    }

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

        return new ViewHolder(viewDataBinding);
    }

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

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

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

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

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


    void checkIfEmpty() {
        if (mEmptyView != null) {
            final boolean emptyViewVisible = getItemCount() == 0;
            mEmptyView.setVisibility(emptyViewVisible ? View.VISIBLE : View.GONE);
        }
    }

}
