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

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;

import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.topology.ControlCenterTopologyComponent;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.UAVTopologyComponent;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

public class ControlCenterUAVPlacement implements PlacementModel {

	private double worldX;
	private double worldY;
	
	private int numberCenters;
	private int numberUAVs;
	
	public static HashMap<ControlCenterTopologyComponent, PositionVector> positions = 
			new HashMap<ControlCenterTopologyComponent, PositionVector>();
	
	public static HashMap<UAVTopologyComponent, Integer> uavToCCmap = 
			new HashMap<UAVTopologyComponent, Integer>();
	
	public static List<ControlCenterTopologyComponent> CClist = 
			new LinkedList<ControlCenterTopologyComponent>();
		
	public static int counter = 0;

	public void setWorldX(double x) {
		this.worldX = x;
	}
	
	public void setWorldY(double y) {
		this.worldY = y;
	}
	
	@Override
	public void addComponent(TopologyComponent comp) {
				
		if(comp instanceof ControlCenterTopologyComponent) {			
			positions.put((ControlCenterTopologyComponent) comp, getNextPosition());
			CClist.add((ControlCenterTopologyComponent) comp);					
		} else if(comp instanceof UAVTopologyComponent) {			
			int offset = counter % numberCenters;			
			uavToCCmap.put((UAVTopologyComponent) comp, offset);			
			counter++;			
		}
	}

	@Override
	public PositionVector place(TopologyComponent comp) {		
		if(comp instanceof ControlCenterTopologyComponent) {			
			PositionVector pv = positions.get(comp);		
			return pv;
		} else if(comp instanceof UAVTopologyComponent) {			
			int index = uavToCCmap.get(comp);			
			PositionVector pv = positions.get(CClist.get(index));			
			return pv;
		}
		
		return null;
	}

	public void setNumberCenters(int numberCenters) {
		this.numberCenters = numberCenters;
	}

	public void setNumberUAVs(int numberUAVs) {
		this.numberUAVs = numberUAVs;
	}
	
	public PositionVector getNextPosition() {
		
		if(numberCenters == 1) {
			PositionVector pv = new PositionVector(new double[] {worldX, worldY});			
			pv.multiplyScalar(0.5);
			return pv;
		}
		
		// TODO how to place control centers sensibly?
		
		Random r = Randoms.getRandom(RandomPositionDistribution.class);
		
		double[] vec = new double[2];
		
		vec[0] = r.nextDouble() * worldX;
		vec[1] = r.nextDouble() * worldY;
		
		PositionVector position = new PositionVector(vec);
		return position;
	}
	

}
