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

import java.util.LinkedHashMap;
import java.util.Map;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ISocialGroupMovementAnalyzer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * With this transition strategy, nodes are roaming around {@link IAttractionPoint}s, without transitions between them. As the {@link ModularMovementModel} 
 * uses a Gauss function to add jitter and offsets to the movement, some nodes may also roam outside of the circle's radius (this is intended to make it more realistic)
 * 
 * @author Julian Zobel
 * @version 10.02.2020
 */
public class AttractionPointRoamingStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {

	public static enum roamingTransitionState {
		PAUSE,
		ROAMING
	}	
	
	protected Map<SimLocationActuator, roamingTransitionState> roamingStates = new LinkedHashMap<>();
	
	@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
	public AttractionPointRoamingStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
		super(defaultPauseTimeMin, defaultPauseTimeMax);
	}
	
	@Override
	public void addComponent(SimLocationActuator comp) {
		this.roamingStates.put(comp, null);	
		IAttractionPoint nextAP = getNewAttractionPointAssignment(comp);
		updateTargetAttractionPoint(comp, nextAP);
	}
	
	@Override
	public void updateTargetAttractionPoint(SimLocationActuator comp,
			IAttractionPoint attractionPoint) {
		this.roamingStates.put(comp, null);	
		super.updateTargetAttractionPoint(comp, attractionPoint);
	}

	@Override
	public void reachedAttractionPoint(SimLocationActuator comp, IAttractionPoint attractionPoint) {
		
		if(roamingStates.get(comp) == roamingTransitionState.PAUSE) {
			return;
		}
				
		if(Monitor.hasAnalyzer(ISocialGroupMovementAnalyzer.class)) {			
			Monitor.getOrNull(ISocialGroupMovementAnalyzer.class).onNodeReachedAttractionPoint(comp, attractionPoint);
		}
		
		this.roamingStates.put(comp, roamingTransitionState.PAUSE);
		
		// schedule roaming
		long roamingPauseTime =  (long) (rnd.nextDouble() * Time.MINUTE * 5) + Time.MINUTE * 5;		
		Event.scheduleWithDelay(roamingPauseTime, this, comp, 0);	
	}	
	
	private void roamAroundAttractionPoint(SimLocationActuator comp) {
		if(roamingStates.get(comp) == roamingTransitionState.PAUSE) {
			IAttractionPoint currentAttractionPoint = this.assignments.get(comp);			
			this.roamingStates.put(comp, roamingTransitionState.ROAMING);				
			notifyListenersOfAssignmentUpdate(comp, currentAttractionPoint);			
		}		
	}
	
	@Override
	public void eventOccurred(Object content, int type) {
	
		SimLocationActuator comp = (SimLocationActuator) content;			
		IAttractionPoint currentAttractionPoint = this.assignments.get(comp);
		
		// if the attraction point was removed in the meantime, go directly to transit state
		if(currentAttractionPoint == null || !attractionProvider.getAttractionPoints().contains(currentAttractionPoint)) {				
			this.addComponent(comp);
		}
		else {			
			this.roamAroundAttractionPoint(comp);
		}		
	}	
}