/*
 * 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 aee.csv;

import java.util.Map;

import aee.engine.DeviceTable;
import aee.engine.FlowDest;
import aee.engine.FlowTable;
import aee.engine.InOutTable;
import aee.exception.AeeException;


/**
 * Write an input or output table as a CSV file
 */
public class InOutTableCsvWriter extends CsvWriter {

	/**
	 * Constructor
	 * @param fn Filename to write
	 * @throws AeeException if any I/O exception
 	 */
	public InOutTableCsvWriter(String fn) throws AeeException {
		super(fn);
	}
	
	/**
	 * Write the table to the file
	 * @param table       The input or output table
	 * @param deviceTable The device table
	 * @throws AeeException if any I/O exception
	 */
	public void write(InOutTable table, DeviceTable deviceTable, FlowTable flowTable)
		throws AeeException {
		
		Map<Integer, String> devices = deviceTable.getNameTable();
		Map<Integer, String> flows   = flowTable.getNameTable();
		int[]                order   = table.getDeviceOrder();
		
		// Header 1 has the format
		//   ,Device#,1,2,3,4,...
		String[] record = new String[2+order.length];
		record[0]       = "";
		record[1]       = "Device#";
		for (int i = 0; i < devices.size(); i++) {
			record[i+2] = String.format("%d", i+1);
		}
		write(record);
		
		// Header 2 has the format
		//     Flow#,,device1,device2, ...
		// where the device names are specified by the device order
		record[0] = "Flow#";
		record[1] = "";
		int  i    = 2;
		for (int iord : order) {
			record[i++] = devices.containsKey(iord)? devices.get(iord) : "";
		}
		write(record);
	
		// Write flow and flow destinations
		for (FlowDest flow : table.getFlows()) {
			record[0] = String.format("%d", flow.getId());
			record[1] = flows.containsKey(flow.getId())? flows.get(flow.getId()) : "";
			
			for (int j = 0; j < order.length; j++)
				record[j+2] = "";
			
			for (Map.Entry<Integer, Integer> entry : flow.getDestinations().entrySet()) {
				int dest  = entry.getKey();
				int value = entry.getValue();
				for (int j = 0; j < order.length; j++) {
					if (dest == order[j]) {
						record[2+j] = String.format("%d", value);
						break;
					}
				}  // j
			}  // entry
			write(record);
		}  // flow
	}
}
