/*
 * 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.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.SocialGroupMovementModel;
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 partially, dissolve or do nothing after the encounter.
 * 
 * @author Marcel Verst
 * @version 1.0, 22.11.2018
 */
public class MergePartialOrDissolve extends AbstractGroupEncounter {
	//--- VARIABLES TO BE DECLARED IN CONFIG ---
	private double probabilityToMergePartially;
	private double probabilityToDissolve;
	private int numberOfHostsToLeave;
	//------------------------------------------

	@XMLConfigurableConstructor({"enableGroupEncounters","groupEncounterMeetingDistance","groupReencounterWaitTime"})
	public MergePartialOrDissolve(boolean enableGroupEncounters,
			double groupEncounterMeetingDistance,
			long groupReencounterWaitTime) {
		super(enableGroupEncounters, groupEncounterMeetingDistance,
				groupReencounterWaitTime);		
	}
	
	@Override
	public void initialize(SocialGroupMovementModel movementModel) {
		super.initialize(movementModel);
		checkConfiguration();
	}

	/**
	 * {@inheritDoc}
	 * Applies dependent on probability values if the groups merge partially, dissolve or do nothing.
	 */
	@Override
	public void handleGroupEncounter(SocialMovementGroup group1, SocialMovementGroup group2) {
		int decision = decide();

		if(decision == DECISION_MERGE_PARTIAL) {
			mergeGroupsPartially(group1, group2, numberOfHostsToLeave);
		}
		else if(decision == DECISION_DISSOLVE) {
			dissolveGroups(group1, group2);
		}
	}
	
	/**
	 * Decider to set the function which a leader should call if its group is in meeting range with another group.
	 * 
	 * @return int
	 */
	protected int decide() {
		
		double decider = rand.nextDouble();

		double maxMergePartially = probabilityToMergePartially;
		double maxDissolve = probabilityToMergePartially + probabilityToDissolve;

		// mergePartially
		if(decider >= 0.0 && decider < maxMergePartially) {
			return DECISION_MERGE_PARTIAL;
		}
		// dissolve
		else if(decider >= maxMergePartially && decider < maxDissolve) {
			return DECISION_DISSOLVE;
		}
		else 
			return DECISION_NONE;
	}
	
	public void setProbabilityToMergePartially(double probabilityToMergePartially) {
		if (probabilityToMergePartially < 0 || probabilityToMergePartially > 1.0) {
			throw new ConfigurationException(
					"probabilityToMergePartially should be between 0.0 and 1.0!");
		}
		this.probabilityToMergePartially = probabilityToMergePartially;
	}

	public void setProbabilityToDissolve(double probabilityToDissolve) {
		if (probabilityToDissolve < 0 || probabilityToDissolve > 1.0) {
			throw new ConfigurationException(
					"probabilityToDissolve should be between 0.0 and 1.0!");
		}
		this.probabilityToDissolve = probabilityToDissolve;
	}

	public void setNumberOfHostsToLeave(int numberOfHostsToLeave) {
		if (numberOfHostsToLeave < 0) {
			throw new ConfigurationException(
					"numberOfHostsToLeave should be greater or equal than 0!");
		}
		this.numberOfHostsToLeave = numberOfHostsToLeave;
	}

	/**
	 * Checks for the correct configuration.
	 * 
	 * @author Marcel Verst
	 */
	private void checkConfiguration() {
		if(probabilityToMergePartially + probabilityToDissolve > 1.0) {
			throw new ConfigurationException(
					"probabilityToMergePartially + probabilityToDissolve should not exceed 1.0!");
		}
	}
}
