package it.integry.integrywmsnative.view.dialogs.choose_arts_from_lista_arts;

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

import com.annimon.stream.Stream;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.CheckableMtbColr;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.DialogChooseArtsFromListaArtsItemModelBinding;

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

    protected Context mContext;

    private List<CheckableMtbColr> mDataset;


    public DialogChooseArtsFromListaArtsAdapter(Context context, List<MtbColr> myDataset) {
        mContext = context;

        if(myDataset != null) {
            mDataset = Stream.of(myDataset)
                    .withoutNulls()
                    .map(CheckableMtbColr::new).toList();
        }
    }


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


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

        public void bind(CheckableMtbColr checkableMtbColr) {
            mViewDataBinding.setCheckableMtbColr(checkableMtbColr);
            mViewDataBinding.executePendingBindings();
        }
    }

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

        return new ViewHolder(viewDataBinding);
    }

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

    }

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

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

    public List<MtbColr> getSelectedItems() {
        return Stream.of(mDataset)
                .filter(y -> y.getChecked().get())
                .map(CheckableMtbColr::getMtbColr).toList();
    }

}
