package com.tandbergtv.watchpoint.pmm.handlers;

import java.io.File;
import java.util.Properties;

import junit.framework.TestCase;

import com.tandbergtv.metadatamanager.JPFActivator;
import com.tandbergtv.watchpoint.pmm.communication.handlers.GetTitleIdsMessageHandler;
import com.tandbergtv.watchpoint.pmm.core.PMMActivator;
import com.tandbergtv.watchpoint.pmm.dao.hibernate.ApplicationContextHelper;
import com.tandbergtv.watchpoint.pmm.title.TitleActivator;
import com.tandbergtv.workflow.message.MessageUIDImpl;
import com.tandbergtv.workflow.message.WorkflowMessage;
import com.tandbergtv.workflow.message.WorkflowPayload;

public class GetTitleId extends TestCase {

	private ApplicationContextHelper helper;
	private static final String PLUGIN_FOLDER_RELATIVE_PATH = "plugins";
	private static final String JPF_PLUGIN_REPOSITORY_PROPERTY = "org.java.plugin.boot.pluginsRepositories";
	private JPFActivator jpf;

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		String[] contextConfigFiles = {
				"MetadataBeansContext.xml",
				"com/tandbergtv/watchpoint/pmm/dao/hibernate/TitleMetadataManagerDBContext.xml",
				"com/tandbergtv/watchpoint/pmm/dao/hibernate/AssetDataSource.xml" };
		helper = new ApplicationContextHelper(contextConfigFiles);
		initializeTitleServices();
		super.setUp();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	private void initializeTitleServices() {
		/* Properties for initializing JPF */
		Properties properties = new Properties();
		String installFolder = "D://opt//tandbergtv//cms";
		File pluginFolder = new File(installFolder, PLUGIN_FOLDER_RELATIVE_PATH);
		properties.put(JPF_PLUGIN_REPOSITORY_PROPERTY, pluginFolder
				.getAbsolutePath());

		/* Start JPF */
		jpf = new JPFActivator();
		jpf.start(properties);

		/* Title and Asset related initialization */
		TitleActivator activator = new TitleActivator();
		activator.setPluginManager(jpf.getPluginManager());
		activator.start();

		/* pmm activator */
		PMMActivator pmmActivator = new PMMActivator();
		pmmActivator.start();
	}
	
	public void _testGettingTitle() {
		WorkflowMessage message = new WorkflowMessage(new MessageUIDImpl(
				"11PM01"));
		WorkflowPayload payload = message.getPayload();
		payload
				.putValue(
						"metadataPath",
						"D://ProjectSpace//Workspace//MetadataManager//tests//com//tandbergtv//spec//cl1_1-sample.xml");
		payload.putValue("titleIds", "");

		GetTitleIdsMessageHandler handler = new GetTitleIdsMessageHandler();
		try {
			handler.handleMessage(message);
			System.out.println("Title ids: " + message.getValue("titleId"));
		} catch (Exception e) {
			System.out.println(e);
			fail();
		}
	}
}
