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.GrigliaDao;
import it.integry.integrywmsnative.core.data_store.db.entity.Griglia;
import it.integry.integrywmsnative.core.data_store.db.repository.GrigliaRepository;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public class GrigliaDataSource extends BaseDataSource implements GrigliaRepository {

    private final GrigliaDao mGrigliaDao;

    @Inject
    public GrigliaDataSource(ExecutorService executorService,
                             Handler handler,
                             GrigliaDao grigliaDao) {
        super(executorService, handler);
        this.mGrigliaDao = grigliaDao;
    }

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

    public void findByCodAlis(String codAlis, RunnableArgs<Griglia> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mGrigliaDao.findByCodAlis(codAlis), onSuccess, onError);
    }


    public void saveGriglia(Griglia griglia, RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onFail) {
        if (griglia.getGrigliaId() <= 0) {
            execute(() -> (int) mGrigliaDao.insert(griglia), grigliaId -> {
                griglia.setGrigliaId(grigliaId);
                onSuccess.run(grigliaId);
            }, onFail);
        } else {
            execute(() -> mGrigliaDao.update(griglia), () -> onSuccess.run(griglia.getGrigliaId()), onFail);
        }

    }

    public void findGrigliaById(int idGriglia, RunnableArgs<Griglia> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mGrigliaDao.findGrigliaById(idGriglia), onSuccess, onError);
    }
}
