package edu.one.core.infra;

import java.io.IOException;
import java.util.List;

import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.platform.Container;

import edu.one.core.infra.http.Renders;
import edu.one.core.infra.security.resources.UserInfos;

public class NotificationHelper {

	private static final String TIMELINE_ADDRESS = "wse.timeline";
	private static final String EMAIL_ADDRESS = "wse.email";
	private final EventBus eb;
	private final Renders render;

	public NotificationHelper(EventBus eb, Container container) {
		this.eb = eb;
		this.render = new Renders(container);
	}

	public void notifyTimeline(HttpServerRequest request, UserInfos sender,
			List<String> recipients, String resource, String template, JsonObject params)
					throws IOException {
		JsonObject event = new JsonObject()
		.putString("action", "add")
		.putString("resource", resource)
		.putString("sender", sender.getUserId())
		.putString("message", render.processTemplate(request, template, params))
		.putArray("recipients", new JsonArray(recipients.toArray()));
		eb.send(TIMELINE_ADDRESS, event);
	}

	public void deleteFromTimeline(String resource) {
		JsonObject json = new JsonObject()
		.putString("action", "delete")
		.putString("resource", resource);
		eb.send(TIMELINE_ADDRESS, json);
	}

	public void sendEmail(HttpServerRequest request, String to, String from, String cc, String bcc,
			String subject, String templateBody, JsonObject templateParams,
			boolean translateSubject, Handler<Message<JsonObject>> handler) throws IOException {
		JsonObject json = new JsonObject()
		.putString("to", to)
		.putString("from", from)
		.putString("cc", cc)
		.putString("bcc", bcc);
		if (translateSubject) {
			json.putString("subject", I18n.getInstance().translate(
					subject, request.headers().get("Accept-Language")));
		} else {
			json.putString("subject", subject);
		}
		String body = render.processTemplate(request, templateBody, templateParams);
		json.putString("body", new String(body.getBytes("UTF-8"), "ISO-8859-1"));
		eb.send(EMAIL_ADDRESS, json, handler);
	}

}

@@ -895,16 +895,23 @@
 der,%0A%09%09%09
+%09%09%09%09   
 List%3CStr
@@ -980,16 +980,299 @@
 params)%0A
+%09%09%09throws IOException %7B%0A%09%09notifyTimeline(request, sender, recipients, resource, null, template, params);%0A%09%7D%0A%0A%09public void notifyTimeline(HttpServerRequest request, UserInfos sender,%0A%09%09%09List%3CString%3E recipients, String resource, String subResource, String template, JsonObject params)%0A
 %09%09%09%09%09thr
@@ -1570,24 +1570,139 @@
 oArray()));%0A
+%09%09if (subResource != null && !subResource.trim().isEmpty()) %7B%0A%09%09%09event.putString(%22sub-resource%22, subResource);%0A%09%09%7D%0A
 %09%09eb.send(TI
@@ -1850,16 +1850,16 @@
 elete%22)%0A
-
 %09%09.putSt
@@ -1921,24 +1921,256 @@
  json);%0A%09%7D%0A%0A
+%09public void deleteSubResourceFromTimeline(String subResource) %7B%0A%09%09JsonObject json = new JsonObject()%0A%09%09%09%09.putString(%22action%22, %22deleteSubResource%22)%0A%09%09%09%09.putString(%22sub-resource%22, subResource);%0A%09%09eb.send(TIMELINE_ADDRESS, json);%0A%09%7D%0A%0A
 %09public void

support sub-resource in notification helper