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

import java.util.Random;
import java.util.Set;

import de.tud.kom.p2psim.api.common.HostProperties;
import de.tud.kom.p2psim.api.common.SimHost;

import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;

import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;



/**
 * Default implementation of a {@link TopologyComponent}.
 *
 * @author Bjoern Richerzhagen, Julian Zobel
 * @version 2.0, 05.09.2018
 */
public class DefaultTopologyComponent extends AbstractTopologyComponent {

	protected static Random rnd = Randoms.getRandom(DefaultTopologyComponent.class);

	private double currentMovementSpeed = -1;


	/**
	 * Create a TopologyComponent for the current host.
	 *
	 * @param host
	 * @param topology
	 * @param movementModel
	 */
	public DefaultTopologyComponent(SimHost host, Topology topology,
			MovementModel movementModel, PlacementModel placementModel, boolean registerAsInformationProviderInSiS) {
		super(host, topology, movementModel, placementModel, registerAsInformationProviderInSiS);
	}

	@Override
	public void initialize() {
		super.initialize();
	}

	@Override
	public double getMinMovementSpeed() {
		HostProperties properties = getHost().getProperties();

		return properties.getMinMovementSpeed();
	}

	@Override
	public double getMaxMovementSpeed() {
		HostProperties properties = getHost().getProperties();
		return properties.getMaxMovementSpeed();
	}

	private void calcRandomMovementSpeed() {
		double min_speed = getMinMovementSpeed();
		double max_speed = getMaxMovementSpeed();

		double value = rnd.nextDouble();
		this.currentMovementSpeed = (value * (max_speed - min_speed))
				+ min_speed;
	}

	@Override
	public double getMovementSpeed() {
		if (currentMovementSpeed == -1) {
			calcRandomMovementSpeed();
		}
		return this.currentMovementSpeed;
	}

	@Override
	public void setMovementSpeed(double speed) {
		this.currentMovementSpeed = speed;
	}
	
	@Override
	public void setTargetAttractionPoint(IAttractionPoint targetAttractionPoint)
			throws UnsupportedOperationException {
		movementModel.changeTargetLocation(this, targetAttractionPoint);
	}

	@Override
	public IAttractionPoint getCurrentTargetAttractionPoint() {
		return movementModel.getTargetLocation(this);
	}

	@Override
	public Set<IAttractionPoint> getAllAttractionPoints() {
		return movementModel.getAllAttractionPoints();
	}	

	public static class Factory implements TopologyComponentFactory {

		@Override
		public TopologyComponent createTopologyComponent(SimHost host,
				Topology topology, MovementModel movementModel,
				PlacementModel placementModel,
				boolean registerAsInformationProviderInSiS) {
			return new DefaultTopologyComponent(host, topology, movementModel, placementModel, registerAsInformationProviderInSiS);
		}
		
	}
}
