/*
 * 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.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.SmarterMovementModelNew;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

public class SmarterHost {
	private SimLocationActuator actuator;
	private Location nextLocation;
	private long _sleepTime;
	private int currentIndex;
	
	private Scanner scanner;
	private boolean init = false;

	public SmarterHost(SimLocationActuator actuator, int currentIndex) {
		this.actuator = actuator;
		this.currentIndex = currentIndex;
	}

	public SimLocationActuator getActuator() {
		return actuator;
	}

	public Location getNextLocation() {
		return nextLocation;
	}

	public long getSleepTime() {
		return _sleepTime;
	}

	public boolean tryNextStep() {
		if (scanner == null && !init) {
			init = true;
			try {
				//File file = new File("smarter/trace/mobilityTraceFile-File-" + currentIndex + ".if");
				File file = new File("smarter/traces-mv/mobilityTraceFile-File-Modified-" + currentIndex + ".if");
				
				if (!file.exists()) {
					_sleepTime = -1;
					return false;
				}
				
				scanner = new Scanner(file);
			} catch (FileNotFoundException e) {
				_sleepTime = -1;
				return false;
			}
		}
		
		if (!scanner.hasNextLine()) {
			_sleepTime = -1;
			return false;
		}
		
		String line = scanner.nextLine();
		String[] split = line.split(" ");

		long nextTimestamp = Long.valueOf(split[0]);
		double positionX = Double.valueOf(split[1]);
		double positionY = Double.valueOf(split[2]);
		
		// The offset to place the nodes correctly at their real starting point on the map (Staum�hle as reference)
		double offsetX = -700;
		double offsetY = -230;
		
		// Calculation of next position
		double posX = positionX+offsetX;
		double posY = positionY+offsetY;

		nextLocation = new PositionVector(positionX, positionY);
		_sleepTime = nextTimestamp * SmarterMovementModelNew.getInstance().getTimeBetweenMoveOperations() - Time.getCurrentTime();
		return true;
	}
}
