package it.integry.integrywmsnative.core.rest.consumers;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.CommonModelConsts;
import it.integry.integrywmsnative.core.model.MtbDepo;

@Singleton
public class DepositoRESTConsumer extends _BaseRESTConsumer {

    private final EntityRESTConsumer entityRESTConsumer;
    private final SystemRESTConsumer systemRESTConsumer;

    public DepositoRESTConsumer(EntityRESTConsumer entityRESTConsumer, SystemRESTConsumer systemRESTConsumer) {
        this.entityRESTConsumer = entityRESTConsumer;
        this.systemRESTConsumer = systemRESTConsumer;
    }

    public void getAll(RunnableArgs<List<MtbDepo>> onComplete, RunnableArgs<Exception> onFailed) {
        Type typeOfObjectsList = new TypeToken<ArrayList<MtbDepo>>() {
        }.getType();
        this.systemRESTConsumer.processSql("SELECT * FROM mtb_depo", typeOfObjectsList, onComplete, onFailed);
    }

    public void getDepoByCodMdep(String codMdep, RunnableArgs<MtbDepo> onComplete, RunnableArgs<Exception> onFailed) {

        MtbDepo mtbDepo = new MtbDepo();
        mtbDepo.setCodMdep(codMdep);
        mtbDepo.setOperation(CommonModelConsts.OPERATION.SELECT);
        mtbDepo.setOnlyPkMaster(false);

        this.entityRESTConsumer.selectEntity(mtbDepo, new ISimpleOperationCallback<>() {
            @Override
            public void onSuccess(List<MtbDepo> value) {
                if (value != null && value.size() > 0) {
                    onComplete.run(value.get(0));
                }
            }

            @Override
            public void onFailed(Exception ex) {
                onFailed.run(ex);
            }
        }, MtbDepo.class);
    }

}
