package it.integry.integrywmsnative.gest.contenuto_bancale.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.RunnableArgs;
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.ListaContenutoBancaleListModelBinding;

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

    private RunnableArgs<MtbColr> mOnItemClickListener;

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

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


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

        public void bind(MtbColr mtbColr) {
            mViewDataBinding.setMtbColr(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.executePendingBindings();
        }
    }


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

        return new ViewHolder(viewDataBinding);
    }

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


    public ContenutoBancaleListAdapter setOnItemClickListener(RunnableArgs<MtbColr> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }
}
