package cz.senslog.analytics.module.api;

import cz.senslog.analytics.domain.CollectorType;
import cz.senslog.analytics.module.*;
import cz.senslog.analytics.repository.*;
import cz.senslog.analytics.server.HttpVertxServer;
import io.vertx.sqlclient.Pool;

import java.util.HashMap;
import java.util.Map;

public final class Module {

    private static final Map<Class<? extends SimpleModule>, SimpleModule> MODULES = new HashMap<>();

    private static final Map<CollectorType, CollectorModule> COLLECTORS  = new HashMap<>();

    public static SimpleModule[] createModules(Pool sensLogDB) {
        ConfigurationRepository configRepo = new AnalyticsConfigRepository(sensLogDB);
        SensLogDataRepository dataRepo = new SensLogDataRepository(sensLogDB);
        AnalyticsDataRepository statisticsRep = new AnalyticsDataRepository(sensLogDB);

        createModule(new ObservationReceiverModule(configRepo));
        createModule(new DoubleStatisticsModule(configRepo, statisticsRep));
        createModule(new MoldAnalysisModule(configRepo, statisticsRep));

        createModule(new NotificationModule(configRepo));
        createModule(new ScheduleDBLoaderModule(configRepo, dataRepo));

        return MODULES.values().toArray(new SimpleModule[0]);
    }

    private static void createModule(SimpleModule module) {
        if (module instanceof CollectorModule c) {
            if (!COLLECTORS.containsKey(c.type())) {
                COLLECTORS.put(c.type(), c);
            }
        }
        if (!MODULES.containsKey(module.getClass())) {
            MODULES.put(module.getClass(), module);
        }
    }

    public static SimpleModule of(Class<? extends SimpleModule> aClass) {
        return MODULES.get(aClass);
    }

    public static ModuleDescriptor collectorOf(CollectorType type) {
        if (type == null) { return null; }
        return COLLECTORS.get(type);
    }
}