package li.rudin.wikidoc.layer;

import java.util.Vector;

import li.rudin.wikidoc.layer.base.WikiInterface;
import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.core.contentRep.SimpleArticle;
import net.sourceforge.jwbf.mediawiki.actions.queries.CategoryMembersFull;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
import net.sourceforge.jwbf.mediawiki.contentRep.CategoryItem;

public class MediaWikiImpl implements WikiInterface
{
	/**
	 * MediaWiki Instance
	 */
	private MediaWikiBot bot;
	
	@Override
	public Article readArticle(String title) throws Exception
	{
		return bot.readContent(title);
	}

	@Override
	public  Vector<String> getCategoryMembers(String category) throws Exception
	{
		CategoryMembersFull members = new CategoryMembersFull(bot, category);
		
		Vector<String> ret = new Vector<String>();
		
		for (CategoryItem item: members)
		{
			ret.add( item.getTitle() );
		}
		
		return ret;
	}

	@Override
	public void open(String url) throws Exception
	{
		bot = new MediaWikiBot(url);
	}

	@Override
	public void login(String username, String password) throws Exception
	{
		bot.login(username, password);
	}

	@Override
	public void writeArticle(SimpleArticle article) throws Exception
	{
		bot.writeContent(article);
	}

	@Override
	public void close() throws Exception
	{
		//TODO
	}

	@Override
	public void deleteArticle(String title) throws Exception
	{
		Article unknownArticle = bot.readContent(title);
		System.out.println("Deleting: " + unknownArticle.getTitle());
		unknownArticle.delete();
	}

}
