package cz.senslog.analytics.module;

import cz.senslog.analytics.module.api.SimpleModule;
import cz.senslog.analytics.repository.AnalyticsRepository;
import io.vertx.core.Promise;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

public class ScheduleDBLoaderModule extends SimpleModule {

    private static final Logger logger = LogManager.getLogger(ScheduleDBLoaderModule.class);

    private final AnalyticsRepository repo;

    public ScheduleDBLoaderModule(AnalyticsRepository repo) {
        this.repo = repo;
    }

    @Override
    public void configure(Promise<Void> completePromise) {
        // TODO load tasks from DB and creat timers
        List<Object> tasks = List.of(
                new Object()
        );
        completePromise.complete();

    }

    @Override
    public void run(Promise<Void> completePromise) {
        // TODO calculate FROM & TO
        completePromise.complete();

//        final String busId = Module.of(ObservationReceiverModule.class).id();
//        sensLogRepo.cursorAllObservations(1000, o -> eventBus().publish(busId, o), logger::catching);


//        vertx.setPeriodic(1000, 216000, l -> sensLogRepo.loadObservations(null, null)
//                .onSuccess(data -> data.forEach(o -> eventBus().publish(busId, o)))
//                .onFailure(logger::catching)
//        );


//        vertx.setPeriodic(1000, (l) -> sensLogRepo.loadObservations(null, null).onSuccess(data -> data
//                .forEach(o -> eventBus().publish(busId, o))
//        ).onFailure(logger::catching));
    }
}