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 InfoAggiuntiveLUDialogViewModel {

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

    private MtbTCol internalTipoPed;

    private Listener mListener;

    private final MagazzinoRESTConsumer mMagazzinoRESTConsumer;

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

    public void init() {
        new Thread(this::sendOnLoadingStarted).start();

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

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


    public void setInternalTipoPed(MtbTCol internalTipoPed) {
        this.internalTipoPed = internalTipoPed;
    }


    public MtbTCol getInternalTipoPed() {
        return internalTipoPed;
    }

    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 void setListener(Listener listener) {
        this.mListener = listener;
    }

    public interface Listener extends ILoadingListener {

        void onError(Exception ex);
    }

}
