package it.integry.integrywmsnative.view.bottom_sheet__lu_content;

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

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

import java.util.ArrayList;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.OnListGeneralChangedCallback;
import it.integry.integrywmsnative.core.expansion.OnSingleClickListener;
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.BottomSheetFragmentLuContentListItemBinding;

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

    private Context mContext;
    private ArrayList<MtbColr> mDataset;
    private OnItemClickListener mOnItemClickListener;


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


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

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

    public BottomSheetFragmentLUContentListAdapter(Context context, ObservableArrayList<MtbColr> mtbColrs) {
        super();
        this.mContext = context;
        this.mDataset = mtbColrs != null ? mtbColrs : new ArrayList<>();

        mtbColrs.addOnListChangedCallback(new OnListGeneralChangedCallback() {
            @Override
            public void onChanged(ObservableList sender) {
                notifyDataSetChanged();
            }
        });
    }

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

        return new BottomSheetFragmentLUContentListAdapter.ViewHolder(viewDataBinding);
    }

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

        if (position % 2 == 1)
            holder.mViewDataBinding.getRoot().setBackgroundColor(mContext.getResources().getColor(R.color.letturaFacilitataBG));

        holder.mViewDataBinding.getRoot().setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                if(mOnItemClickListener != null) mOnItemClickListener.onItemClick(holder.mViewDataBinding.getMtbColr());
            }
        });
    }

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


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

    interface OnItemClickListener {
        void onItemClick(MtbColr item);
    }

}
