package it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.dialog_ask_mag_prossimita;

import androidx.lifecycle.MutableLiveData;

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

import it.integry.integrywmsnative.core.di.BindableString;
import it.integry.integrywmsnative.core.interfaces.viewmodel_listeners.ILoadingListener;
import it.integry.integrywmsnative.core.model.JtbFasi;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.utility.UtilityDate;

public class DialogAskMagazzinoProssimitaViewModel {

    public BindableString startDateBindable = new BindableString();
    public BindableString endDateBindable = new BindableString();

    public Date startDate = new Date();
    public Date endDate = new Date();

    private Listener mListener;

    private final MutableLiveData<List<JtbFasi>> mJtbFasi = new MutableLiveData<>();

    private final SystemRESTConsumer mSystemRESTConsumer;

    public DialogAskMagazzinoProssimitaViewModel(SystemRESTConsumer systemRESTConsumer) {
        this.mSystemRESTConsumer = systemRESTConsumer;
    }

    public void init(String codMdep) {

        Calendar c = UtilityDate.getCalendarInstance();
        c.set(Calendar.DAY_OF_WEEK, c.getActualMinimum(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DAY_OF_MONTH, -7);
        startDate = c.getTime();
        startDateBindable.set(UtilityDate.formatDate(startDate, UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN));

        c.add(Calendar.DAY_OF_MONTH, 7);
        endDate = c.getTime();
        endDateBindable.set(UtilityDate.formatDate(endDate, UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN));

//        this.sendOnLoadingStarted();
//
//        String sql = "SELECT DISTINCT jrl_fase_posizioni.posizione as cod_jfas, " +
//                "                descrizione, " +
//                "                jrl_fase_posizioni.cod_mdep " +
//                "FROM jrl_fase_posizioni " +
//                "         INNER JOIN mtb_depo_posizioni mdp ON jrl_fase_posizioni.cod_mdep = mdp.cod_mdep AND " +
//                "                                              jrl_fase_posizioni.posizione = mdp.posizione " +
//                "WHERE cod_jfas IN (SELECT cod_jfas FROM jtb_fasi WHERE um_prod IS NOT NULL) " +
//                "  AND jrl_fase_posizioni.cod_mdep = " + UtilityDB.valueToString(codMdep) + " " +
//                "ORDER BY jrl_fase_posizioni.posizione";
//
//
//        Type typeOfObjectsList = new TypeToken<ArrayList<JtbFasi>>() {}.getType();
//        this.mSystemRESTConsumer.<List<JtbFasi>>processSql(sql, typeOfObjectsList, jtbFasi -> {
//            this.mJtbFasi.postValue(jtbFasi);
//
//            this.sendOnLoadingEnded();
//        }, this::sendError);
    }



//    public MutableLiveData<List<JtbFasi>> getJtbFasi() {
//        return mJtbFasi;
//    }


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

    }

}
