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

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

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.data_store.db.entity.SqlMtbColt;
import it.integry.integrywmsnative.core.data_store.db.wrappers.DocInternoWrapper;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedRecyclerView;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.databinding.DocInterniListModelBinding;

public class DocInterniListAdapter extends ExtendedRecyclerView<DocInterniListModel, DocInterniListAdapter.SingleItemViewHolder> {

    private Context mContext;

    private RunnableArgs<SqlMtbColt> mOnItemClicked;

    public DocInterniListAdapter(Context context, ObservableArrayList<DocInterniListModel> myDataset) {
        super(myDataset);
        this.mContext = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull SingleItemViewHolder holder, int position) {
        final DocInterniListModel doc = mDataset.get(position);
        SqlMtbColt mtbColt = doc.getDocument();

        String testataDocString = String.format(mContext.getString(R.string.doc_interni_doc_header), mtbColt.getId(), mtbColt.getCodDtipProvv());
        holder.binding.dataDoc.setText(Html.fromHtml(UtilityDate.formatDate(mtbColt.getDataCollo(), UtilityDate.COMMONS_DATE_FORMATS.DM_HUMAN)));
        holder.binding.docDescription.setText(Html.fromHtml(testataDocString));
        holder.binding.numRows.setText(Html.fromHtml(String.format(mContext.getString(R.string.doc_interni_num_arts_doc), doc.getProductsCount())));

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


    public void setOnItemClicked(RunnableArgs<SqlMtbColt> onItemClicked){
        this.mOnItemClicked = onItemClicked;
    }
    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        DocInterniListModelBinding binding;

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


}
