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

import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.view.ExtendedRecyclerView;
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.ListaRettificaGiacenzeModelBinding;

public class RettificaGiacenzeMainListAdapter extends ExtendedRecyclerView<MtbColr, RettificaGiacenzeMainListAdapter.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    public RettificaGiacenzeMainListAdapter(ObservableArrayList<MtbColr> myDataset) {
        super(myDataset);
    }

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


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

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


    @Override
    public RettificaGiacenzeMainListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        ListaRettificaGiacenzeModelBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.lista_rettifica_giacenze_model, parent, false);
        return new ViewHolder(viewDataBinding);
    }

    @Override
    public void onBindViewHolder(RettificaGiacenzeMainListAdapter.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));
        }

        holder.mViewDataBinding.getRoot().setOnClickListener(x -> {
            if(mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(holder.mViewDataBinding.getMtbColr());
            }
        });

        holder.mViewDataBinding.executePendingBindings();
    }


    public RettificaGiacenzeMainListAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }

    public interface OnItemClickListener {
        void onItemClick(MtbColr item);
    }

}
