package ca.carleton.gcrc.couch.onUpload.mail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ca.carleton.gcrc.couch.client.CouchDesignDocument;
import ca.carleton.gcrc.couch.user.UserDesignDocument;
import ca.carleton.gcrc.couch.user.UserDocument;

public class MockUserDesignDocument implements UserDesignDocument {

	private String emailAddress = null;
	private String displayName = null;

	@Override
	public CouchDesignDocument getSupportingDesignDocument() {
		return null;
	}

	@Override
	public Collection<UserDocument> getUsersWithRole(String role) throws Exception {
		List<String> roles = new ArrayList<String>(1);
		roles.add(role);
		return getUsersWithRoles(roles);
	}

	@Override
	public Collection<UserDocument> getUsersWithRoles(List<String> roles) throws Exception {
		JSONObject obj = new JSONObject();
		obj.put("_id", "org.couchdb.user:test");
		obj.put("name", "test");
		obj.put("type", "user");
		
		if( null != displayName ){
			obj.put("display", displayName);
		}
		if( null != emailAddress ){
			JSONArray ary = new JSONArray();
			ary.put(emailAddress);
			obj.put("nunaliit_emails", ary);
		}
		if( null != roles ){
			JSONArray ary = new JSONArray();
			for(String role : roles){
				ary.put(role);
			}
			obj.put("roles", ary);
		}
		
		UserDocument user = new UserDocument(obj);
		List<UserDocument> users = new ArrayList<UserDocument>(1);
		users.add(user);
		return users;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
}

@@ -569,24 +569,587 @@
 n null;%0A%09%7D%0A%0A
+%09@Override%0A%09public Collection%3CUserDocument%3E getAllUsers() throws Exception %7B%0A%09%09JSONObject obj = new JSONObject();%0A%09%09obj.put(%22_id%22, %22org.couchdb.user:test%22);%0A%09%09obj.put(%22name%22, %22test%22);%0A%09%09obj.put(%22type%22, %22user%22);%0A%09%09%0A%09%09if( null != displayName )%7B%0A%09%09%09obj.put(%22display%22, displayName);%0A%09%09%7D%0A%09%09if( null != emailAddress )%7B%0A%09%09%09JSONArray ary = new JSONArray();%0A%09%09%09ary.put(emailAddress);%0A%09%09%09obj.put(%22nunaliit_emails%22, ary);%0A%09%09%7D%0A%09%09%0A%09%09UserDocument user = new UserDocument(obj);%0A%09%09List%3CUserDocument%3E users = new ArrayList%3CUserDocument%3E(1);%0A%09%09users.add(user);%0A%09%09return users;%0A%09%7D%0A%0A
 %09@Override%0A%09

Fix build issue