package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.ParamValue;
import innovimax.mixthem.arguments.RuleParam;

import java.util.Map;
import java.util.Random;

/**
* <p>Alternates two lines.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineAlternation extends AbstractLineOperation {

	private final static int DEFAULT_RANDOM_SEED = 1789;
	
	private final AltMode mode;
	private boolean first;
	private final Random random;

	/**
	* @param mode The alternate mode to process
 	* @param params The list of parameters (maybe empty)	
	* @see innovimax.mixthem.operation.RuleParam
	* @see innovimax.mixthem.operation.ParamValue
	*/
	public DefaultLineAlternation(AltMode mode, Map<RuleParam, ParamValue> params) {
		super(params);
		this.mode = mode;
		this.first = true;
		this.random = new Random(DEFAULT_RANDOM_SEED);
		if (this.params.containsKey(RuleParam._RANDOM_SEED)) {
			this.random.setSeed(this.params.get(RuleParam._RANDOM_SEED).asInt());
		}
	}	

	/**
 	* Returns the alternated line.
	* @param line1 The first line to alternate
 	* @param line2 The second line to alternate
 	* @return The alternated line
 	* @throws MixException - If an mixing error occurs
 	*/
	@Override
	public String process(String line1, String line2) throws MixException {
		String line = null;
		switch (this.mode) {
			case _RANDOM:
				line = this.random.nextBoolean() ? line1 : line2;
				break;
			case _NORMAL:
			default:
				line = this.first ? line1 : line2;
				this.first = !this.first;
		}
		return line;
	}

}

@@ -1087,10 +1087,31 @@
 line
+ or null if no result
 .%0A
-
 %09* @
@@ -1227,16 +1227,37 @@
 ted line
+ or null if no result
 %0A %09* @th
@@ -1390,17 +1390,118 @@
 eption %7B
+%09%09
 %0A
+%09%09if (line1 == null) %7B%0A%09%09%09return line2;%0A%09%09%7D else if (line2 == null) %7B%0A%09%09%09return line1;%0A%09%09%7D else %7B%0A%09
 %09%09String
@@ -1516,16 +1516,17 @@
 null;%0A%09%09
+%09
 switch (
@@ -1537,24 +1537,25 @@
 .mode) %7B%0A%09%09%09
+%09
 case _RANDOM
@@ -1556,16 +1556,17 @@
 RANDOM:%0A
+%09
 %09%09%09%09line
@@ -1607,24 +1607,25 @@
 e1 : line2;%0A
+%09
 %09%09%09%09break;%0A%09
@@ -1626,16 +1626,17 @@
 eak;%0A%09%09%09
+%09
 case _NO
@@ -1644,16 +1644,17 @@
 MAL:%0A%09%09%09
+%09
 default:
@@ -1654,16 +1654,17 @@
 efault:%0A
+%09
 %09%09%09%09line
@@ -1698,16 +1698,17 @@
 e2;%0A%09%09%09%09
+%09
 this.fir
@@ -1727,18 +1727,20 @@
 irst;%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1746,14 +1746,18 @@
 n line;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

update alt-line operation