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

import androidx.lifecycle.MutableLiveData;

import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.core.interfaces.viewmodel_listeners.ILoadingListener;
import it.integry.integrywmsnative.core.model.MtbTCol;
import it.integry.integrywmsnative.core.rest.consumers.MagazzinoRESTConsumer;

public class DialogInfoAggiuntiveLUViewModel {

    private final MutableLiveData<List<MtbTCol>> tipiCollo = new MutableLiveData<>();

    private Listener mListener;
    private final MagazzinoRESTConsumer mMagazzinoRESTConsumer;

    @Inject
    public DialogInfoAggiuntiveLUViewModel(MagazzinoRESTConsumer mMagazzinoRESTConsumer) {
        this.mMagazzinoRESTConsumer = mMagazzinoRESTConsumer;
    }


    public void init() {
        this.sendOnLoadingStarted();

        mMagazzinoRESTConsumer.getTipiCollo(tipiCollo -> {
            this.tipiCollo.postValue(tipiCollo);
            this.sendOnLoadingEnded();
        }, this::sendError);
    }

    public MutableLiveData<List<MtbTCol>> getTipiCollo() {
        return tipiCollo;
    }

    public void setListener(Listener listener) {
        this.mListener = listener;
    }


    private void sendOnLoadingStarted() {
        if (this.mListener != null) mListener.onLoadingStarted();
    }

    private void sendOnLoadingEnded() {
        if (this.mListener != null) mListener.onLoadingEnded();
    }

    private void sendError(Exception ex) {
        if (this.mListener != null) mListener.onError(ex);
    }

    public interface Listener extends ILoadingListener {

        void onError(Exception ex);
    }

}