package org.jvalue.ceps.main;

import com.google.inject.Guice;
import com.google.inject.Injector;

import org.jvalue.ceps.adapter.AdapterModule;
import org.jvalue.ceps.auth.AuthModule;
import org.jvalue.ceps.data.DataManager;
import org.jvalue.ceps.data.DataModule;
import org.jvalue.ceps.db.DbModule;
import org.jvalue.ceps.esper.EsperModule;
import org.jvalue.ceps.event.EventGarbageCollector;
import org.jvalue.ceps.event.EventModule;
import org.jvalue.ceps.notifications.NotificationManager;
import org.jvalue.ceps.notifications.NotificationsModule;
import org.jvalue.ceps.notifications.garbage.ClientGarbageCollectorManager;
import org.jvalue.ceps.ods.OdsModule;
import org.jvalue.ceps.rest.DataApi;
import org.jvalue.ceps.rest.EplAdapterApi;
import org.jvalue.ceps.rest.EventApi;
import org.jvalue.ceps.rest.RegistrationApi;
import org.jvalue.ceps.rest.RestModule;
import org.jvalue.ceps.rest.SourcesApi;
import org.jvalue.ceps.rest.VersionApi;
import org.jvalue.common.auth.AuthBinder;
import org.jvalue.common.rest.DbExceptionMapper;
import org.jvalue.common.rest.JsonExceptionMapper;
import org.jvalue.common.rest.UnauthorizedExceptionMapper;

import javax.ws.rs.core.Context;

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;


public final class CepsApplication extends Application<CepsConfig> {

	private String cepsServerName = "http://faui2o2f.cs.fau.de:8080/cep-service";
	private String odsServerName = "http://faui2o2f.cs.fau.de:8080/open-data-service";


	public static void main(String[] args) throws Exception {
		new CepsApplication().run(args);
	}


	@Override
	public String getName() {
		return "CEP Service";
	}


	@Override
	public void initialize(Bootstrap<CepsConfig> configuration) {
		// nothing to do
	}


	@Override
	@Context
	public void run(CepsConfig configuration, Environment environment) {
		Injector injector = Guice.createInjector(
				new ConfigModule(configuration),
				new DbModule(configuration.getCouchDb()),
				new NotificationsModule(),
				new DataModule(),
				new EventModule(),
				new AdapterModule(),
				new EsperModule(),
				new OdsModule(),
				new RestModule(),
				new AuthModule(configuration.getAdmins()));

		environment.lifecycle().manage(injector.getInstance(DataManager.class));
		environment.lifecycle().manage(injector.getInstance(NotificationManager.class));
		environment.lifecycle().manage(injector.getInstance(EventGarbageCollector.class));
		environment.lifecycle().manage(injector.getInstance(ClientGarbageCollectorManager.class));

		environment.jersey().getResourceConfig().register(injector.getInstance(AuthBinder.class));
		environment.jersey().register(injector.getInstance(DataApi.class));
		environment.jersey().register(injector.getInstance(EventApi.class));
		environment.jersey().register(injector.getInstance(RegistrationApi.class));
		environment.jersey().register(injector.getInstance(SourcesApi.class));
		environment.jersey().register(injector.getInstance(EplAdapterApi.class));
		environment.jersey().register(injector.getInstance(VersionApi.class));
		environment.jersey().register(new DbExceptionMapper());
		environment.jersey().register(new JsonExceptionMapper());
		environment.jersey().register(new UnauthorizedExceptionMapper());
	}



	/*
	private void startSourceMonitoring() {
		try {
			DataSource source = new DataSource(
					"de-pegelonline",
					new URL(odsServerName),
					"ods/de/pegelonline/stations/$class");
			DataManager manager = DataManager.getInstance();

			if (manager.isBeingMonitored(source)) return;

			manager.startMonitoring(
					source,
					cepsServerName + OdsRestHook.URL_NOTIFY_SOURCE_CHANGED,
					OdsRestHook.PARAM_SOURCE);

		} catch (RestException | MalformedURLException e) {
			throw new IllegalStateException(e);
		}
	}
	*/


	/*
	private static void startGarbageCollection() {
	TODO
		// try removing old clients every 3 days (exact removal interval depends on client type)
		NotificationManager notificationManager = NotificationManager.getInstance();
		org.jvalue.ceps.notifications.garbage.GarbageCollector gcmCollector = new GcmGarbageCollector(new GcmUtils("/googleApi.key"));
		GarbageCollectorMapper mapper = new GarbageCollectorMapper(gcmCollector);
		new GarbageCollectorManager(notificationManager, mapper, 259200000).startCollection();
	}
	*/

}

@@ -3292,1069 +3292,6 @@
 %09%7D%0A%0A
-%0A%0A%09/*%0A%09private void startSourceMonitoring() %7B%0A%09%09try %7B%0A%09%09%09DataSource source = new DataSource(%0A%09%09%09%09%09%22de-pegelonline%22,%0A%09%09%09%09%09new URL(odsServerName),%0A%09%09%09%09%09%22ods/de/pegelonline/stations/$class%22);%0A%09%09%09DataManager manager = DataManager.getInstance();%0A%0A%09%09%09if (manager.isBeingMonitored(source)) return;%0A%0A%09%09%09manager.startMonitoring(%0A%09%09%09%09%09source,%0A%09%09%09%09%09cepsServerName + OdsRestHook.URL_NOTIFY_SOURCE_CHANGED,%0A%09%09%09%09%09OdsRestHook.PARAM_SOURCE);%0A%0A%09%09%7D catch (RestException %7C MalformedURLException e) %7B%0A%09%09%09throw new IllegalStateException(e);%0A%09%09%7D%0A%09%7D%0A%09*/%0A%0A%0A%09/*%0A%09private static void startGarbageCollection() %7B%0A%09TODO%0A%09%09// try removing old clients every 3 days (exact removal interval depends on client type)%0A%09%09NotificationManager notificationManager = NotificationManager.getInstance();%0A%09%09org.jvalue.ceps.notifications.garbage.GarbageCollector gcmCollector = new GcmGarbageCollector(new GcmUtils(%22/googleApi.key%22));%0A%09%09GarbageCollectorMapper mapper = new GarbageCollectorMapper(gcmCollector);%0A%09%09new GarbageCollectorManager(notificationManager, mapper, 259200000).startCollection();%0A%09%7D%0A%09*/%0A%0A
 %7D%0A

Remove commented code