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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.zhukic.sectionedrecyclerview.SectionedRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.data_store.db.entity.ArticoloOrdine;
import it.integry.integrywmsnative.core.expansion.OnListGeneralChangedCallback;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.databinding.FragmentPvArticoliOrdineAcquistoListSingleItemBinding;
import it.integry.integrywmsnative.databinding.FragmentPvOrdiniAcquistoListHeaderBinding;
import it.integry.integrywmsnative.ui.fastscroll.SectionTitleProvider;

public class OrdineAcquistoArtListAdapter extends SectionedRecyclerViewAdapter<OrdineAcquistoArtListAdapter.SubheaderHolder, OrdineAcquistoArtListAdapter.SingleItemViewHolder> implements SectionTitleProvider {


    private final Context mContext;

    private final List<OrdineAcquistoArtListModel> mDataset = new ArrayList<>();

    RunnableArgs<ArticoloOrdine> mOnItemSelect;


    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentPvOrdiniAcquistoListHeaderBinding binding;

        SubheaderHolder(FragmentPvOrdiniAcquistoListHeaderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentPvArticoliOrdineAcquistoListSingleItemBinding binding;

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


    public OrdineAcquistoArtListAdapter(Context context, ObservableArrayList<OrdineAcquistoArtListModel> myDataset) {
        mContext = context;

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

    public OrdineAcquistoArtListAdapter setOnItemClicked(RunnableArgs<ArticoloOrdine> onItemSelect) {
        this.mOnItemSelect = onItemSelect;
        return this;
    }



    @Override
    public SubheaderHolder onCreateSubheaderViewHolder(ViewGroup parent, int viewType) {
        FragmentPvOrdiniAcquistoListHeaderBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_pv_ordini_acquisto__list_header, parent, false);
        return new SubheaderHolder(binding);
    }

    @Override
    public SingleItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        FragmentPvArticoliOrdineAcquistoListSingleItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_pv_articoli_ordine_acquisto__list_single_item, parent, false);
        return new SingleItemViewHolder(binding);
    }


    @Override
    public void onBindSubheaderViewHolder(OrdineAcquistoArtListAdapter.SubheaderHolder subheaderHolder, int nextItemPosition) {
        subheaderHolder.binding.title.setText(mContext.getString(R.string.item_list));
    }

    @Override
    public void onBindItemViewHolder(OrdineAcquistoArtListAdapter.SingleItemViewHolder h, int itemPosition) {
        final ArticoloOrdine articoloOrdine = mDataset.get(itemPosition).getOriginalModel();
        final OrdineAcquistoArtListAdapter.SingleItemViewHolder holder = h;

        holder.binding.setArticolo(articoloOrdine);
        holder.binding.getRoot().setOnClickListener(v -> mOnItemSelect.run(articoloOrdine));

    }


    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {
        return false;
    }

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

    @Override
    public String getSectionTitle(int position) {
        return null;
    }

}
