package it.integry.integrywmsnative.gest.spedizione.dialogs.row_info.prod_fabbisogno_linee_prod;

import androidx.lifecycle.MutableLiveData;

import java.util.Date;
import java.util.List;

import it.integry.integrywmsnative.core.interfaces.viewmodel_listeners.ILoadingListener;
import it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.dto.ProdFabbisognoLineeProdDTO;
import it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.rest.ProdFabbisognoLineeProdRESTConsumer;

public class DialogRowInfoProdFabbisognoLineeProdViewModel {

    private final ProdFabbisognoLineeProdRESTConsumer mProdFabbisognoLineeProdRESTConsumer;

    private final MutableLiveData<List<ProdFabbisognoLineeProdDTO>> mFabbisognoList = new MutableLiveData<>();

    private Listener mListener;


    public DialogRowInfoProdFabbisognoLineeProdViewModel(ProdFabbisognoLineeProdRESTConsumer mProdFabbisognoLineeProdRESTConsumer) {
        this.mProdFabbisognoLineeProdRESTConsumer = mProdFabbisognoLineeProdRESTConsumer;
    }


    public void init(String codMart, String codMdep, Date startDate, Date endDate) {
        this.sendOnLoadingStarted();

        this.mProdFabbisognoLineeProdRESTConsumer.loadFabbisogno(codMart, startDate, endDate, codMdep, fabbisognoList -> {
            this.mFabbisognoList.postValue(fabbisognoList);
            this.sendOnLoadingEnded();
        }, this::sendError);
    }


    public MutableLiveData<List<ProdFabbisognoLineeProdDTO>> getFabbisognoList() {
        return mFabbisognoList;
    }

    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);

    }
}
