// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package dk.vajhoej.forum.test;

import java.util.Map;
import java.util.ServiceLoader;

import javax.jcr.PathNotFoundException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.RepositoryFactory;

import dk.vajhoej.forum.ForumException;
import dk.vajhoej.forum.ForumManager;
import dk.vajhoej.forum.ModelMapper;
import dk.vajhoej.forum.model.Comment;
import dk.vajhoej.forum.model.Forum;
import dk.vajhoej.forum.model.Post;
import dk.vajhoej.forum.model.Topic;

public class Test {
	private static Repository getRepository(Map<String, String> parameters) throws RepositoryException {
		for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
			Repository repo = factory.getRepository(parameters);
			if(repo != null) {
				return repo;
			}
		}
		return null;
	}
	private static void printComment(String indent, Comment comment) {
		System.out.println(indent + "Comment:");
		System.out.println(indent + "Author = " + comment.getAuthor());
		System.out.println(indent + "Time = " + comment.getTime());
		System.out.println(indent + "Number = " + comment.getNumber());
		System.out.println(indent + "Text = " + comment.getText());
		for(Comment v : comment.getVersions()) {
			System.out.println(indent + "Version = (" + v.getTime() + "," + v.getText() + ")");
		}
		for(Comment c : comment.getComments()) {
			printComment(indent + "  ", c);
		}
	}
	private static void printPost(String indent, Post post) {
		System.out.println(indent + "Post:");
		System.out.println(indent + "Author = " + post.getAuthor());
		System.out.println(indent + "Time = " + post.getTime());
		System.out.println(indent + "Title = " + post.getTitle());
		System.out.println(indent + "Body = " + post.getBody());
		for(Post v : post.getVersions()) {
			System.out.println(indent + "Version = (" + v.getTime() + "," + v.getBody() + ")");
		}
		for(Comment c : post.getComments()) {
			printComment(indent + "  ", c);
		}
	}
	private static void printTopic(String indent, Topic topic) {
		System.out.println(indent + "Topic:");
		System.out.println(indent + "Name = " + topic.getName());
		System.out.println(indent + "Description = " + topic.getDescription());
		for(Post p : topic.getPosts()) {
			printPost(indent + "  ", p);
		}
	}
	private static void printForum(Forum forum) {
		for(Topic t : forum.getTopics()) {
			printTopic("", t);
		}
		
	}
	private static void testForumLoad(ForumManager fmgr) throws RepositoryException, ForumException {
		ModelMapper mm = new ModelMapper(fmgr);
		printForum(mm.loadForum());
	}
	private static void testTopicLoad(ForumManager fmgr, String path) throws PathNotFoundException, RepositoryException, ForumException {
		System.out.println("Load: " + path);
		ModelMapper mm = new ModelMapper(fmgr);
		printTopic("", mm.loadTopic(path, true));
	}
	private static void testPostLoad(ForumManager fmgr, String path) throws PathNotFoundException, RepositoryException, ForumException {
		System.out.println("Load: " + path);
		ModelMapper mm = new ModelMapper(fmgr);
		printPost("", mm.loadPost(path, true));
	}
	private static void testCommentLoad(ForumManager fmgr, String path) throws PathNotFoundException, RepositoryException, ForumException {
		System.out.println("Load: " + path);
		ModelMapper mm = new ModelMapper(fmgr);
		printComment("", mm.loadComment(path, true));
	}
	private static void testPostAuthorSearch(ForumManager fmgr, String author) throws RepositoryException, ForumException {
		System.out.println("Post author search for " + author + ":");
		ModelMapper mm = new ModelMapper(fmgr);
		for(Post p : mm.findPostByAuthor(author, true)) {
			printPost("", p);
		}
	}
	private static void testPostContentSearch(ForumManager fmgr, String content) throws RepositoryException, ForumException {
		System.out.println("Post content search for " + content + ":");
		ModelMapper mm = new ModelMapper(fmgr);
		for(Post p : mm.findPostByContent(content, true)) {
			printPost("", p);
		}
	}
	private static void testCommentAuthorSearch(ForumManager fmgr, String author) throws RepositoryException, ForumException {
		System.out.println("Comment author search for " + author + ":");
		ModelMapper mm = new ModelMapper(fmgr);
		for(Comment c : mm.findCommentByAuthor(author, true)) {
			printComment("", c);
		}
	}
	private static void testCommentContentSearch(ForumManager fmgr, String content) throws RepositoryException, ForumException {
		System.out.println("Comment content search for " + content + ":");
		ModelMapper mm = new ModelMapper(fmgr);
		for(Comment c :mm.findCommentByContent(content, true)) {
			printComment("", c);
		}
	}
	public static void test(Map<String, String> parameters, String usr, String pwd) throws RepositoryException, ForumException, InterruptedException {
		Repository repo = getRepository(parameters);
		ForumManager fmgr = new ForumManager(repo, usr, pwd);
		// add 2 topics
		String smart = fmgr.addTopic("Smart questions", "This is for smart questions");
		String dumb = fmgr.addTopic("Dumb questions", "This is for dumb questions");
		// add 3 posts
		String plus1 = fmgr.addPost(dumb, "What is 1+1?", "I have a math question: what is 1+1?","arne");
		String plus2 = fmgr.addPost(dumb, "What is 2+2?", "I have another math question: what is 2+2?", "arne");
		String life = fmgr.addPost(smart, "What is the purpose of life?", "I am in the philosophical mood. What is the purpose of life?", "arne");
		// add 4 comments
		String plus1a = fmgr.addCommentAutoNumber(plus1, "2", "arne");
		String plus2a = fmgr.addCommentAutoNumber(plus2, "4", "arne");
		String lifea1 = fmgr.addCommentAutoNumber(life, "Good question.", "arne");
		String lifea2 = fmgr.addCommentAutoNumber(life, "Please tell me if you find out.", "arne");
		// add 2 comments to comments to comments
		fmgr.addCommentAutoNumber(plus1a, "Hmmm.", "Arne");
		fmgr.addCommentAutoNumber(plus2a, "Hmmm.", "Arne");
		// update 1 comment twice
		Thread.sleep(1000);
		fmgr.updateComment(plus1a, "Forget it.");
		fmgr.updateComment(plus2a, "Forget it.");
		Thread.sleep(1000);
		fmgr.updateComment(plus1a, "Sorry.");
		fmgr.updateComment(plus2a, "Sorry.");
		// test load
		testForumLoad(fmgr);
		testTopicLoad(fmgr, smart);
		testPostLoad(fmgr, life);
		testCommentLoad(fmgr, lifea1);
		testCommentLoad(fmgr, lifea2);
		// test search
		testPostAuthorSearch(fmgr, "xxx");
		testPostAuthorSearch(fmgr, "arne");
		testPostContentSearch(fmgr, "math");
		testPostContentSearch(fmgr, "foobar");
		testCommentAuthorSearch(fmgr, "xxx");
		testCommentAuthorSearch(fmgr, "arne");
		testCommentContentSearch(fmgr, "good");
		testCommentContentSearch(fmgr, "foobar");
	}
}
