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

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

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

import com.annimon.stream.Stream;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedSectionedRecyclerView;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.ProdRecuperoMaterialeListHeaderBinding;
import it.integry.integrywmsnative.databinding.ProdRecuperoMaterialeListItemBinding;
import it.integry.integrywmsnative.gest.prod_recupero_materiale.dto.HistoryVersamentoProdULDTO;

public class HistoryULsListAdapter extends ExtendedSectionedRecyclerView<HistoryULsListModel, HistoryULsListAdapter.SubheaderHolder,HistoryULsListAdapter.SingleItemViewHolder> {


    private Context mContext;

    private RunnableArgs<HistoryULsListModel> mOnItemClicked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        ProdRecuperoMaterialeListHeaderBinding binding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        ProdRecuperoMaterialeListItemBinding binding;

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



    public HistoryULsListAdapter(Context context, ObservableArrayList<HistoryULsListModel> myDataset) {
        super(myDataset);
        mContext = context;
    }

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


    private List<HistoryVersamentoProdULDTO> orderItems(List<HistoryVersamentoProdULDTO> dataset) {
        return Stream.of(dataset)
                .sortBy(HistoryVersamentoProdULDTO::getCodJfas)
                .toList();

    }



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

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



    @Override
    public void onBindSubheaderViewHolder(SubheaderHolder subheaderHolder, int nextItemPosition) {
        subheaderHolder.binding.recuperMaterialeMainListGroupHeader.setText(UtilityString.toCamelCase(mDataset.get(nextItemPosition).getGroupTitle()));
    }


    @Override
    public void onBindItemViewHolder(SingleItemViewHolder holder, int itemPosition) {
        final HistoryULsListModel ul = mDataset.get(itemPosition);

        holder.binding.codMart.setText(ul.getCodMart());
        holder.binding.descrizione.setText(ul.getDescrizione());
        holder.binding.numCollo.setText(String.valueOf(ul.getNumCollo()));
        holder.binding.partitaMag.setText("(" + ul.getPartitaMag() + ")");

        holder.binding.qtaVersata.setText(String.valueOf(ul.getQtaVersata()));
        holder.binding.untMisQtaVersata.setText(String.valueOf(ul.getUntMisVersata()));

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


    }


    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {
        if (getItemSize() == 1) return true;
        else if (getItemSize() > 1) {
            HistoryULsListModel compare1 = this.mDataset.get(position);
            HistoryULsListModel compare2 = this.mDataset.get(position + 1);

            if (UtilityString.equalsIgnoreCase(compare1.getGroupTitle(), compare2.getGroupTitle())) {
                return false;
            }
        }

        return true;
    }

}
