package com.tandbergtv.watchpoint.pmm;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;

import junit.framework.TestCase;

import com.tandbergtv.metadatamanager.model.Field;
import com.tandbergtv.metadatamanager.model.Group;
import com.tandbergtv.metadatamanager.model.Item;
import com.tandbergtv.metadatamanager.model.Item.ItemType;
import com.tandbergtv.watchpoint.pmm.core.PMMActivator;
import com.tandbergtv.watchpoint.pmm.dao.hibernate.ApplicationContextHelper;
import com.tandbergtv.watchpoint.pmm.entities.DistributionSchedule;
import com.tandbergtv.watchpoint.pmm.entities.Schedule;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.title.TitleActivator;
import com.tandbergtv.watchpoint.pmm.title.conf.specs.RightsManagerFactory;
import com.tandbergtv.watchpoint.pmm.web.schedule.ScheduleStatistics;

public class RightsManagerTest extends TestCase {

	private static final String PLUGIN_FOLDER_RELATIVE_PATH = "plugins";
	private static final String JPF_PLUGIN_REPOSITORY_PROPERTY = "org.java.plugin.boot.pluginsRepositories";

	/*
	 * (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" };
		new ApplicationContextHelper(contextConfigFiles);
		initializeTitleServices();
		super.setUp();
	}

	public void _testRightsManager() {
		Title t = new Title();

		Group g = new Group();
		Item titleItem = new Item();
		titleItem.setType(ItemType.TITLE);
		titleItem.addField(new Field(
				"/tns:Fields/tns:Rights/tns:LicensingWindowEnd",
				"2009-04-10"));
		g.addChild(titleItem);
		t.setAsset(g);

		System.out.println("Title licensed: "
				+ RightsManagerFactory.getRightsManager().isLicensed(t,
						new Date()));
	}

	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();

		/* pmm activator */
		PMMActivator pmmActivator = new PMMActivator();
		pmmActivator.start();
	}

	public void testRefreshRateCalculation() {
		Title t = new Title();

		Group g = new Group();
		Item titleItem = new Item();
		titleItem.setType(ItemType.TITLE);
		titleItem.addField(new Field("/tns:Fields/tns:Duration/tns:Duration",
				"00:01:30"));
		g.addChild(titleItem);
		t.setAsset(g);

		Schedule s = new DistributionSchedule();
		((DistributionSchedule) s).setPitchDate(new Date());
		s.addTitle(t);
		
		Collection<Schedule> previous = new ArrayList<Schedule>();
		 previous.add(s);
		
		ScheduleStatistics statistics = new ScheduleStatistics(s, previous);
		System.out.println("Refresh rate: " + statistics.getRefreshRate());
	}
}
