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

import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.core.data_store.db.dao.MtbColrDao;
import it.integry.integrywmsnative.core.data_store.db.entity.SqlMtbColr;
import it.integry.integrywmsnative.core.data_store.db.entity.SqlMtbColt;
import it.integry.integrywmsnative.core.data_store.db.repository.MtbColrRepository;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public class SqlMtbColrDataSource extends Repository implements MtbColrRepository {

    private MtbColrDao mMtbColrDao;

    @Inject
    public SqlMtbColrDataSource(MtbColrDao mtbColrDao) {
        this.mMtbColrDao = mtbColrDao;
    }

    public void selectAll(RunnableArgs<List<SqlMtbColr>> onSuccess, RunnableArgs<Exception> onError) {
        Runnable query = () -> {
            try {
                onSuccess.run(mMtbColrDao.getAll());
            } catch (Exception e) {
                onError.run(e);
            }
        };
        execute(query);
    }

    public void insert(SqlMtbColr mtbColr, RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> {
            try {
                Long id = mMtbColrDao.insert(mtbColr);
                onSuccess.run(id.intValue());
            } catch (Exception e) {
                onError.run(e);
            }
        });
    }


    public void update(SqlMtbColr mtbColr, RunnableArgs<SqlMtbColr> onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> {
            try {
                mMtbColrDao.update(mtbColr);
                onSuccess.run(mtbColr);
            } catch (Exception e) {
                onError.run(e);
            }
        });
    }

    public void delete(SqlMtbColr mtbColr, Runnable onSuccess, RunnableArgs<Exception> onError) {
        execute(() -> {
            try {
                mMtbColrDao.delete(mtbColr);
                onSuccess.run();
            } catch (Exception e) {
                onError.run(e);
            }
        });
    }

    public void getDocumentRows(SqlMtbColt document, RunnableArgs<List<SqlMtbColr>> onSuccess, RunnableArgs<Exception> onError) {
        Runnable query = () -> {
            try {
                onSuccess.run(mMtbColrDao.getDocumentRows(document.getId()));
            } catch (Exception e) {
                onError.run(e);
            }
        };
        execute(query);
    }
}
