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

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

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

import com.annimon.stream.Stream;
import com.zhukic.sectionedrecyclerview.SectionedRecyclerViewAdapter;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.data_store.db.entity.Ordine;
import it.integry.integrywmsnative.core.data_store.db.wrappers.OrdineWrapper;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.databinding.FragmentPvOrdiniAcquistoListHeaderBinding;
import it.integry.integrywmsnative.databinding.FragmentPvOrdiniAcquistoListSingleItemBinding;
import it.integry.integrywmsnative.ui.fastscroll.SectionTitleProvider;

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


    private Context mContext;

    private List<OrdineWrapper> mDataset;
    private RunnableArgs<Ordine> mOnItemSelect;


    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentPvOrdiniAcquistoListHeaderBinding binding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentPvOrdiniAcquistoListSingleItemBinding binding;

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


    public OrdineAcquistoPvOpenListAdapter(Context context, List<OrdineWrapper> myDataset, RunnableArgs<Ordine> onItemSelect) {
        mContext = context;
        mDataset = orderItems(myDataset);
        mOnItemSelect = onItemSelect;
    }

    public void updateItems(List<OrdineWrapper> updatedDataset) {
        mDataset.clear();
        mDataset.addAll(orderItems(updatedDataset));
        notifyDataSetChanged();
        notifyDataChanged();
    }


    private List<OrdineWrapper> orderItems(List<OrdineWrapper> dataset) {
        return Stream.of(dataset)
                .distinctBy(OrdineWrapper::getOrdine)
                .sortBy(x -> x.getOrdine().getOrdineId())
                .toList();

    }


    @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) {
        FragmentPvOrdiniAcquistoListSingleItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_pv_ordini_acquisto__list_single_item, parent, false);
        return new SingleItemViewHolder(binding);
    }


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

    @Override
    public void onBindItemViewHolder(OrdineAcquistoPvOpenListAdapter.SingleItemViewHolder h, int itemPosition) {
        final OrdineWrapper ordineWrapper = mDataset.get(itemPosition);
        final Ordine ordine = ordineWrapper.getOrdine();
        final OrdineAcquistoPvOpenListAdapter.SingleItemViewHolder holder = h;


        String testataOrdString = String.format(mContext.getString(R.string.ord_acq_testata), ordine.getOrdineId(), UtilityDate.formatDate(ordine.getDataIns(), UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN));

        holder.binding.numArts.setText(Html.fromHtml(String.format(mContext.getString(R.string.ord_acq_num_articoli), String.valueOf(ordineWrapper.getCountArticoli()))));

        holder.binding.orderIdentifier.setText(Html.fromHtml(testataOrdString));

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

    }


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

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

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

}
