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

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

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

import java.math.BigDecimal;

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.MtbAart;
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.FragmentChooseOrdsLavFromListItemModelBinding;
import it.integry.integrywmsnative.gest.prod_versamento_materiale.dto.VersamentoMerceOrdineLavListModel;

public class ChooseOrdsLavFromListAdapter extends ExtendedRecyclerView<VersamentoMerceOrdineLavListModel, ChooseOrdsLavFromListAdapter.ViewHolder> {

    private final Context mContext;
    private final boolean mForceAllToColli;

    private RunnableArgs<VersamentoMerceOrdineLavListModel> mOnItemChecked;


    public ChooseOrdsLavFromListAdapter(Context context, ObservableArrayList<VersamentoMerceOrdineLavListModel> myDataset, boolean forceAllToColli) {
        super(myDataset);
        mContext = context;
        mForceAllToColli = forceAllToColli;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        protected FragmentChooseOrdsLavFromListItemModelBinding mViewDataBinding;


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

        public void bind(VersamentoMerceOrdineLavListModel versamentoMerceOrdineLavListModel)  {
            mViewDataBinding.setListModel(versamentoMerceOrdineLavListModel);



            versamentoMerceOrdineLavListModel.getQtaBigDecimalObservable().resetOnPropertyChangedCallback();
            versamentoMerceOrdineLavListModel.getQtaBigDecimalObservable().addOnPropertyChangedCallback(() -> {
                setQtaText(versamentoMerceOrdineLavListModel.getOriginalMtbAart(), versamentoMerceOrdineLavListModel.getQtaBigDecimal(), versamentoMerceOrdineLavListModel.getNumCnfBigDecimal());
            });
            setQtaText(versamentoMerceOrdineLavListModel.getOriginalMtbAart(), versamentoMerceOrdineLavListModel.getQtaBigDecimal(), versamentoMerceOrdineLavListModel.getNumCnfBigDecimal());



            versamentoMerceOrdineLavListModel.getQtaBigDecimalObservable().addOnPropertyChangedCallback(() -> {
                setPeso(versamentoMerceOrdineLavListModel.getOriginalMtbAart(), versamentoMerceOrdineLavListModel.getQtaBigDecimal());
            });
            setPeso(versamentoMerceOrdineLavListModel.getOriginalMtbAart(), versamentoMerceOrdineLavListModel.getQtaBigDecimal());


            mViewDataBinding.getRoot().setOnClickListener(v -> {
                versamentoMerceOrdineLavListModel.getSelected().set(!versamentoMerceOrdineLavListModel.getSelected().get());
            });

            mViewDataBinding.executePendingBindings();
            mViewDataBinding.checkbox.jumpDrawablesToCurrentState();
        }

        private void setQtaText(MtbAart mtbAart, BigDecimal qtaText, BigDecimal numCnf) {
            if (!mForceAllToColli && (mtbAart == null || mtbAart.isFlagQtaCnfFissaBoolean())) {
                String text = UtilityNumber.decimalToString(qtaText);

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

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

        private void setPeso(MtbAart mtbAart, BigDecimal pesoKg) {
            mViewDataBinding.pesoKg.setText(UtilityNumber.decimalToString(pesoKg) + " " + mtbAart.getUntMis());
        }
    }

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

    @Override
    public void onBindViewHolder(ChooseOrdsLavFromListAdapter.ViewHolder holder, int position) {
        final VersamentoMerceOrdineLavListModel mtbColrVersamentoMaterialeListModel = mDataset.get(position);

        mtbColrVersamentoMaterialeListModel.getSelected().resetOnPropertyChangedCallback();

        mtbColrVersamentoMaterialeListModel.getSelected().addOnPropertyChangedCallback(() -> {
            if(this.mOnItemChecked != null) this.mOnItemChecked.run(mtbColrVersamentoMaterialeListModel);
        });

        holder.bind(mtbColrVersamentoMaterialeListModel);
    }


    public ChooseOrdsLavFromListAdapter setOnItemClickListener(RunnableArgs<VersamentoMerceOrdineLavListModel> onItemChecked) {
        this.mOnItemChecked = onItemChecked;
        return this;
    }


}
