package it.integry.integrywmsnative.gest.lista_bancali.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 android.view.View;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.databinding.ListaBancaliListModelBinding;

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

    protected Context mContext;
    protected ObservableArrayList<MtbColt> mDataset;

    private View mEmptyView;

    private IRecyclerItemClicked<MtbColt> mOnItemClickListener;

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


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

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

    public MainListListaColliAdapter(Context context, ObservableArrayList<MtbColt> myDataset, IRecyclerItemClicked<MtbColt> onItemClickListener, View emptyView) {
        mContext = context;
        mDataset = myDataset;
        mEmptyView = emptyView;
        mOnItemClickListener = onItemClickListener;

        myDataset.addOnListChangedCallback(onListChangedCallback);

        checkIfEmpty();
    }


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

        return new ViewHolder(viewDataBinding);
    }


    @Override
    public void onBindViewHolder(MainListListaColliAdapter.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(MainListListaColliAdapter.ViewHolder holder) {
        super.onViewRecycled(holder);
    }


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


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


    private ObservableList.OnListChangedCallback onListChangedCallback = new ObservableList.OnListChangedCallback<ObservableList<MtbColt>>() {
        @Override
        public void onChanged(ObservableList<MtbColt> sender) {
            notifyDataSetChanged();
            checkIfEmpty();
        }

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

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

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

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

}
