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

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * This class checks if there is any pair of groups which encounter each other. Randomly decides
 * if the groups merge fully or not after the encounter.
 * 
 * @author Marcel Verst
 * @version 1.0, 22.11.2018
 */
public class Merge extends AbstractGroupEncounter {
	//--- VARIABLES TO BE DECLARED IN CONFIG ---
	private double probabilityToMergeFully;
	//------------------------------------------

	@XMLConfigurableConstructor({"enableGroupEncounters","groupEncounterMeetingDistance","groupReencounterWaitTime"})
	public Merge(boolean enableGroupEncounters,
			double groupEncounterMeetingDistance,
			long groupReencounterWaitTime) {
		super(enableGroupEncounters, groupEncounterMeetingDistance,
				groupReencounterWaitTime);		
	}
	
	/**
	 * 
	 * Lets both groups merge fully with a certain probability in case they encounter.
	 */
	@Override
	public void handleGroupEncounter(SocialMovementGroup group1, SocialMovementGroup group2) {
		if(decide() == DECISION_MERGE_FULL) {
			mergeGroupsFully(group1, group2);
		}
	}

	
	public void setProbabilityToMergeFully(double probabilityToMergeFully) {
		if (probabilityToMergeFully < 0 || probabilityToMergeFully > 1.0) {
			throw new ConfigurationException(
					"probabilityToMergeFully should be between 0.0 and 1.0!");
		}
		this.probabilityToMergeFully = probabilityToMergeFully;
	}
	
	protected int decide() {
		// mergePartially
		if(rand.nextDouble() < probabilityToMergeFully) {
			return DECISION_MERGE_FULL;
		}		
		else 
			return DECISION_NONE;
	}
}
