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 AltMode mode;
	private boolean odd;
	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(final AltMode mode, final Map<RuleParam, ParamValue> params) {
		super(params);
		this.mode = mode;
		this.odd = true;
		this.random = new Random(params.getOrDefault(RuleParam.RANDOM_SEED, AltOperation.DEFAULT_RANDOM_SEED.getValue()).asInt());
	}	

	@Override
	public void process(final String line1, final String line2, final LineResult result) throws MixException {		
		result.reset();
		if (line1 == null) {
			result.setResult(line2);
		} else if (line2 == null) {
			result.setResult(line1);
		} else {
			String line = null;
			switch (this.mode) {
				case RANDOM:
					line = this.random.nextBoolean() ? line1 : line2;
					break;
				case NORMAL:
				default:
					line = this.odd ? line1 : line2;
					this.odd = !this.odd;
			}
			result.setResult(line);
		}
	}
	
	@Override
	public void process(final String[] lineRange, final LineResult result) throws MixException {
		//TODO
		process(lineRange[0], lineRange[1], result);
	}

}

@@ -427,16 +427,38 @@
 an odd;%0A
+%09private int channel;%0A
 %09private
@@ -833,16 +833,36 @@
 = true;%0A
+%09%09this.channel = 0;%0A
 %09%09this.r
@@ -1620,64 +1620,950 @@
 %7B%0A%09%09
-//TODO%0A%09%09process(lineRange%5B0%5D, lineRange%5B1%5D, result)
+result.reset();%0A%09%09int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(lineRange.length);%0A%09%09String line = lineRange%5Bchannel%5D;%0A%09%09System.out.println(%22RANGE=%22+Arrays.toString(lineRange));%0A%09%09System.out.println(%22CHANNEL=%22+channel+%22 LINE=%22+line);%0A%09%09if (line == null) %7B%0A%09%09%09channel = nextChannel(lineRange, channel);%0A%09%09%09line = lineRange%5Bchannel%5D;%0A%09%09%09System.out.println(%22NEW_CHANNEL=%22+channel+%22 LINE=%22+line);%0A%09%09%7D%09%09%0A%09%09if (this.mode == AltMode.NORMAL) %7B%0A%09%09%09this.channel = channel + 1;%0A%09%09%09if (this.channel == lineRange.length) %7B%0A%09%09%09%09this.channel = 0;%0A%09%09%09%7D%09%09%0A%09%09%7D%0A%09%09result.setResult(line);%0A%09%7D%0A%0A%09private int nextChannel(final String%5B%5D lineRange, final int curChannel) %7B%0A%09%09int channel = curChannel+1;%0A%09%09while (channel != curChannel) %7B%0A%09%09%09if (channel %3C lineRange.length) %7B%0A%09%09%09%09final String line = lineRange%5Bchannel%5D;%0A%09%09%09%09if (line != null) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09channel++;%0A%09%09%09%7D else %7B%0A%09%09%09%09channel = 0;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return channel
 ;%0A%09%7D%0A
+%09
 %0A%7D%0A

Update DefaultLineAlternation.java