package src.hibernatebb.unittests;

import junit.framework.TestCase;
import junit.framework.Assert;

import src.hibernatebb.discussion.Post;
import src.hibernatebb.discussion.Topic;
import src.hibernatebb.discussion.User;
import src.hibernatebb.discussion.BoardManager;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;

public class TopicTest extends TestCase {
	Topic t = new Topic();
	Topic tn = new Topic();
	List plist = new ArrayList();
	List chklist = new ArrayList();
	ListIterator l1,l2;
	Post p1,p2,p3,p4;
	User u1 = new User("b$Money","ykooj");
	User u2,u3;
	BoardManager bm = new BoardManager();
	Topic t1,t2,t3;

	protected void setUp() throws Exception {
		super.setUp();
		bm.saveUser(u1);
		p1 = new Post();
		p2 = new Post();
		p3 = new Post();
		
		p1.setSubject("Test1");
		p2.setSubject("Test2");
		p3.setSubject("Test3");
		
		p1.setBody("TestBody1");
		p2.setBody("TestBody2");
		p3.setBody("TestBody3");
		
		p1.setPoster(u1);
		p2.setPoster(u1);
		p3.setPoster(u1);
		
		t.setID("The Test Topic");
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testTopicSave() throws Exception {
		bm.saveTopic(t);
		t1 = new Topic();
		t2 = new Topic();
		t3 = new Topic();
		
		t1.setID("Much ado about nothing");
		t2.setID("Why we deserve extra credit");
		t3.setID("The secret to Jooky");
		
		p4 = new Post();
		p4.setSubject("The real jooky");
		p4.setBody("Its jooky its jooky, party in a can!");
		p4.setPoster(u1);
		
		bm.saveTopic(t1);
		bm.saveTopic(t2);
		
		chklist.add(p4);
		bm.saveTopic(t3);
		
		t3.setPosts(chklist);
		
		tn = bm.loadTopic(t.getID());
		Assert.assertEquals(t.getID(),tn.getID());
		
		//System.out.println(":::Topic saved and loaded");
		
		plist.add(p1);
		plist.add(p2);
		plist.add(p3);
		
		t.setPosts(plist);
		bm.saveTopic(t);
		
		tn = bm.loadTopic(t.getID());
		//System.out.println(":::Topic id: " + tn.getID());
		
		chklist = tn.getPosts();
		
		l1 = plist.listIterator();
		l2 = chklist.listIterator();
		
		while (l1.hasNext() && l2.hasNext()) {
			p1 = (Post)l1.next();
			p2 = (Post)l2.next();
			
			System.out.println(p1.getPoster().getID());
			
			System.out.println("Orig: " + p1.getSubject() + p1.getPoster().getID() + 
					" ||| New: " + p2.getSubject() + p2.getPoster().getID());
			Assert.assertEquals(p1.getSubject(),p2.getSubject());			
		}
		
	}

	public void testGetTopics() throws Exception {
		List l = bm.getTopics();
		ListIterator li = l.listIterator();
		
		while(li.hasNext()) {
			System.out.println(((Topic)li.next()).getID());
			Assert.assertTrue(true);
		}
		
	}

	public void testGetUserFromTopic() throws Exception {
		Topic top = bm.loadTopic("The Test Topic");
		List l = top.getPosts();
		ListIterator li = l.listIterator();
		
		while(li.hasNext()) {
			System.out.println(((Post)li.next()).getPoster().getID());
			Assert.assertTrue(true);
		}
		
	}

}
