package it.integry.integrywmsnative.gest.contab_doc_interni.dialog.adapter;

import android.content.Context;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.annimon.stream.Stream;

import java.util.List;

import it.integry.integrywmsnative.gest.contab_doc_interni.dto.FornitoreDTO;

public class DialogSelectDocInfo_FornitoreAdapter extends ArrayAdapter<DialogSelectDocInfo_FornitoreAdapter.ListModel> {

    public DialogSelectDocInfo_FornitoreAdapter(@NonNull Context context, int resource, MutableLiveData<List<FornitoreDTO>> inputData) {
        super(context, resource);

        if (inputData == null) return;

        inputData.observeForever(data -> {
            List<ListModel> mDataset = Stream.of(data)
                    .map(x -> new ListModel()
                            .setOriginalModel(x))
                    .toList();

            clear();
            addAll(mDataset);
        });
    }

    public static class ListModel {

        private FornitoreDTO originalModel;

        public FornitoreDTO getOriginalModel() {
            return originalModel;
        }

        public ListModel setOriginalModel(FornitoreDTO originalModel) {
            this.originalModel = originalModel;
            return this;
        }

        @Override
        public String toString() {
            return this.getOriginalModel().getCodAnag() +
                    (this.getOriginalModel().getCodVdes() != null ? " - " + this.getOriginalModel().getCodVdes() : "") +
                    " ( " + this.getOriginalModel().getDescrizione() + " )";
        }
    }

}
