package it.integry.integrywmsnative.gest.picking_libero.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.ListaPickingLiberoListModelBinding;

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

    private OnItemClickListener mOnItemClickListener;

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

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


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

        public void bind(MtbColr 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() : "";
                }

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

            mViewDataBinding.setMtbColr(mtbColr);
            mViewDataBinding.executePendingBindings();
        }
    }



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

        return new ViewHolder(viewDataBinding);
    }

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

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

        holder.mViewDataBinding.executePendingBindings();
    }


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

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

}
