package it.integry.integrywmsnative.view.dialogs.basket_lu.pages.page2;

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

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

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.UtilityNumber;
import it.integry.integrywmsnative.core.utility.UtilityResources;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogBasketLuMtbColrModelBinding;

public class DialogBasketLU_Page2_ListAdapter extends RecyclerView.Adapter<DialogBasketLU_Page2_ListAdapter.ViewHolder>{

    protected Context mContext;

    private List<MtbColr> mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        protected DialogBasketLuMtbColrModelBinding mViewDataBinding;


        public ViewHolder(DialogBasketLuMtbColrModelBinding v) {
            super(v.getRoot());
            mViewDataBinding = v;
        }

        public void bind(MtbColr mtbColt) {
            mViewDataBinding.setMtbColr(mtbColt);
            mViewDataBinding.executePendingBindings();
        }
    }

    public DialogBasketLU_Page2_ListAdapter(Context context, List<MtbColr> myDataset) {
        mContext = context;
        mDataset = myDataset;
    }

    @Override
    public DialogBasketLU_Page2_ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        DialogBasketLuMtbColrModelBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.dialog_basket_lu__mtb_colr_model, parent, false);

        return new ViewHolder(viewDataBinding);
    }

    @Override
    public void onBindViewHolder(DialogBasketLU_Page2_ListAdapter.ViewHolder holder, int position) {
        MtbColr mtbColr = mDataset.get(position);
        holder.bind(mtbColr);

        //Setting qty with unt_mis
        if(!SettingsManager.iDB().isFlagForceAllToColli() && (mtbColr.getMtbAart() == null || mtbColr.getMtbAart().isFlagQtaCnfFissaBoolean())){
            String text = UtilityNumber.decimalToString(mtbColr.getQtaCol());

            if(mtbColr.getMtbAart() != null) {
                text += !UtilityString.isNullOrEmpty(mtbColr.getMtbAart().getUntMis()) ? "\n" + mtbColr.getMtbAart().getUntMis() : "";
            }

            holder.mViewDataBinding.qtaTextview.setText(text);
        } else {
            holder.mViewDataBinding.qtaTextview.setText(UtilityNumber.decimalToString(mtbColr.getNumCnf()) + "\n" + UtilityResources.getString(R.string.unt_mis_col));
        }
    }

    @Override
    public void onViewRecycled(DialogBasketLU_Page2_ListAdapter.ViewHolder holder) {
        super.onViewRecycled(holder);
    }

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

}
