package it.integry.integrywmsnative.core.expansion.view;

import android.view.View;

import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.RecyclerView;

import com.zhukic.sectionedrecyclerview.SectionedRecyclerViewAdapter;

import java.util.ArrayList;

import it.integry.integrywmsnative.core.expansion.OnListGeneralChangedCallback;

public abstract class ExtendedSectionedRecyclerView<T, SH extends RecyclerView.ViewHolder, VH extends RecyclerView.ViewHolder>
        extends SectionedRecyclerViewAdapter<SH, VH> {

    protected ArrayList<T> mDataset = new ArrayList<>();
    private View mEmptyView;

    public ExtendedSectionedRecyclerView(ObservableArrayList<T> myDataset) {
        mDataset.addAll(myDataset);

        myDataset.addOnListChangedCallback(new OnListGeneralChangedCallback<T>() {
            @Override
            public void onChanged(ObservableList<T> sender) {
                mDataset.clear();
                mDataset.addAll(sender);
                notifyDataChanged();
                notifyDataSetChanged();
                checkIfEmpty();
            }
        });

        checkIfEmpty();
    }

    public ExtendedSectionedRecyclerView<T, SH, VH> setEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
        this.checkIfEmpty();
        return this;
    }

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

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

}
