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

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * Implementation of the interface {@link AttractionGenerator}.
 * 
 * Generates the given number of {@link IAttractionPoint}s and sets the
 * position randomly within the world dimensions.
 *
 * v1.1: statically available attraction points [JZ]
 * v1.2: added a minimum distance between attraction points [JZ]
 * v1.21: added a random radius to attraction points. Minimum is 10 meters, default maximum 100 meters. APs cannot have overlapping radii. 
 * 
 * @author Christoph Muenker, Julian Zobel
 * @version 1.21, 12 2018
 */
public class RandomAttractionGenerator extends AbstractAttractionProvider {

	private Random rand;

	private int numberOfAttractionPoints;

	private boolean numberOfAPsSet = false;
	
	private double minimumDistance = 50;
	
	private double maximumRadius = 100; 
	
	private double minimumRadius = 10;
	
	@XMLConfigurableConstructor({"numberOfAttractionPoints", "maximumRadius", "minimumRadius", "minimumDistance"})
	public RandomAttractionGenerator(int numberOfAttractionPoints, double maximumRadius, double minimumRadius, double minimumDistance) {
		super();
		
		this.rand = Randoms.getRandom(RandomAttractionGenerator.class);		
		if (numberOfAttractionPoints <= 0) {
			throw new ConfigurationException(
					"NumberOfAttractionPoints should be at least 1!");
		}
		this.numberOfAPsSet = true;
		this.numberOfAttractionPoints = numberOfAttractionPoints;
		this.maximumRadius = maximumRadius;
		this.minimumRadius = minimumRadius;
		this.minimumDistance = minimumDistance;
		
		createAttractionPoints();
	}

	@Override
	public LinkedList<AttractionPoint> getAttractionPoints() {
		if (!numberOfAPsSet) {
			throw new ConfigurationException(
					"Number of Attraction Points is not set in RandomAttractionGenerator!");
		}

		if(super.getAttractionPoints().isEmpty())
			createAttractionPoints();
		
		return super.getAttractionPoints();
	}
	
	private void createAttractionPoints() {
		LinkedList<AttractionPoint> result = new LinkedList<AttractionPoint>();
		
		// make a break counter to prevent more than 10 iterations and an infinity loop in general.
		int c = 0;
			
		create: for (int i = 0; i < numberOfAttractionPoints; i++) {
			PositionVector posVec = createPosVec();
						
			// set the radius of this attraction point
			// minimum radius is 10 meters
			double radius = Math.max(minimumRadius, rand.nextDouble() * maximumRadius);
			
			if(c < 20)
			{
				// if not within the world dimensions, directly go back to calculation
				if(posVec.getX() + radius > worldDimension.getX() || posVec.getY() + radius > worldDimension.getY()
							|| posVec.getX() - radius < 0 || posVec.getY() - radius < 0) {
					i--;
					c++;
					continue create;
				}				
				
				for (IAttractionPoint ap : result) {					
					// if this point is closer than the given minimum distance to another point, or the radii of the points would overlap, 
					// or if the radius would exceed the simulation area
					// then discard this attraction point and create a new one
					if(posVec.distanceTo(ap) < minimumDistance || (posVec.distanceTo(ap) - radius - ap.getRadius()) < 0 ) {
						i--;
						c++;
						continue create;
					}
				}			
			}
			else
			{				
				radius = minimumRadius;
			}			
			
			AttractionPoint aPoint = new AttractionPoint("AP-"+i, posVec);
			aPoint.setRadius(radius);
			c = 0;
			result.add(aPoint);
		}
		
		for (AttractionPoint ap : result) {
			addAttractionPoint(ap);
		}
	}

	private PositionVector createPosVec() {
		double x = rand.nextDouble() * worldDimension.getX();
		double y = rand.nextDouble() * worldDimension.getY();
		return new PositionVector(x, y);
	}
	
	/**
	 * Set a minimum distance between the randomly generated attraction points
	 * @param distance
	 */
	public void setMinimumDistance(double distance) {
		this.minimumDistance = distance;
	}
	
	/**
	 * Set a maximum radius that an attraction point can have.
	 * @param radius
	 */
	public void setMaximumRadius(double radius) {
		this.maximumRadius = radius;
	}
	
	/**
	 * Set a minimum radius that an attraction point can have. Minimum is 10 meters.
	 * @param radius
	 */
	public void setMinimumRadius(double radius) {
		if(radius < 10) {
			this.minimumRadius = 10;
		}
		else {
			this.minimumRadius = radius;
		}
		
	}
}
