package it.integry.integrywmsnative.gest.prod_linee_produzione.ui;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedRecyclerView;
import it.integry.integrywmsnative.databinding.FragmentLineeProdMainListModelBinding;
import it.integry.integrywmsnative.gest.prod_linee_produzione.dto.ProdLineStatusDTO;

public class LinesListAdapter extends ExtendedRecyclerView<ProdLineStatusDTO, LinesListAdapter.SingleItemViewHolder> {

    private RunnableArgs<ProdLineStatusDTO> mOnItemClicked;

    public LinesListAdapter(ObservableArrayList<ProdLineStatusDTO> myDataset) {
        super(myDataset);
    }

    @NonNull
    @Override
    public LinesListAdapter.SingleItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FragmentLineeProdMainListModelBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_linee_prod_main_list_model, parent, false);
        return new LinesListAdapter.SingleItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull LinesListAdapter.SingleItemViewHolder holder, int position) {
        final ProdLineStatusDTO line = mDataset.get(position);

        holder.binding.setListModel(line);
        holder.binding.executePendingBindings();

        holder.binding.getRoot().setOnClickListener(v -> {
            if (this.mOnItemClicked != null)
                this.mOnItemClicked.run(line);
        });
    }


    public LinesListAdapter setOnItemClicked(RunnableArgs<ProdLineStatusDTO> onItemClicked) {
        this.mOnItemClicked = onItemClicked;
        return this;
    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentLineeProdMainListModelBinding binding;

        SingleItemViewHolder(FragmentLineeProdMainListModelBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
