package it.integry.integrywmsnative.view.dialogs.choose_arts_from_lista_arts;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.annimon.stream.Stream;

import java.util.HashMap;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.CheckableMtbColr;
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.DialogChooseArtsFromListaArtsItemModelBinding;

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

    protected Context mContext;

    private List<CheckableMtbColr> mDataset;
    private HashMap<CheckableMtbColr, Integer> mDatasetPositions = new HashMap<>();


    public DialogChooseArtsFromListaArtsAdapter(Context context, List<DialogChooseArtsFromListaArtsItemModel> myDataset) {
        mContext = context;

        if(myDataset != null) {
            mDataset = Stream.of(myDataset)
                    .withoutNulls()
                    .map(CheckableMtbColr::new).toList();

            for(int i = 0; i < mDataset.size(); i++) {
                final CheckableMtbColr itemModel = mDataset.get(i);

                mDatasetPositions.put(itemModel, i);

                itemModel.getHidden().addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
                    @Override
                    public void onPropertyChanged(Observable sender, int propertyId) {
                        onItemHidden(itemModel);
                    }
                });
            }
        }
    }

    private void onItemHidden(CheckableMtbColr itemModel) {
        if(itemModel.isHidden()) {
            mDataset.remove(itemModel);
        } else {
            mDataset.add(mDatasetPositions.get(itemModel) , itemModel);
        }
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private DialogChooseArtsFromListaArtsItemModelBinding mViewDataBinding;


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

        public void bind(CheckableMtbColr checkableMtbColr) {
            mViewDataBinding.setCheckableMtbColr(checkableMtbColr);

            MtbColr mtbColr = checkableMtbColr.getItem().getMtbColr();

            //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 DialogChooseArtsFromListaArtsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        DialogChooseArtsFromListaArtsItemModelBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.dialog_choose_arts_from_lista_arts__item_model, parent, false);

        return new ViewHolder(viewDataBinding);
    }

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

    }

    @Override
    public void onViewRecycled(DialogChooseArtsFromListaArtsAdapter.ViewHolder holder) {
        super.onViewRecycled(holder);
    }

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

    public List<MtbColr> getSelectedItems() {
        return Stream.of(mDataset)
                .filter(y -> y.getChecked().get())
                .map(x -> x.getItem().getMtbColr()).toList();
    }

}
