package codeine;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import codeine.configuration.Links;
import codeine.jsons.labels.LabelJsonProvider;
import codeine.jsons.mails.AlertsCollectionType;
import codeine.jsons.mails.CollectorNotificationJson;
import codeine.mail.Mail;
import codeine.model.Constants;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class AggregateMailPrepare {

	@Inject private Links links;
	@Inject	private LabelJsonProvider labelJsonProvider;
	
	
	public List<Mail> prepare(List<NotificationContent> notificationContent, AlertsCollectionType alertsCollectionType) {
		List<Mail> $ = Lists.newArrayList();
		for (NotificationContent item : notificationContent) {
			StringBuilder content = new StringBuilder();
			content.append("Hi,\nBelow are alerts from monitors in codeine for policy " + alertsCollectionType + ".\n");
			content.append("For more info: " + links.getWebServerLandingPage() + "\n");
			content.append("Enjoy!\n\n");
			content.append("========================================================================\n");
			
			for (CollectorNotificationJson notification : item.notifications()) {
				String nodeName = notification.node() == null ? "unknown" : notification.node().alias();
				String version = notification.version() == null ? Constants.NO_VERSION : labelJsonProvider.labelForVersion(notification.version(), notification.project_name());
				content.append("Project       : " + notification.project_name() + "\n");
				content.append("Node          : " + nodeName + "\n");
				content.append("Server        : " + notification.peer() + "\n");
				long time = notification.time();
				content.append("Time on node  : " + formatTime(time) + "\n");
				content.append("Version       : " + version + "\n");
				content.append("Monitor       : " + notification.collector_name() + "\n");
				content.append("Output\n" + notification.output() + "\n");
				content.append("========================================================================\n");
			}
			$.add(new Mail(Lists.newArrayList(item.user()), "Aggregated alerts from codeine for policy " + alertsCollectionType, content.toString()));
		}
		return $;
	}
	private String formatTime(long time) {
		return new SimpleDateFormat("HH:mm:ss dd/MM/yyyy").format(new Date(time));
	}
	
//	public static void main(String[] args) {
//		System.out.println(formatTime(System.currentTimeMillis()));
//	}
	
//	public static void main(String[] args) {
//		AggregateMailPrepare aggregateMailSender = new AggregateMailPrepare();
//		NotificationContent notificationContent2 = new NotificationContent("oshai");
//		NodeJson node = new NodeJson("itstl1043:12345");
//		notificationContent2.add(Lists.newArrayList(new CollectorNotificationJson("collector_name", "project_name", "outputdfgdfg\n\n\nsadkljfhsdfaklh", false, node)));
//		List<NotificationContent> notificationContent = Lists.newArrayList(notificationContent2);
//		List<Mail> mails = aggregateMailSender.prepare(notificationContent );
//		Send.mail(mails.get(0));
//	}
}

@@ -95,24 +95,59 @@
 il.List;%0D%0A%0D%0A
+import org.apache.log4j.Logger;%0D%0A%0D%0A
 import codei
@@ -502,16 +502,153 @@
 re %7B%0D%0A%0D%0A
+%09private static final Logger log = Logger%0D%0A%09%09%09.getLogger(AggregateMailPrepare.class);%0D%0A%09private static final int MAX_MAIL_SIZE = 10000;%0D%0A
 %09@Inject
@@ -2281,16 +2281,278 @@
 %0D%0A%09%09%09%7D%0D%0A
+%09%09%09String stringContent = %22%22;%0D%0A%09%09%09if (content.length() %3E MAX_MAIL_SIZE)%7B%0D%0A%09%09%09%09log.warn(%22mail was too big to user %22 + item.user());%0D%0A%09%09%09%09stringContent = content.substring(0, MAX_MAIL_SIZE) + %22...%22;%0D%0A%09%09%09%7D%0D%0A%09%09%09else %7B%0D%0A%09%09%09%09stringContent = content.toString();%0D%0A%09%09%09%7D%0D%0A
 %09%09%09$.add
@@ -2663,34 +2663,29 @@
 onType, 
-content.toString()
+stringContent
 ));%0D%0A%09%09%7D

cut mails with size that is bigger than 10,000 chars