package notifiers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import models.Comment;
import models.Module;
import models.Project;
import models.User;
import models.UserStatus;
import play.mvc.Mailer;

public class Emails extends Mailer {
	
	private static final String FROM = "Ceylon repo <ceylon-repo@inforealm.org>";

	public static void confirm(User user) {
		setSubject("[Ceylon repo] Please confirm your email address");
		addRecipient(user.email);
		setFrom(FROM);
		send(user);
	}

	public static void projectClaimNotification(Project project, User user) {
		setSubject("[Ceylon repo] New project claim for "+project.moduleName+" from "+user.userName);
		setFrom(FROM);
		for(String recipient : getNotificationRecipients(null, user)){
			setRecipient(recipient);
			send(project, user);
		}
	}

	public static void projectStatusNotification(Project project, User user) {
		String status = project.status.toString().toLowerCase();
		setSubject("[Ceylon repo] Your project claim "+project.moduleName+" has been "+status);
		setFrom(FROM);
		for(String recipient : getNotificationRecipients(project, user)){
			setRecipient(recipient);
			send(project, status);
		}
	}

	public static void commentNotification(Comment comment, User user) {
		Project project = comment.project;
		setSubject("[Ceylon repo] New comment from "+user.userName+" on project claim "+project.moduleName);
		setFrom(FROM);
		for(String recipient : getNotificationRecipients(project, user)){
			setRecipient(recipient);
			send(project, comment, user);
		}
	}

	public static void addAdminNotification(Module module, User admin, User user) {
		setSubject("[Ceylon repo] You are now admin on the "+module.name + " module");
		setFrom(FROM);
		addRecipient(admin.email);
		
		send(module, admin, user);
	}

	private static String[] getNotificationRecipients(Project project, User user) {
		// every admin except current user
		Set<User> users = new HashSet<User>(); 
		users.addAll(User.find("isAdmin = ? AND id != ? AND status = ?", true, user.id, UserStatus.REGISTERED).<User>fetch());
		// possibly the project claimer if not current user
		if(project != null && project.owner != user)
			users.add(project.owner);
		String[] emails = new String[users.size()];
		Iterator<User> iterator = users.iterator();
		for(int i=0;i<emails.length;i++)
			emails[i] = iterator.next().email;
		return emails;
	}
	
	private static void setRecipient(String recipient){
        HashMap<String, Object> map = infos.get();
        List<String> list = new ArrayList<String>();
        list.add(recipient);
        map.put("recipients", list);
	}
}

@@ -422,16 +422,80 @@
 m.org%3E%22;
+%0A%09private static final String SUBJECT_PREFIX = %22%5BCeylon Herd%5D %22;
 %0A%0A%09publi
@@ -542,31 +542,34 @@
 Subject(
-%22%5BCeylon repo%5D 
+SUBJECT_PREFIX + %22
 Please c
@@ -744,39 +744,42 @@
 %09setSubject(
-%22%5BCeylon repo%5D 
+SUBJECT_PREFIX + %22
 New project 
@@ -1124,31 +1124,34 @@
 Subject(
-%22%5BCeylon repo%5D 
+SUBJECT_PREFIX + %22
 Your pro
@@ -1471,31 +1471,34 @@
 Subject(
-%22%5BCeylon repo%5D 
+SUBJECT_PREFIX + %22
 New comm
@@ -1816,23 +1816,26 @@
 ect(
-%22%5BCeylon repo%5D 
+SUBJECT_PREFIX + %22
 You 

new identity