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

import java.util.Date;
import java.util.List;

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 Repository implements MtbColtRepository {

    private MtbColtDao mMtbColtDao;

    @Inject
    public SqlMtbColtDataSource(MtbColtDao mtbColtDao) {
        this.mMtbColtDao = mtbColtDao;
    }

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

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


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

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

    @Override
    public void getLocalDocumentsByCodDtip(String codDtip, String codAnag, String codVdes, Date dataDoc, String numDoc, RunnableArgs<List<DocInternoWrapper>> onSuccess, RunnableArgs<Exception> onError) {
        Runnable query = () -> {
            try {
                onSuccess.run(mMtbColtDao.getLocalDocumentsByCodDtip(codDtip,codAnag,codVdes,dataDoc,numDoc));
            } catch (Exception e) {
                onError.run(e);
            }
        };
        execute(query);
    }

    @Override
    public void getNextNumCollo(RunnableArgs<Integer> onSuccess, RunnableArgs<Exception> onError) {
        Runnable query = () -> {
            try {
                onSuccess.run(mMtbColtDao.getNextNumCollo());
            } catch (Exception e) {
                onError.run(e);
            }
        };
        execute(query);
    }


}
