package cz.senslog.analytics.module;

import cz.senslog.analytics.module.api.Module;
import cz.senslog.analytics.module.api.SimpleModule;
import cz.senslog.analytics.repository.ConfigurationRepository;
import cz.senslog.analytics.repository.SensLogDataRepository;
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 ConfigurationRepository configRepo;

    private final SensLogDataRepository sensLogRepo;

    public ScheduleDBLoaderModule(ConfigurationRepository configRepo, SensLogDataRepository dataRepo) {
        this.configRepo = configRepo;
        this.sensLogRepo = dataRepo;
    }

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

    }

    @Override
    public void run() throws Exception {
        // TODO calculate FROM & TO

//        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));
    }
}