package it.integry.integrywmsnative.core.data_store.db.repository.datasource;

import android.os.Handler;

import java.util.List;
import java.util.concurrent.ExecutorService;

import javax.inject.Inject;

import it.integry.integrywmsnative.core.data_store.db.dao.OrdineDao;
import it.integry.integrywmsnative.core.data_store.db.entity.Ordine;
import it.integry.integrywmsnative.core.data_store.db.repository.OrdineRepository;
import it.integry.integrywmsnative.core.data_store.db.wrappers.OrdineWrapper;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public class OrdineDataSource extends BaseDataSource implements OrdineRepository {

    private final OrdineDao mOrdineDao;

    @Inject
    public OrdineDataSource(ExecutorService executorService,
                            Handler handler,
                            OrdineDao ordineDao) {
        super(executorService, handler);
        this.mOrdineDao = ordineDao;
    }

    public void selectAll(RunnableArgs<List<Ordine>> onSuccess, RunnableArgs<Exception> onError) {
        execute(mOrdineDao::getAll, onSuccess, onError);
    }

    public void selectAllTransmitted(RunnableArgs<List<Ordine>> onSuccess, RunnableArgs<Exception> onError) {
        execute(mOrdineDao::getAllTrasnmitted, onSuccess, onError);
    }

    public void selectAllOpenOrders(String codAlis, RunnableArgs<List<OrdineWrapper>> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mOrdineDao.getAllOpenOrdersByGriglia(codAlis), onSuccess, onError);
    }

    public void insert(Ordine ordine, RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mOrdineDao.insert(ordine),
                ordineId -> onSuccess.run(ordineId.intValue()),
                onError);
    }


    public void updateOrder(Ordine ordine, RunnableArgs<Ordine> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mOrdineDao.update(ordine),
                () -> onSuccess.run(ordine),
                onError);
    }

    public void delete(Ordine ordine, Runnable onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mOrdineDao.delete(ordine),
                onSuccess,
                onError);
    }
}
