/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


package com.nokia.carbide.trk.support.service;

import com.nokia.carbide.remoteconnections.interfaces.*;
import com.nokia.carbide.trk.support.connection.*;

import java.util.*;

/**
 *
 */
public class ConnectedServiceFactory implements IConnectedServiceFactory {

	/* (non-Javadoc)
	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectedServiceFactory#createConnectedService(com.nokia.carbide.remoteconnections.interfaces.IService, com.nokia.carbide.remoteconnections.interfaces.IConnection)
	 */
	public IConnectedService createConnectedService(IService service, IConnection connection) {
		if (service instanceof TracingService &&
				isCompatibleConnection(getCompatibleTracingConnectionTypeIds(), connection)) {
			return new TracingConnectedService(service, (AbstractSynchronizedConnection) connection);
		}
		else if (service instanceof TRKService &&
				isCompatibleConnection(getCompatibleTRKConnectionTypeIds(), connection)) {
			return new TRKConnectedService(service, (AbstractSynchronizedConnection) connection);
		}
		
		return null;
	}
	
	private static boolean isCompatibleConnection(Collection<String> compatibleIds, IConnection connection) {
		String connectionTypeId = connection.getConnectionType().getIdentifier();
		for (String id : compatibleIds) {
			if (connectionTypeId.equals(id))
				return true;
		}
		
		return false;
	}

	private Collection<String> getCompatibleTracingConnectionTypeIds() {
		return Arrays.asList(new String[] {
				USBConnectionType.ID
		});
	}
	
	private Collection<String> getCompatibleTRKConnectionTypeIds() {
		return Arrays.asList(new String[] {
				SerialConnectionType.ID, 
				SerialBTConnectionType.ID,
				USBConnectionType.ID}); 
	}

	/* (non-Javadoc)
	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectedServiceFactory#getCompatibleConnectionTypeIds(com.nokia.carbide.remoteconnections.interfaces.IService)
	 */
	public Collection<String> getCompatibleConnectionTypeIds(IService service) {
		if (service instanceof TRKService)
			return getCompatibleTRKConnectionTypeIds();
		else if (service instanceof TracingService)
			return getCompatibleTracingConnectionTypeIds();
		return Collections.EMPTY_LIST;
	}

}
