package com.tandbergtv.watchpoint.pmm;

import static com.tandbergtv.watchpoint.pmm.title.search.TitleSearchKey.TITLE_PROVIDER_ID;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

import junit.framework.TestCase;

import com.tandbergtv.metadatamanager.JPFActivator;
import com.tandbergtv.watchpoint.pmm.dao.hibernate.ApplicationContextHelper;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.title.MetadataField;
import com.tandbergtv.watchpoint.pmm.title.TitleActivator;
import com.tandbergtv.watchpoint.pmm.title.TitleService;
import com.tandbergtv.watchpoint.pmm.title.provider.internal.DefaultTitleSearchStrategy;
import com.tandbergtv.workflow.util.SearchCriteria;

public class SearchCriteriaBuilderTest extends TestCase {

	private static final String PLUGIN_FOLDER_RELATIVE_PATH = "plugins";
	private static final String JPF_PLUGIN_REPOSITORY_PROPERTY = "org.java.plugin.boot.pluginsRepositories";

	private ApplicationContextHelper helper;

	/*
	 * (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();
	}

	public void _testFindingRootTitles() {
		Collection<MetadataField> metadata = new ArrayList<MetadataField>();

		MetadataField f1 = new MetadataField(
				"/tns:Fields/tns:Title/tns:ShortTitle/tns:Value", "some title");
		metadata.add(f1);

		SearchCriteria criteria = null;
		// SearchCriteriaBuilder.getCriteria(Integer
		// .parseInt(DEFAULT_SYSTEM_ID.toString()), 0,
		// AssetSearchKey.ALL_DESCENDANT_FIELDS, metadata, 0,
		// Integer.MAX_VALUE, null, null, null);

		criteria.removeParameter(TITLE_PROVIDER_ID.toString());
		DefaultTitleSearchStrategy strategy = new DefaultTitleSearchStrategy();
		Collection<Title> titles = strategy.search(criteria);
		System.out.println("Got " + titles.size() + " titles.");
	}

	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 */
		JPFActivator jpf = new JPFActivator();
		jpf.start(properties);

		/* Title and Asset related initialization */
		TitleActivator activator = new TitleActivator();
		activator.setPluginManager(jpf.getPluginManager());
		activator.start();
	}

	public void testGettingItems() {
		TitleService service = new TitleService();

//		Collection<Asset> assets = service
//				.findAssetsWithFilePath("captaincorellis.bmp");
//		for (Asset a : assets) {
//			System.out.println(a.getId());
//		}		
	}
}
