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


import java.util.LinkedHashSet;
import java.util.Set;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;


/**
 * 
 * 
 * 
 * @author Julian Zobel
 * @version 1.1, 27.01.2020
 * 
 */
public class HostAtAttractionPointCounter {
		
	/**
	 * Compares the locations of a given {@link Host} and a given {@link IAttractionPoint} and checks if the host lies within the radius of the {@link IAttractionPoint}.
	 * Returns true, if the hosts location is within the radius of the {@link IAttractionPoint}, false otherwise.
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @param IAttractionPoint 
	 * @return Boolean
	 */
	public static boolean isHostInAttractionPointArea(SimLocationActuator host, IAttractionPoint attractionPoint) {
		PositionVector hostLocation = host.getRealPosition();
		
		if(!attractionPoint.hasRadius())
			return false;
		
		if(attractionPoint.distanceTo(hostLocation)	 <= attractionPoint.getRadius()) 
			return true;
		
		return false;
	}
	
	/**
	 * Returns the number of hosts within a specific {@link IAttractionPoint}
	 * 
	 * @param IAttractionPoint The {@link IAttractionPoint} to be checked.
	 * @return Integer
	 */
	public static int getHostCountOfAttractionPoint(IAttractionPoint attractionPoint, LinkedHashSet<SimLocationActuator> hostsToConsider) {
		return getHostsOfAttractionPoint(attractionPoint, hostsToConsider).size();
	}
	
	/**
	 * Returns a set of all hosts currently located within a specific {@link IAttractionPoint} area.
	 * 
	 * @param IAttractionPoint The {@link IAttractionPoint} to be checked.
	 * @return Set<SimLocationActuator>
	 */
	public static LinkedHashSet<SimLocationActuator> getHostsOfAttractionPoint(IAttractionPoint attractionPoint, Set<SimLocationActuator> hostsToConsider) {
		
		LinkedHashSet<SimLocationActuator> hosts = new LinkedHashSet<SimLocationActuator>();
		
		for(SimLocationActuator h : hostsToConsider) {		
			if(isHostInAttractionPointArea(h, attractionPoint)) {
				hosts.add(h);				
			}
		}
		
		return hosts;
	}
}
