/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement.smarter.host;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;

/**
 * Sets the initial positions for the nodes by reading an If file with the format "ID x y". From the SMARTER traces we acquired the If files
 * by transforming the GPS coordinates into 2D coordinates with x and y values with a certain reference point placed in Staumuehle.
 * Before calling this class you should apply the analyzing function in (@link SmarterMovementModelNew) to create a new If file which contains 
 * the first relevant position of all nodes where each line corresponds to the initial position of a node.
 * 
 * @author MarcelV
 * @version 1.0, 12.06.2018
 */
public class IfPlacement implements PlacementModel {
	private List<PositionVector> positions;
	private String file;
	private int numberOfComponents;
	
	// The file parts separator. If file has the format "nodeID x y"
	private final String SEP = " ";

	private int positionIndex;

	IfPlacement(String file){
		positions = new Vector<PositionVector>();
		
		// TODO Set String to file either in XML document or hard code it here. Discuss solution
		this.file = file;
		numberOfComponents = 0;
	}
	
	/**
	 *  Calculating the total number of added components
	 */
	@Override
	public void addComponent(TopologyComponent comp) {
		numberOfComponents++;
	}
	
	/**
	 *  Placing the components on their initial position acquired from the If file. Is called for each component to be created
	 */
	@Override
	public PositionVector place(TopologyComponent comp) {
		if(positions.isEmpty() || positionIndex >= positions.size()) {
			readData();
			positionIndex = 0;
		}
		PositionVector vec = positions.get(positionIndex);
		// TODO implementieren
		//comp.updateCurrentLocation(positions.get(positionIndex));
		positionIndex++;
		return vec;
	}
	
	/**
	 * Reads the If file and stores all positions locally in the class variable where they can then be accessed later within the place() 
	 * function in order to place the components.
	 */
	public void readData() {
		positions.clear();
		boolean entrySuccessfullyRead = false;
		BufferedReader ifReader = null;

		try {
			ifReader = new BufferedReader(new FileReader(file));
			
			// Extracting initial position values and storing them in a list of PositionVector objects
			while(ifReader.ready()) {
				String line = ifReader.readLine();

				if(line.indexOf(SEP) > -1) {
					// nodeID x y
					String[] parts = line.split(SEP);
					if(parts.length == 3) {
						try {
							double x = Double.parseDouble(parts[1]);
							double y = Double.parseDouble(parts[2]);

							positions.add(new PositionVector(x,y));
							entrySuccessfullyRead = true;
						}
						catch (NumberFormatException e) {
							if(!entrySuccessfullyRead)
								e.printStackTrace();
						}
					}
				}
				else {
					throw new AssertionError("To many columns in CSV.");
				}
			}
		} catch (Exception e) {
			System.err.println(e.toString());
		}
		// Closing the reader
		finally {
			if (ifReader != null) {
				try {
					ifReader.close();
				} catch (IOException e) {
					// 
				}
			}
		}
	}
	
	public int getNumberOfComponents() {
		return numberOfComponents;
	}
}