/*
 * 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.LinkedHashMap;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.IAttractionBasedMovementAnalyzer;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * In the current implementation, {@link IAttractionPoint}s cannot move anymore.
 * We hold a static list of attraction points to ensure that there exists only
 * one instance of each name at a time.
 * 
 * @author Christoph Muenker, Bjoern Richerzhagen, Julian Zobel
 * @version 1.0, 02.07.2013
 * @version 1.1, 26.10.2018 - since the class was very similar to {@link BasicAttractionPoint}, made this class an extension 
 * @version 1.2, 24.01.2020 - added pause time interval, weight, and radius exclusively to this class, because {@link BasicAttractionPoint} should be just a named point.
 */
public class AttractionPoint extends BasicAttractionPoint {	
	protected static LinkedHashMap<String, AttractionPoint> instances = new LinkedHashMap<>();
	
	protected long pauseTimeMin = -1;
    protected long pauseTimeMax = -1;
    protected double weight = 0;
    protected double radius = 0;
    protected double area = 1;
	
    @XMLConfigurableConstructor({ "name", "x", "y" })
    public AttractionPoint(String name, double x, double y) {
    	this(name, new PositionVector(x, y));
    }
        
    public AttractionPoint(String name, PositionVector posVec) {
    	super(name, posVec);
		
		if (instances.containsKey(name)) {
			throw new AssertionError("Name "+name+" already in use for an attraction point.");
		}
		instances.put(name, this);		
		
		if(Monitor.hasAnalyzer(IAttractionBasedMovementAnalyzer.class)) {			
			Monitor.getOrNull(IAttractionBasedMovementAnalyzer.class).onAttractionPointAdded(this);
		}
    }
	    
	public AttractionPoint(String name, PositionVector posVec, double weight, double radius, long pauseTimeMin, long pauseTimeMax) {
		this(name, posVec);
		
		assert weight >= 0 && weight <= 1.0;
		assert radius >= 0;
		
		this.weight = weight;
		this.radius = radius;
		this.setPauseTime(pauseTimeMin, pauseTimeMax);
		
		if(radius > 0) {
			this.area = Math.PI * Math.pow(radius, 2);
		}		
	}

	@Override
	public IAttractionPoint clone(String newName) {
		return new AttractionPoint(name, this, weight, radius, pauseTimeMin, pauseTimeMax);
	}
	
	@Override
	public int getTransmissionSize() {
		return super.getTransmissionSize() + name.length() + Double.BYTES * 2;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = prime * ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) 
			return false;		
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		AttractionPoint other = (AttractionPoint) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name)) {
			return false;
		} else if(weight != other.weight) {
			return false;
		} else if(radius != other.radius) {
			return false;
		} else if(pauseTimeMin != other.pauseTimeMin) {
			return false;
		} else if(pauseTimeMax != other.pauseTimeMax) {
			return false;
		}
		return true;
	}
	
	 @Override
    public double getWeight() {
        return weight;
    }

    @Override
    public double getRadius() {
        return radius;
    }

    @Override
    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public void setRadius(double radius) {
        this.radius = radius;
        
        if(radius > 0) {
			this.area = Math.PI * Math.pow(radius, 2);
		}	
    }	    

	@Override
	public long getPauseTimeMin() {
		return pauseTimeMin;
	}

	@Override
	public long getPauseTimeMax() {
		return pauseTimeMax;
	}

	@Override
	public void setPauseTime(long pauseTimeMin, long pauseTimeMax) {
		assert pauseTimeMin >= 0 && pauseTimeMax >= 0;
		assert pauseTimeMax >= pauseTimeMin;
		
		this.pauseTimeMin = pauseTimeMin;
		this.pauseTimeMax = pauseTimeMax;
	}
	
	@Override
	public boolean hasPauseTime() {
		if(pauseTimeMax >= 0 || pauseTimeMin >= 0) {
			return true;
		}
		else 
			return false;
	}
	
	@Override
	public boolean hasWeight() {
		return true;
	}
	
	@Override
	public boolean hasRadius() {
		return true;
	}
	
	@Override
	public String toString() {
		return getName() + " (" + getX() + ", " + getY() + ")" + " <" + getRadius() + "> ";
	}
	
	public double getArea() {
		return area;
	}
}
