/*
 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
 *
 * EXPORT CONTROLLED
 * ECCN#   EAR99
 * Warning: This document contains technical data which is controlled under the
 * Bureau of Industry & Security's Export Administration Regulations and may not be
 * exported or re-exported without authorization by the Bureau of Industry & Security.
 * Diversion contrary to US law is prohibited.
 *
 * (c) 2011 United Technologies Corp.
 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
 * including any software or models in source or binary form, as well as any drawings, 
 * specifications, and documentation (collectively "the Data"), to deal in the Data without 
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Data.
 *
 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
 * USE OR OTHER DEALINGS IN THE DATA.
*/

package proxy.clients;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import meta.rmi.Service;
import meta.rmi.ServiceResponse;
import proxy.CompositeArgumentProxyElement;
import proxy.CompositeArgumentProxySource;
import proxy.MissionProxy;
import proxy.ProxyElement;
import exception.ControllerException;

/**
 * A proxy to the TSFC service
 */
public class TsfcProxy extends SOAProxy {
	private String  host;
	private Service service;

	/**
	 * Constructor
	 *
	 * @param host Where the Fuel Consumption service is running
	 */
	public TsfcProxy(String host) {
		this.host    = host;
		this.service = null;
	}

	/* (non-Javadoc)
	 * @see proxy.SOAProxy#getService()
	 */
	@Override
	public Service getService() throws MalformedURLException, RemoteException,
			NotBoundException {
		if (service == null)
			service = (Service) Naming.lookup(String.format("rmi://%s/tsfc", host));
		return service;
	}

	/**
	 * Run the TSFC service
	 * @param platform The platform name
	 * @param arch     The architecture
	 * @param mission  The mission
	 * @return The tsfc response
	 * @throws ControllerException if an error invoking the TSFC service
	 */
	public ServiceResponse calcTsfc(final String platform, 
			                                   final ProxyElement arch, 
			                                   MissionProxy mission)
		throws ControllerException {
		List<ProxyElement> args = new ArrayList<ProxyElement>(1+ mission.size());
		args.addAll(mission.getElements());
		
		CompositeArgumentProxySource source = new CompositeArgumentProxySource() {

			@Override
			public List<Element> getElements() throws ControllerException {
				List<Element> elements = new ArrayList<Element>(1);
				elements.add(arch.getElement());
				return elements;
			}

			@Override
			public String getName()  throws ControllerException {
				return platform;
			}

			@Override
			public String getType()  throws ControllerException {
				return arch.getElement().getName();
			}
		};
		args.add(new CompositeArgumentProxyElement(source));
		return run("TSFC", args);
	}
}
