/**
 * TitleMetadataOptionProvider.java
 * Created on May 19, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.tandbergtv.watchpoint.pmm.job.conf.ParameterReferenceGroup;
import com.tandbergtv.watchpoint.pmm.job.conf.ParameterReferenceItem;
import com.tandbergtv.watchpoint.pmm.title.conf.ISpecificationManager;
import com.tandbergtv.workflow.core.service.ServiceRegistry;

/**
 * This class simply calls SpecManager to get the list of menu options
 * and builds the group objects required to be returned back.
 * 
 * @author spuranik 
 */
public class TitleMetadataOptionProvider implements IMenuOptionProvider {

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.job.IMenuOptionProvider#getJobRuleParams()
	 */
	public List<ParameterReferenceItem> getJobRuleMenuOptions() {
		ISpecificationManager specMgr = ServiceRegistry.getDefault().lookup(
				ISpecificationManager.class);	
		Map<String, Set<String>> options = specMgr.getJobRuleMenuOptions();		
		List<ParameterReferenceItem> groups = buildGroups(options);
		return groups;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.job.IMenuOptionProvider#getJobParameterMenuOptions()
	 */
	public List<ParameterReferenceItem> getJobParameterMenuOptions()
	{
		ISpecificationManager specMgr = ServiceRegistry.getDefault().lookup(
				ISpecificationManager.class);
		Map<String, Set<String>> options = specMgr.getJobParameterMenuOptions();
		List<ParameterReferenceItem> groups = buildGroups(options);
		return groups;
	}
	
	/**
	 * @param options
	 * the map of options returned by spec manager
	 * @return
	 * list of group objects reflecting the same structure as in the options
	 */
	private List<ParameterReferenceItem> buildGroups(Map<String, Set<String>> options)
	{
		List<ParameterReferenceItem> groups = new ArrayList<ParameterReferenceItem>();
		Iterator<String> iterator = options.keySet().iterator();
		// for each entry in the map build a group
		// NOTE: the parent is not set here. It is in the one who calls this class
		while(iterator.hasNext())
		{
			String groupName = iterator.next();
			ParameterReferenceGroup group = new ParameterReferenceGroup();
			groups.add(group);
			group.setName(groupName);
			// for each item in the list build an item object and add it to the group
			Set<String> items = options.get(groupName);
			for(String currItem : items)
			{
				String itemname = currItem;
				ParameterReferenceItem item = new ParameterReferenceItem();
				item.setName(itemname);
				item.setValue(itemname);
				item.setParent(group);
				group.addItem(item);
			}			
		}
		return groups;
	}
}
