package nl.nikhef.sfp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;

/**
 * SFPManager keeps a list of SFPProviders.
 * 
 * @author vincentb
 *
 */
public class SFPManager {

	private static final Logger LOG = Logger.getLogger(SFPManager.class.getSimpleName());
	
	
	private List<SFPProvider> _providers = new ArrayList<SFPProvider>();
	private List<SFPProvider> _umProviders = Collections.unmodifiableList(_providers);
	
	
	public SFPManager() 
	{
		ServiceLoader<SFPProvider> prov = ServiceLoader.load(SFPProvider.class);
		

		for (SFPProvider sp : prov)
		{
			LOG.info(String.format("Found provider: %s", sp.getName()));
			_providers.add(sp);
		}
		
	}
	
	
	public List<SFPProvider> getProviders()
	{
		return _umProviders;
	}
	
	/**
	 * Proxy function which will add the listener to each provider.
	 */
	public void addSFPProviderListener(SFPProviderListener l)
	{
		for (SFPProvider prov : _providers)
		{
			prov.addSFPDeviceListener(l);
		}
	}
	
	/**
	 * Proxy function which will remove the listener from each provider.
	 */
	public void removeSFPProviderListener(SFPProviderListener l)
	{
		for (SFPProvider prov : _providers){
			prov.removeSFPDeviceListener(l);
		}
	}
	
	
	/**
	 * Must be invoked each second to update the sfp device state.
	 * 
	 * @param scanForNewDevices		Whether or not to scan for new devices on the bus.
	 */
	public synchronized void update(boolean scanForNewDevices)
	{
		for (SFPProvider prov : _providers)
		{
			if (scanForNewDevices) prov.scanForNewDevices();
			else prov.updateDevices();
		}
	}
			
	
	

	
	
}
