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

import android.os.Handler;

import androidx.lifecycle.LiveData;

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

import javax.inject.Inject;

import it.integry.integrywmsnative.core.data_store.db.dao.MtbColtDao;
import it.integry.integrywmsnative.core.data_store.db.entity.SqlMtbColt;
import it.integry.integrywmsnative.core.data_store.db.repository.MtbColtRepository;
import it.integry.integrywmsnative.core.data_store.db.wrappers.DocInternoWrapper;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public class SqlMtbColtDataSource extends BaseDataSource implements MtbColtRepository {

    private final MtbColtDao mMtbColtDao;

    @Inject
    public SqlMtbColtDataSource(ExecutorService executorService,
                                Handler handler,
                                MtbColtDao mtbColtDao) {
        super(executorService, handler);
        this.mMtbColtDao = mtbColtDao;
    }

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

    public void insert(SqlMtbColt mtbColt, RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mMtbColtDao.insert(mtbColt),
                mtbColtId -> onSuccess.run(mtbColtId.intValue()),
                onError);
    }


    public void update(SqlMtbColt mtbColt, RunnableArgs<SqlMtbColt> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mMtbColtDao.update(mtbColt),
                () -> onSuccess.run(mtbColt),
                onError);
    }

    public void delete(SqlMtbColt mtbColt, Runnable onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> mMtbColtDao.delete(mtbColt),
                onSuccess,
                onError);
    }

    @Override
    public LiveData<List<DocInternoWrapper>> getDocuments(String codDtip, String codAnag, String codVdes, Date dataDoc, String numDoc) {
         return mMtbColtDao.getLocalDocumentsByCodDtip(codDtip, codAnag, codVdes, dataDoc, numDoc);
    }

    @Override
    public void getNextNumCollo(RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onError) {
        execute(mMtbColtDao::getNextNumCollo,
                onSuccess,
                onError);
    }


}
