package io.spacedog.jobs;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.PublishRequest;
import com.google.common.base.Throwables;

import io.spacedog.client.http.SpaceEnv;
import io.spacedog.utils.Optional7;
import io.spacedog.utils.Utils;

public class Internals {

	private final AmazonSNS sns;

	public void notify(String title, Throwable t) {
		notify(title, Throwables.getStackTraceAsString(t));
	}

	public void notify(String title, String message) {

		try {
			Optional7<String> topic = SpaceEnv.env().superdogNotificationTopic();
			if (!topic.isPresent())
				Utils.warn("unable to send platform alert [%s][%s]: no SNS topic in env", //
						title, message);
			else
				sns.publish(new PublishRequest()//
						.withTopicArn(topic.get())//
						.withSubject(title)//
						.withMessage(message));

		} catch (Throwable ignore) {
			ignore.printStackTrace();
		}
	}

	//
	// Singleton
	//

	private static Internals singleton = new Internals();

	public static Internals get() {
		return singleton;
	}

	private Internals() {
		sns = AmazonSNSClientBuilder.standard()//
				.withRegion(Regions.EU_WEST_1)//
				.build();
	}
}

@@ -698,116 +698,141 @@
 t())
-%0A%09%09%09%09Utils.warn(%22unable to send platform alert %5B%25s%5D%5B%25s%5D: no SNS topic in env%22, //%0A%09%09%09%09%09%09title, message
+ %7B%0A%09%09%09%09Utils.warn(%22%25s: %25s%22, title, message);%0A%09%09%09%09Utils.warn(%22unable to notify superdogs: no superdog topic in env properties%22
 );%0A%09%09%09
+%7D 
 else
@@ -1264,17 +1264,48 @@
 ons.
-EU_WEST_1
+fromName(SpaceEnv.env().backendRegion())
 )//%0A
@@ -1321,10 +1321,11 @@
 d();%0A%09%7D%0A
+%0A
 %7D%0A

Improve superdog notification to use region from env properties